Skip to content

Commit

Permalink
fix 4, --filter=[unit]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Dec 30, 2024
1 parent 4144890 commit 0db14a3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 59 deletions.
5 changes: 5 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ static inline size_t sw_active_thread_count(void) {
return 1;
}
#endif

void sw_php_exit(int status);
void sw_php_print_backtrace(zend_long cid = 0,
zend_long options = 0,
zend_long limit = 0,
zval *return_value = nullptr);

//----------------------------------Constant API------------------------------------

Expand Down
33 changes: 20 additions & 13 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1399,18 +1399,7 @@ static PHP_METHOD(swoole_coroutine, getBackTrace) {
}
}

static PHP_METHOD(swoole_coroutine, printBackTrace) {
zend_long cid = 0;
zend_long options = 0;
zend_long limit = 0;

ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(cid)
Z_PARAM_LONG(options)
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

void sw_php_print_backtrace(zend_long cid, zend_long options, zend_long limit, zval *return_value) {
zval argv[2];
ZVAL_LONG(&argv[0], options);
ZVAL_LONG(&argv[1], limit);
Expand All @@ -1421,7 +1410,10 @@ static PHP_METHOD(swoole_coroutine, printBackTrace) {
PHPContext *ctx = (PHPContext *) PHPCoroutine::get_context_by_cid(cid);
if (UNEXPECTED(!ctx)) {
swoole_set_last_error(SW_ERROR_CO_NOT_EXISTS);
RETURN_FALSE;
if (return_value) {
RETVAL_FALSE;
}
return;
}
zend_execute_data *ex_backup = EG(current_execute_data);
EG(current_execute_data) = ctx->execute_data;
Expand All @@ -1430,6 +1422,21 @@ static PHP_METHOD(swoole_coroutine, printBackTrace) {
}
}

static PHP_METHOD(swoole_coroutine, printBackTrace) {
zend_long cid;
zend_long options = 0;
zend_long limit = 0;

ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(cid)
Z_PARAM_LONG(options)
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

sw_php_print_backtrace(cid, options, limit, return_value);
}

static PHP_METHOD(swoole_coroutine, list) {
zval zlist;
array_init(&zlist);
Expand Down
56 changes: 38 additions & 18 deletions ext-src/swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,15 @@ static zend_internal_arg_info *copy_arginfo(zend_function *zf, zend_internal_arg
}

static void free_arg_info(zend_internal_function *function) {
if ((function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
function->arg_info) {

if ((function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE | ZEND_ACC_HAS_TYPE_HINTS)) && function->arg_info) {
uint32_t i;
uint32_t num_args = function->num_args + 1;
zend_internal_arg_info *arg_info = function->arg_info - 1;

if (function->fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
for (i = 0 ; i < num_args; i++) {
for (i = 0; i < num_args; i++) {
zend_type_release(arg_info[i].type, /* persistent */ 1);
}
free(arg_info);
Expand Down Expand Up @@ -1263,8 +1261,40 @@ static void hook_stream_throw_exception(const char *type) {
swoole_exception_ce, SW_ERROR_PHP_FATAL_ERROR, "failed to register `%s` stream transport factory", type);
}

static void hook_remove_stream_flags(uint32_t *flags_ptr) {
uint32_t flags = *flags_ptr;
// stream factory
if (flags & PHPCoroutine::HOOK_TCP) {
flags ^= PHPCoroutine::HOOK_TCP;
}
if (flags & PHPCoroutine::HOOK_UDP) {
flags ^= PHPCoroutine::HOOK_UDP;
}
if (flags & PHPCoroutine::HOOK_UNIX) {
flags ^= PHPCoroutine::HOOK_UNIX;
}
if (flags & PHPCoroutine::HOOK_UDG) {
flags ^= PHPCoroutine::HOOK_UDG;
}
if (flags & PHPCoroutine::HOOK_SSL) {
flags ^= PHPCoroutine::HOOK_SSL;
}
if (flags & PHPCoroutine::HOOK_TLS) {
flags ^= PHPCoroutine::HOOK_TLS;
}
// stream ops
if (flags & PHPCoroutine::HOOK_FILE) {
flags ^= PHPCoroutine::HOOK_FILE;
}
if (flags & PHPCoroutine::HOOK_STDIO) {
flags ^= PHPCoroutine::HOOK_STDIO;
}
*flags_ptr = flags;
}

static void hook_stream_factory(uint32_t *flags_ptr) {
uint32_t flags = *flags_ptr;

if (flags & PHPCoroutine::HOOK_TCP) {
if (!(runtime_hook_flags & PHPCoroutine::HOOK_TCP)) {
if (php_stream_xport_register("tcp", socket_create) != SUCCESS) {
Expand Down Expand Up @@ -1646,22 +1676,12 @@ bool PHPCoroutine::enable_hook(uint32_t flags) {
*/
if (sw_is_main_thread()) {
if (sw_active_thread_count() > 1) {
zend_throw_exception(swoole_exception_ce,
"The stream runtime hook must be enabled or disabled only when "
"there are no active threads.",
SW_ERROR_UNDEFINED_BEHAVIOR);
swoole_warning(
"The stream runtime hook must be enabled or disabled only when there are no active threads.");
hook_remove_stream_flags(&flags);
}
} else {
// stream factory
flags ^= PHPCoroutine::HOOK_TCP;
flags ^= PHPCoroutine::HOOK_UDP;
flags ^= PHPCoroutine::HOOK_UNIX;
flags ^= PHPCoroutine::HOOK_UDG;
flags ^= PHPCoroutine::HOOK_SSL;
flags ^= PHPCoroutine::HOOK_TLS;
// stream ops
flags ^= PHPCoroutine::HOOK_FILE;
flags ^= PHPCoroutine::HOOK_STDIO;
hook_remove_stream_flags(&flags);
}

if (swoole_isset_hook((enum swGlobalHookType) PHP_SWOOLE_HOOK_BEFORE_ENABLE_HOOK)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/swoole_thread/fatal_error_1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Swoole\Thread;

$args = Thread::getArguments();
if (empty($args)) {
echo "start child thread\n";
$threads[] = new Thread(__FILE__, 'error');
$threads[0]->join();
echo "stop child thread\n";
} else {
Co\run(function () {
(function () {
swoole_implicit_fn('fatal_error');
})();
});
}
echo "DONE\n";
17 changes: 1 addition & 16 deletions tests/swoole_thread/fatal_error_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,8 @@ skip_if_nts();
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Thread;

$pm = ProcessManager::exec(function () {
$args = Thread::getArguments();
if (empty($args)) {
echo "start child thread\n";
$threads[] = new Thread(__FILE__, 'error');
$threads[0]->join();
echo "stop thread exited\n";
} else {
Co\run(function () {
(function () {
swoole_implicit_fn('fatal_error');
})();
});
}
echo "DONE\n";
include __DIR__ . '/fatal_error_1.inc';
});
$output = $pm->getChildOutput();
Assert::contains($output, "start child thread\n");
Expand Down
16 changes: 16 additions & 0 deletions tests/swoole_thread/fatal_error_2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Swoole\Thread;

$args = Thread::getArguments();
if (empty($args)) {
echo "start child thread\n";
$threads[] = new Thread(__FILE__, 'error');
$threads[0]->join();
echo "stop child thread\n";
} else {
(function () {
swoole_implicit_fn('fatal_error');
})();
}
echo "DONE\n";
13 changes: 1 addition & 12 deletions tests/swoole_thread/fatal_error_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ require __DIR__ . '/../include/bootstrap.php';
use Swoole\Thread;

$pm = ProcessManager::exec(function () {
$args = Thread::getArguments();
if (empty($args)) {
echo "start child thread\n";
$threads[] = new Thread(__FILE__, 'error');
$threads[0]->join();
echo "stop thread exited\n";
} else {
(function () {
swoole_implicit_fn('fatal_error');
})();
}
echo "DONE\n";
include __DIR__ . '/fatal_error_2.inc';
});
$output = $pm->getChildOutput();
Assert::contains($output, "start child thread\n");
Expand Down
1 change: 1 addition & 0 deletions tests/swoole_thread/fatal_error_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ $tm->run();
child thread
main thread
shutdown
[%s] WARNING PHPCoroutine::enable_hook(): The stream runtime hook must be enabled or disabled only when there are no active threads.
[%s] WARNING php_swoole_thread_rshutdown(): Fatal Error: 2 active threads are running, cannot exit safely.

0 comments on commit 0db14a3

Please sign in to comment.