From d1e4fec635463c4a5df4eecab3e94bce18d3a852 Mon Sep 17 00:00:00 2001 From: MARiA so cute <33935209+NathanFreeman@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:06:19 +0800 Subject: [PATCH] Fix thread error (#5092) --- src/coroutine/system.cc | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/coroutine/system.cc b/src/coroutine/system.cc index 0d15883672f..37738b8979b 100644 --- a/src/coroutine/system.cc +++ b/src/coroutine/system.cc @@ -266,6 +266,20 @@ bool System::wait_signal(int signo, double timeout) { Reactor::EXIT_CONDITION_CO_SIGNAL_LISTENER, [](Reactor *reactor, size_t &event_num) -> bool { return SwooleTG.co_signal_listener_num == 0; }); } + +#ifdef SW_USE_THREAD_CONTEXT + swoole_event_defer( + [signo](void *) { + swoole_signal_set(signo, [](int signo) { + Coroutine *co = listeners[signo]; + if (co) { + listeners[signo] = nullptr; + co->resume(); + } + }); + }, + nullptr); +#else swoole_signal_set(signo, [](int signo) { Coroutine *co = listeners[signo]; if (co) { @@ -273,6 +287,8 @@ bool System::wait_signal(int signo, double timeout) { co->resume(); } }); +#endif + SwooleTG.co_signal_listener_num++; TimerNode *timer = nullptr; @@ -296,7 +312,11 @@ bool System::wait_signal(int signo, double timeout) { }; co->yield(&cancel_fn); +#ifdef SW_USE_THREAD_CONTEXT + swoole_event_defer([signo](void *) { swoole_signal_set(signo, nullptr); }, nullptr); +#else swoole_signal_set(signo, nullptr); +#endif SwooleTG.co_signal_listener_num--; if (listeners[signo] != nullptr) { @@ -503,15 +523,16 @@ struct EventWaiter { } if (timeout > 0) { - timer = swoole_timer_add(timeout, - false, - [](Timer *timer, TimerNode *tnode) { - EventWaiter *waiter = (EventWaiter *) tnode->data; - waiter->timer = nullptr; - waiter->error_ = ETIMEDOUT; - waiter->co->resume(); - }, - this); + timer = swoole_timer_add( + timeout, + false, + [](Timer *timer, TimerNode *tnode) { + EventWaiter *waiter = (EventWaiter *) tnode->data; + waiter->timer = nullptr; + waiter->error_ = ETIMEDOUT; + waiter->co->resume(); + }, + this); } co->yield(&cancel_fn); @@ -639,7 +660,7 @@ bool async(async::Handler handler, AsyncEvent &event, double timeout) { return false; } else { event.canceled = _ev->canceled; - event.error = errno = _ev->error; + event.error = errno = _ev->error; event.retval = _ev->retval; return true; }