Repository navigation
Remove Zend Signals - #25627
Remove Zend Signals#25627dunglas wants to merge 3 commits into
Conversation
bc1048e to
6c87180
Compare
6c87180 to
4068490
Compare
There was a problem hiding this comment.
I have not reviewed the patch but I can confirm that zend signals breaks assumptions about signals that threaded Apache MPMs make. I'd been meaning to open a PR that warned when building ZTS with zend signals, but removing it would be better, theoretically.
4068490 to
6af2297
Compare
AWS x86_64 (c6id.metal)
Laravel 12.11.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)
Symfony 2.8.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)
Wordpress 6.9 main page - 50 iterations, 20 warmups, 20 requests (sec)
bench.php - 50 iterations, 20 warmups, 2 requests (sec)
|
6af2297 to
91c1b42
Compare
arnaud-lb
left a comment
There was a problem hiding this comment.
I confirm the benchmark numbers (no regressions).
I'm not sure anymore that we even need to protect the critical sections against signal deliveries. This protects against longjmp() in a signal handler leaving the lock in this state forever, and from interruption in the middle of an SHM update due to signals whose disposition is to terminate the process. But the former doesn't exist anymore, and opcache is supposed to be resilient to the later by other means, because of SIGKILL / OOM kill / exit() from another thread.
So unless I'm forgetting something me may remove Zend Signal without replacing it by masking.
If we keep masking, we should mask for the entire process even in ZTS.
Request shutdown reset every signal touched by pcntl_signal() to SIG_DFL, discarding the handler installed by the SAPI or the host. With the CLI server, a request installing a SIGPIPE handler left the server to be killed by the next SIGPIPE instead of ignoring it. In FPM, replacing SIGQUIT turned the next graceful reload into an immediate kill. Save the handler in place before the first pcntl_signal() call for each signal, and reinstall it at request shutdown.
91c1b42 to
ae7e1d3
Compare
They deferred signals around shared memory writes so that a handler could not longjmp() out while holding the write lock. Since PHP 7.1, the timeout handler no longer raises the error itself, and in-tree handlers set a flag or terminate the process, apart from phpdbg bailing out on a second SIGINT while stopping. That ends like a fatal error during a write: accel_post_deactivate() releases the lock. Readers look up the shared hash without the lock, so each entry is already published with a single pointer store once complete. A process dying mid-write, through SIGKILL, the OOM killer or exit() from another thread, leaves a state readers already handle. Builds without Zend Signals never deferred anything.
Zend Signals installed process-wide handlers for seven signals on every request and deferred them while a per-thread counter was non-zero. Since timeouts only set EG(vm_interrupt), the only remaining callers were the OPcache shared memory writes, which no longer need it. On ZTS the design cannot work: dispositions and delivery are per process while the handler table, depth counter and queue are per thread. A signal delivered to another thread is not deferred, and that thread does not see handlers registered by the one that installed them. The per-request handlers also replaced those of host runtimes without SA_ONSTACK. Official Docker images and many ZTS packages already disable it. Zend/zend_signal.h and its API are removed too: SAPIs and extensions use signal() and sigaction() directly.
ae7e1d3 to
d751a25
Compare
|
@arnaud-lb thank you very much for your review. Indeed, it looks safe to remove the feature entirely. |
arnaud-lb
left a comment
There was a problem hiding this comment.
This looks good to me! I will merge if there are no objections.
|
@arnaud-lb I did a bit of digging and it looks like opcache might not be that resilient to FPM killing a worker after this change. FPM sends SIGTERM to the worker after request_terminate_timeout, and if the worker is just storing a file to opcache at that time, it now gets killed in the middle of the SHM write. Previously this was handled using zend_signal_handler_defer so opcache could finish the write, but now there is no such protection. It looks to me that this is a problem for interned strings because accel_new_interned_string() links the new string to the hash chain before it writes it and before it moves top. So if the worker dies in between, it can end up with a collision chain pointing to itself (lookups would loop forever in all workers) or with a string that gets overwritten later. From the inspection it seems that this can already happen with SIGKILL and also in inheritance cache, which is not protected even now, so it's not completely new. It should not normally happen but I'm not sure it's good to add more of those cases. This one might be fixable by re-ordering: --- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@ -554,10 +554,11 @@
s = ZCSG(interned_strings).top;
hash_slot = STRTAB_HASH_TO_SLOT(&ZCSG(interned_strings), h);
STRTAB_COLLISION(s) = *hash_slot;
- *hash_slot = STRTAB_STR_TO_POS(&ZCSG(interned_strings), s);
GC_SET_REFCOUNT(s, 2);
GC_TYPE_INFO(s) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT)| (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0);
ZSTR_H(s) = h;
ZSTR_LEN(s) = ZSTR_LEN(str);
memcpy(ZSTR_VAL(s), ZSTR_VAL(str), ZSTR_LEN(s) + 1);
ZCSG(interned_strings).top = STRTAB_NEXT(s);
+ /* Link last so a process killed here cannot leave a partial string visible */
+ *hash_slot = STRTAB_STR_TO_POS(&ZCSG(interned_strings), s);It would be probably good to check if there are more such cases. |
accel_new_interned_string() linked the new string into its hash slot before writing it and advancing the buffer top. A process killed in between, by SIGKILL or the OOM killer, left the slot pointing at the unchanged top. When the next string written there hashed to the same bucket, its collision link pointed at itself, and every later lookup in that bucket spun forever. Link the string last, once it is complete and the top has moved past it. Reported in php#25627 (comment).
The restarting process cleared restart_pending before resetting the cache and only restored accelerator_enabled at the end. If it was killed in between, by SIGKILL or the OOM killer, nothing scheduled another restart and the cache stayed disabled until the server was restarted. Clear restart_pending last, so the next request redoes the whole restart, whose steps can run again on a partially reset cache. zend_accel_schedule_restart() set restart_pending before saving accelerator_enabled into cache_status_before_restart. A kill in between made the next restart restore a stale value, false on a cache that never restarted before. Set restart_pending once that state is saved. Found while working on the removal of Zend Signals in php#25627.
Compiled traces refer to their slot in zend_jit_traces by number, for exit info and the stack map. Root traces were installed and side traces linked into their parent before ZEND_JIT_TRACE_NUM moved past their slot. If the process was killed in between, by SIGKILL or the OOM killer, the next trace reused the slot while the installed code still pointed at it, so a side exit from that code restored the VM state from the wrong trace's metadata. Do the bookkeeping first and make the code reachable last. Found while working on the removal of Zend Signals in php#25627.
shm_put_var() advanced the end of the data before shrinking the free space, and removing a variable grew the free space before moving the end back. A process killed between the two stores, by SIGKILL or the OOM killer, left a segment whose free space overstated what was left. The segment outlives the process, so later shm_put_var() calls wrote past its end. Order the stores so that the free space never exceeds what is left. A kill in the middle of a removal can still leave the moved variables inconsistent. Found while working on the removal of Zend Signals in php#25627.
The restarting process cleared restart_pending before resetting the cache and only restored accelerator_enabled at the end. If it was killed in between, by SIGKILL or the OOM killer, nothing scheduled another restart and the cache stayed disabled until the server was restarted. Clear restart_pending last, so the next request redoes the whole restart, whose steps can run again on a partially reset cache. Count the restart just before, so a redone restart is counted once. zend_accel_schedule_restart() set restart_pending before saving accelerator_enabled into cache_status_before_restart. A kill in between made the next restart restore a stale value, false on a cache that never restarted before. Set restart_pending once that state is saved. A kill can still leave restart_pending set with the cache enabled, and requests kept using the cache while a restart was pending, so under overlapping traffic the readers never drained and the restart waited for opcache.force_restart_timeout, forever when it is 0. Don't let requests use the cache while a restart is pending. Found while working on the removal of Zend Signals in php#25627.
Compiled traces refer to their slot in zend_jit_traces by number, for exit info and the stack map. Root traces were installed and side traces linked into their parent before ZEND_JIT_TRACE_NUM moved past their slot. If the process was killed in between, by SIGKILL or the OOM killer, the next trace reused the slot while the installed code still pointed at it, so a side exit from that code restored the VM state from the wrong trace's metadata. Do the bookkeeping first and make the code reachable last. Found while working on the removal of Zend Signals in php#25627.
accel_new_interned_string() linked the new string into its hash slot before writing it and advancing the buffer top. A process killed in between, by SIGKILL or the OOM killer, left the slot pointing at the unchanged top. When the next string written there hashed to the same bucket, its collision link pointed at itself, and every later lookup in that bucket spun forever. Link the string last, once it is complete and the top has moved past it. Reported in php#25627 (comment).
The restarting process cleared restart_pending before resetting the cache and only restored accelerator_enabled at the end. If it was killed in between, by SIGKILL or the OOM killer, nothing scheduled another restart and the cache stayed disabled until the server was restarted. Clear restart_pending last, so the next request redoes the whole restart, whose steps can run again on a partially reset cache. Count the restart just before, so a redone restart is counted once. zend_accel_schedule_restart() set restart_pending before saving accelerator_enabled into cache_status_before_restart. A kill in between made the next restart restore a stale value, false on a cache that never restarted before. Set restart_pending once that state is saved. A kill can still leave restart_pending set with the cache enabled, and requests kept using the cache while a restart was pending, so under overlapping traffic the readers never drained and the restart waited for opcache.force_restart_timeout, forever when it is 0. Don't let requests use the cache while a restart is pending. Found while working on the removal of Zend Signals in php#25627.
Compiled traces refer to their slot in zend_jit_traces by number, for exit info and the stack map. Root traces were installed and side traces linked into their parent before ZEND_JIT_TRACE_NUM moved past their slot. If the process was killed in between, by SIGKILL or the OOM killer, the next trace reused the slot while the installed code still pointed at it, so a side exit from that code restored the VM state from the wrong trace's metadata. Do the bookkeeping first and make the code reachable last. Found while working on the removal of Zend Signals in php#25627.
Zend Signals defers signals delivered while the engine is in a critical section, mainly to keep the opcache SHM lock from being left held. It has two design problems on ZTS:
zend_signal_register()withoutSA_ONSTACK(onlyzend_sigaction()sets it since Set SA_ONSTACK in zend_sigaction #9597), which breaks runtimes embedding PHP that rely on alternate signal stacks (Go via FrankenPHP for instance).The official Docker images and many ZTS packages already build with
--disable-zend-signals.This PR removes it without a replacement. Since PHP 7.1 (650c1c0) the timeout handler no longer raises the error itself, and in-tree handlers set a flag or terminate the process, apart from phpdbg bailing out on a second SIGINT, which ends like a fatal error during a write:
accel_post_deactivate()releases the lock. Opcache readers look up the shared hash without the lock, so a process dying mid-write (SIGKILL, OOM killer,exit()from another thread) leaves a state they already handle. Builds without Zend Signals have always run this way.pcntl_signal()instead ofSIG_DFL. Zend Signals did this implicitly.HANDLE_(UN)BLOCK_INTERRUPTIONS()calls around SHM writes.Zend/zend_signal.h,Zend/zend_signal.c, theHANDLE_(UN)BLOCK_INTERRUPTIONS()macros, thezend.signal_checkINI setting and the--disable-zend-signalsconfigure option.Syscalls per request in an FPM worker serving a hello world script (strace, 1,000 requests, startup subtracted):
rt_sigactionFPM throughput (NTS release builds, nginx + 4 static children, 8 alternating rounds, medians):
Rounds vary by 10% or more on this machine, so these differences are noise.