Skip to content

Commit

Permalink
Fix coredump
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamsxin committed Nov 4, 2019
1 parent c40a24e commit d46bf63
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
40 changes: 17 additions & 23 deletions examples/async/wsab.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,21 @@ function microtime_float($usefloat = false)
'name' => 'requests',
'shortName' => 'n',
'required' => true,
'help' => "Number of requests to perform",
'help' => "Number of requests to perform",
]);
$opts->add([
'type' => \Phalcon\Cli\Options::TYPE_INT,
'name' => 'concurrency',
'shortName' => 'c',
'required' => true,
'help' => "Number of multiple requests to make",
'help' => "Number of multiple requests to make",
]);
$opts->add([
'type' => \Phalcon\Cli\Options::TYPE_STRING,
'name' => 'url',
'shortName' => 'u',
'required' => true,
'help' => "websocket server",
'help' => "websocket server",
]);
$vals = $opts->parse();
if (!isset($vals['url'])) {
Expand All @@ -536,8 +536,6 @@ function microtime_float($usefloat = false)
return;
}

$group = $requests/$concurrency;

$tasks = [];
$total_request = 0;
$connect_success = 0;
Expand Down Expand Up @@ -565,28 +563,25 @@ function microtime_float($usefloat = false)
$ws->send('hello');
$send_success++;
});

$etime = microtime_float(true);
$requesttime = $etime - $stime;
if ($min_request_time < 0 || $requesttime < $min_request_time) {
$min_request_time = $requesttime;
}
if ($requesttime > $max_request_time) {
$max_request_time = $requesttime;
}
} catch (\Throwable $e) {var_dump($e);
$connect_fail++;
}

$etime = microtime_float(true);
$requesttime = $etime - $stime;
if ($min_request_time < 0 || $requesttime < $min_request_time) {
$min_request_time = $requesttime;
}
if ($requesttime > $max_request_time) {
$max_request_time = $requesttime;
}
};

$channel = new \Phalcon\Async\Channel($concurrency);
$channel = new \Phalcon\Async\Channel(0);

for ($j=0; $j<=$concurrency; $j++) {
for ($j=0; $j<$concurrency; $j++) {
$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) {
foreach ($it as $url) {
if ($url == 'close') {
break;
}

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

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

$n = 0;
$shownum = ceil($requests/10);
for ($i=0; $i<$requests; $i++) {
for ($i=1; $i<=$requests; $i++) {
$channel->send($url);
$c = floor($i/$shownum);
if ($c > $n || $i == ($requests-1)) {
Expand All @@ -608,13 +603,12 @@ function microtime_float($usefloat = false)
}
}

$channel->send('close');

$channel->close();
foreach ($tasks as $t) {
\Phalcon\Async\Task::await($t);
}
echo 'Completed requests'.PHP_EOL;

$channel->close();

$finish_time = microtime_float(true);
$total_time = ($finish_time - $begin_time);
Expand Down
2 changes: 1 addition & 1 deletion ext/async/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,7 @@ ASYNC_FIBER_CALLBACK run_scheduler_fiber(void *arg)

scheduler->flags &= ~ASYNC_TASK_SCHEDULER_FLAG_ACTIVE;

if (UNEXPECTED(scheduler->flags & (ASYNC_TASK_SCHEDULER_FLAG_DISPOSED | ASYNC_TASK_SCHEDULER_FLAG_ERROR))) {
if (UNEXPECTED(scheduler->flags & ASYNC_TASK_SCHEDULER_FLAG_ERROR)) {
break;
}
} while (again || scheduler->refticks > 0);
Expand Down
9 changes: 6 additions & 3 deletions ext/async/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,14 @@ static PHP_METHOD(TcpSocket, connect)
Z_PARAM_OPTIONAL
Z_PARAM_OBJECT_OF_CLASS_EX(tls, async_tls_client_encryption_ce, 1, 0)
ZEND_PARSE_PARAMETERS_END();


ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_DISPOSED, async_socket_exception_ce, "Task scheduler has been disposed");
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");

code = async_dns_lookup_ip(ZSTR_VAL(name), &dest, IPPROTO_TCP);

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

async_socket_set_port((struct sockaddr *) &dest, port);

socket = async_tcp_socket_object_create();
Expand Down
3 changes: 3 additions & 0 deletions ext/async/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ static PHP_METHOD(UdpSocket, connect)
host = Z_STR_P(name);
}

ASYNC_CHECK_EXCEPTION(async_task_scheduler_get()->flags & ASYNC_TASK_SCHEDULER_FLAG_DISPOSED, async_socket_exception_ce, "Task scheduler has been disposed");
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");

code = async_dns_lookup_ip(ZSTR_VAL(host), &addr, IPPROTO_UDP);

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

0 comments on commit d46bf63

Please sign in to comment.