Skip to content

Commit

Permalink
[4.8]Fix bug #5080 (#5082)
Browse files Browse the repository at this point in the history
* Fix assert failed

* Fix bug #5080

* fix error

* Fix thread error
  • Loading branch information
NathanFreeman committed Jul 4, 2023
1 parent 6cdb42d commit 6e1fd74
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
12 changes: 12 additions & 0 deletions ext-src/swoole_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,11 @@ static PHP_METHOD(swoole_process, signal) {
if (zcallback == nullptr) {
fci_cache = signal_fci_caches[signo];
if (fci_cache) {
#ifdef SW_USE_THREAD_CONTEXT
swoole_event_defer([signo](void *) { swoole_signal_set(signo, nullptr); }, nullptr);
#else
swoole_signal_set(signo, nullptr);
#endif
signal_fci_caches[signo] = nullptr;
swoole_event_defer(sw_zend_fci_cache_free, fci_cache);
SwooleTG.signal_listener_num--;
Expand Down Expand Up @@ -576,7 +580,11 @@ static PHP_METHOD(swoole_process, signal) {
SwooleTG.signal_listener_num++;
}
signal_fci_caches[signo] = fci_cache;
#ifdef SW_USE_THREAD_CONTEXT
swoole_event_defer([signo, handler](void *) { swoole_signal_set(signo, handler); }, nullptr);
#else
swoole_signal_set(signo, handler);
#endif
RETURN_TRUE;
}

Expand All @@ -601,7 +609,11 @@ static PHP_METHOD(swoole_process, signal) {
// use user settings
SwooleG.use_signalfd = SwooleG.enable_signalfd;

#ifdef SW_USE_THREAD_CONTEXT
swoole_event_defer([signo, handler](void *) { swoole_signal_set(signo, handler); }, nullptr);
#else
swoole_signal_set(signo, handler);
#endif

RETURN_TRUE;
}
Expand Down
4 changes: 4 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2288,5 +2288,9 @@
<configureoption default="no" name="enable-swoole-json" prompt="enable json support?"/>
<configureoption default="no" name="enable-swoole-curl" prompt="enable curl support?"/>
<configureoption default="no" name="enable-cares" prompt="enable cares support?"/>
<configureoption default="no" name="enable-swoole-pgsql" prompt="enable PostgreSQL database support?"/>
<configureoption default="no" name="with-swoole-odbc" prompt="enable ODBC database support?"/>
<configureoption default="no" name="with-swoole-oracle" prompt="enable Oracle database support?"/>
<configureoption default="no" name="enable-swoole-sqlite" prompt="enable Sqlite database support?"/>
</extsrcrelease>
</package>
42 changes: 32 additions & 10 deletions src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,29 @@ bool System::wait_signal(int signo, double timeout) {
}
/* always enable signalfd */
SwooleG.use_signalfd = SwooleG.enable_signalfd = 1;

#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) {
listeners[signo] = nullptr;
co->resume();
}
});
#endif

SwooleTG.co_signal_listener_num++;

TimerNode *timer = nullptr;
Expand All @@ -299,7 +315,12 @@ 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) {
Expand Down Expand Up @@ -506,15 +527,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);
Expand Down Expand Up @@ -642,7 +664,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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/coroutine/thread_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "swoole_api.h"
#include "swoole_async.h"
#include "swoole_signal.h"
#include "swoole_coroutine_context.h"

#ifdef SW_USE_THREAD_CONTEXT
Expand Down Expand Up @@ -84,6 +85,7 @@ bool Context::swap_out() {
}

void Context::context_func(void *arg) {
swoole_signal_block_all();
Context *_this = (Context *) arg;
SwooleTG.reactor = g_reactor;
SwooleTG.timer = g_timer;
Expand Down

0 comments on commit 6e1fd74

Please sign in to comment.