Skip to content

Commit d46bf63

Browse files
committed
Fix coredump
1 parent c40a24e commit d46bf63

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

examples/async/wsab.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,21 @@ function microtime_float($usefloat = false)
501501
'name' => 'requests',
502502
'shortName' => 'n',
503503
'required' => true,
504-
'help' => "Number of requests to perform",
504+
'help' => "Number of requests to perform",
505505
]);
506506
$opts->add([
507507
'type' => \Phalcon\Cli\Options::TYPE_INT,
508508
'name' => 'concurrency',
509509
'shortName' => 'c',
510510
'required' => true,
511-
'help' => "Number of multiple requests to make",
511+
'help' => "Number of multiple requests to make",
512512
]);
513513
$opts->add([
514514
'type' => \Phalcon\Cli\Options::TYPE_STRING,
515515
'name' => 'url',
516516
'shortName' => 'u',
517517
'required' => true,
518-
'help' => "websocket server",
518+
'help' => "websocket server",
519519
]);
520520
$vals = $opts->parse();
521521
if (!isset($vals['url'])) {
@@ -536,8 +536,6 @@ function microtime_float($usefloat = false)
536536
return;
537537
}
538538

539-
$group = $requests/$concurrency;
540-
541539
$tasks = [];
542540
$total_request = 0;
543541
$connect_success = 0;
@@ -565,28 +563,25 @@ function microtime_float($usefloat = false)
565563
$ws->send('hello');
566564
$send_success++;
567565
});
568-
569-
$etime = microtime_float(true);
570-
$requesttime = $etime - $stime;
571-
if ($min_request_time < 0 || $requesttime < $min_request_time) {
572-
$min_request_time = $requesttime;
573-
}
574-
if ($requesttime > $max_request_time) {
575-
$max_request_time = $requesttime;
576-
}
577566
} catch (\Throwable $e) {var_dump($e);
578567
$connect_fail++;
579568
}
569+
570+
$etime = microtime_float(true);
571+
$requesttime = $etime - $stime;
572+
if ($min_request_time < 0 || $requesttime < $min_request_time) {
573+
$min_request_time = $requesttime;
574+
}
575+
if ($requesttime > $max_request_time) {
576+
$max_request_time = $requesttime;
577+
}
580578
};
581579

582-
$channel = new \Phalcon\Async\Channel($concurrency);
580+
$channel = new \Phalcon\Async\Channel(0);
583581

584-
for ($j=0; $j<=$concurrency; $j++) {
582+
for ($j=0; $j<$concurrency; $j++) {
585583
$tasks[] = \Phalcon\Async\Task::asyncWithContext($context, static function (iterable $it) use ($context, $work, &$total_request, &$connect_success, &$connect_fail, &$send_success, &$send_fail, &$recv_success, &$recv_fail, &$min_request_time, &$max_request_time) {
586584
foreach ($it as $url) {
587-
if ($url == 'close') {
588-
break;
589-
}
590585

591586
$work($url, $total_request, $connect_success, $connect_fail, $send_success, $send_fail, $recv_success, $recv_fail, $min_request_time, $max_request_time);
592587

@@ -599,7 +594,7 @@ function microtime_float($usefloat = false)
599594

600595
$n = 0;
601596
$shownum = ceil($requests/10);
602-
for ($i=0; $i<$requests; $i++) {
597+
for ($i=1; $i<=$requests; $i++) {
603598
$channel->send($url);
604599
$c = floor($i/$shownum);
605600
if ($c > $n || $i == ($requests-1)) {
@@ -608,13 +603,12 @@ function microtime_float($usefloat = false)
608603
}
609604
}
610605

611-
$channel->send('close');
612-
606+
$channel->close();
613607
foreach ($tasks as $t) {
614608
\Phalcon\Async\Task::await($t);
615609
}
610+
echo 'Completed requests'.PHP_EOL;
616611

617-
$channel->close();
618612

619613
$finish_time = microtime_float(true);
620614
$total_time = ($finish_time - $begin_time);

ext/async/task.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ ASYNC_FIBER_CALLBACK run_scheduler_fiber(void *arg)
15691569

15701570
scheduler->flags &= ~ASYNC_TASK_SCHEDULER_FLAG_ACTIVE;
15711571

1572-
if (UNEXPECTED(scheduler->flags & (ASYNC_TASK_SCHEDULER_FLAG_DISPOSED | ASYNC_TASK_SCHEDULER_FLAG_ERROR))) {
1572+
if (UNEXPECTED(scheduler->flags & ASYNC_TASK_SCHEDULER_FLAG_ERROR)) {
15731573
break;
15741574
}
15751575
} while (again || scheduler->refticks > 0);

ext/async/tcp.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,14 @@ static PHP_METHOD(TcpSocket, connect)
331331
Z_PARAM_OPTIONAL
332332
Z_PARAM_OBJECT_OF_CLASS_EX(tls, async_tls_client_encryption_ce, 1, 0)
333333
ZEND_PARSE_PARAMETERS_END();
334-
334+
335+
ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_DISPOSED, async_socket_exception_ce, "Task scheduler has been disposed");
336+
ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_ERROR, async_socket_exception_ce, "Task scheduler was stopped due to an error");
337+
335338
code = async_dns_lookup_ip(ZSTR_VAL(name), &dest, IPPROTO_TCP);
336-
339+
337340
ASYNC_CHECK_EXCEPTION(code < 0, async_socket_exception_ce, "Failed to assemble IP address: %s", uv_strerror(code));
338-
341+
339342
async_socket_set_port((struct sockaddr *) &dest, port);
340343

341344
socket = async_tcp_socket_object_create();

ext/async/udp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ static PHP_METHOD(UdpSocket, connect)
306306
host = Z_STR_P(name);
307307
}
308308

309+
ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_DISPOSED, async_socket_exception_ce, "Task scheduler has been disposed");
310+
ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_ERROR, async_socket_exception_ce, "Task scheduler was stopped due to an error");
311+
309312
code = async_dns_lookup_ip(ZSTR_VAL(host), &addr, IPPROTO_UDP);
310313

311314
ASYNC_CHECK_EXCEPTION(code < 0, async_socket_exception_ce, "Failed to assemble IP address: %s", uv_strerror(code));

0 commit comments

Comments
 (0)