Repository navigation
fix(thread): keep main-thread signal handlers when other threads exit - #6410
Open
binaryfire wants to merge 2 commits into
Open
binaryfire wants to merge 2 commits into
binaryfire wants to merge 2 commits into
Conversation
Process::signal() only runs on the main thread, but its callbacks live in a process-wide array. Every thread's request shutdown freed all of them through its own executor, so a main thread with a signal handler aborted with "zend_mm_heap corrupted" as soon as a child thread or SWOOLE_THREAD worker ended. A process forked from a child thread hit the same problem: the after-fork cleanup freed the inherited callbacks from the wrong heap, and the child aborted before running its callback. Skip the cleanup in non-main threads, and have a process forked from a non-main thread drop the inherited callbacks instead of freeing them. Builds without thread support are unchanged.
The main thread registers a SIGTERM handler, a child thread starts a process that must exit cleanly and then returns, and the main thread checks the handler still runs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 6.1 #6410 +/- ##
==========================================
- Coverage 85.81% 85.80% -0.02%
==========================================
Files 110 110
Lines 17113 17113
Branches 3062 3063 +1
==========================================
- Hits 14685 14683 -2
- Misses 2428 2430 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In builds with
--enable-swoole-thread, if the main thread has aProcess::signal()handler, the process crashes as soon as any other thread ends:This aborts with
zend_mm_heap corruptedbeforejoined. ASWOOLE_THREADserver that sets a handler inonStartcrashes the same way when its worker threads exit at shutdown.Cause
Process::signal()only works on the main thread, but its callbacks are stored in the process-widesignal_fci_caches. Every thread's request shutdown callsphp_swoole_process_signal_clean(), which frees all of them through that thread's executor and resets the shared listener count.Process::start()called from a child thread has the same problem in the new process: the after-fork cleanup frees the inherited callbacks from the wrong heap, and the process aborts before running its callback.Fix
swoole_fork()has already removed their signal handlers.Non-ZTS builds are unchanged, since
sw_is_main_thread()is always true there.php_swoole_runtime_rshutdown()already returns early outside the main thread in the same way.Tests
tests/swoole_thread/signal_handler_thread_exit.phpt: the main thread registers a SIGTERM handler, a child thread starts a process that must exit cleanly, and after the join the main thread checks its handler still runs. It fails on 6.1, passes with this change, and fails if either change is removed. I also ran theswoole_thread,swoole_process_poolandswoole_processsignal and fork tests on a PHP 8.4 ZTS thread build, with no new failures.