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

[QUESTION] Bubbling exceptions up the coroutine call stack? #3434

Closed ValiDrv closed 4 years ago

ValiDrv commented 4 years ago

1. What did you do? If possible, provide a simple script for reproducing the error.

Is there a way to bubble up the exceptions to the first coroutine that catches it?

Example:

go(function () {
    try {
        # OOP code, which can contain other coroutines
        go(function () {
            # ... code
            go(function () {
                # Which eventually could throw some exception.
                throw new Exception("Foo");
            });
        });
    } catch (Throwable $e) {
        echo $e->getMessage();
    }
});

We can catch the exception in the deepest go coroutine, then add it to some Chanel, and then wait to pop the Chanel somehow, but that's very tedious, and must use a wrappers for every coroutine call everywhere.

2. What did you expect to see?

Instead, when swoole gets a Fatal error: Uncaught Exception:, it could check if it's in a coroutine, and try to re-throw that exception on each parent level until one catches the exception of finishes the whole coroutine stack, it which point.

3. What did you see instead?

The exception gets thrown at the first level, and then skips all the parent coroutine stacks.

4. What version of Swoole are you using (show your php --ri swoole)?

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.5.2
Built => May 29 2020 03:13:35
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1d  10 Sep 2019
http2 => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

5. What is your machine environment used (including version of kernel & php & gcc) ?

docker: phpswoole/swoole:4.5.2-php7.4

doubaokun commented 4 years ago

At the moment there is no way to bubbling exceptions, as different coroutines are independent.