swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.42k stars 3.16k forks source link

Calling macro `swoole_fatal_error` will immediately terminate the process without executing `php_request_shutdown` in Swoole 5.1.x #5385

Closed Appla closed 2 months ago

Appla commented 3 months ago

With the following codes, php_request_shutdown will be called in PHP 7.4 + Swoole 4.8.x but not in PHP 8.3 + Swoole 5.1.x. Is this expected behavior?

\register_shutdown_function(static function () {
    echo "register_shutdown_function called\n";
});
echo "before\n";
\Swoole\Coroutine::create(static function () {
    (new \Swoole\Process(static fn() => file_get_contents('https://www.google.com')))->start();
});
echo "after\n";
\Swoole\Event::wait();

Outputs:

Fatal error: Uncaught Swoole\Error: must be forked outside the coroutine in /path/to/test.php:8 Stack trace:

0 /path/to/test.php(8): Swoole\Process->start()

1 [internal function]: {closure}()

2 {main}

thrown in /path/to/test.php on line 8


* `PHP 7.4 + Swoole 4.8.13`

before PHP Fatal error: Uncaught Swoole\Error: must be forked outside the coroutine in /path/to/test.php:8 Stack trace:

0 /path/to/test.php(8): Swoole\Process->start()

1 {main}

thrown in /path/to/test.php on line 8 register_shutdown_function called

NathanFreeman commented 3 months ago

In coroutines, fork or Swoole\Process cannot be used, otherwise the coroutine context will be duplicated.

Appla commented 3 months ago

In coroutines, fork or Swoole\Process cannot be used, otherwise the coroutine context will be duplicated.

The above example is intended to trigger swoole_fatal_error to demonstrate the issue.

matyhtf commented 2 months ago

The swoole_fatal_error is triggered, which indicates that it is not a runtime error, there is a problem with your code, you must modify the code.

swoole_fatal_error cannot be caught, and the program will exit immediately.