Skip to content

Remove Zend Signals - #25627

Open
dunglas wants to merge 3 commits into
php:masterfrom
dunglas:refactor/remove-zend-signals
Open

dunglas wants to merge 3 commits into
php:masterfrom
dunglas:refactor/remove-zend-signals

Conversation

@dunglas

@dunglas dunglas commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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:

  • Signal dispositions are per process, but Zend Signals keeps its handler table, depth counter and queue in per-thread globals. A signal delivered to another thread is not deferred, and that thread does not see the handlers registered by the one that installed them.
  • The per-request handlers are installed by zend_signal_register() without SA_ONSTACK (only zend_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: at RSHUTDOWN, restore the handler that was installed before pcntl_signal() instead of SIG_DFL. Zend Signals did this implicitly.
  • opcache: drop the HANDLE_(UN)BLOCK_INTERRUPTIONS() calls around SHM writes.
  • core: delete Zend/zend_signal.h, Zend/zend_signal.c, the HANDLE_(UN)BLOCK_INTERRUPTIONS() macros, the zend.signal_check INI setting and the --disable-zend-signals configure option.

Syscalls per request in an FPM worker serving a hello world script (strace, 1,000 requests, startup subtracted):

base (f09dd61) this PR
rt_sigaction 8 1
all syscalls 33 26

FPM throughput (NTS release builds, nginx + 4 static children, 8 alternating rounds, medians):

Scenario base this PR
hello world (RPS) 44,111 42,755 (-3.1%)
500 classes, warm cache (RPS) 13,104 12,811 (-2.2%)
5,000 files, cold cache (ms) 124.9 126.0 (+0.8%)

Rounds vary by 10% or more on this machine, so these differences are noise.

Comment thread Zend/zend_signal.h Resolved
@dunglas
dunglas force-pushed the refactor/remove-zend-signals branch from 6c87180 to 4068490 Compare November 20, 2026 16:54

@morrisonlevi morrisonlevi left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ext/opcache/ZendAccelerator.c Resolved
@LamentXU123
LamentXU123 removed their request for review November 21, 2026 18:47
@github-actions

Copy link
Copy Markdown

AWS x86_64 (c6id.metal)

Attribute Value
Environment aws
Instance type c6id.metal
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz, 64 cores @ 2900 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 251 GB
Kernel 6.18.56-125.164.amzn2023.x86_64
OS Amazon Linux 2023.12.20261026
GCC 14.2.1
Binary layout strategy bolt-align
Time 2026-11-21 21:51:03 UTC
Job details https://github.com/php/php-src/actions/runs/44953635768 (Artifacts)
Changeset https://github.com/php/php-src/compare/95bca157ab..6af2297a4e

Laravel 12.11.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@95bca15 (ZTS) 0.39514 0.39611 0.00024 0.06% 0.39551 0.00% 0.39544 0.00% 0.964 0.000 1.000 31.76 MB
PHP - refactor/remove-zend-signals (ZTS) 0.39528 0.39678 0.00038 0.10% 0.39591 0.10% 0.39585 0.10% 0.624 -5.519 0.000 31.86 MB

Symfony 2.8.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@95bca15 (ZTS) 0.70034 0.70269 0.00044 0.06% 0.70097 0.00% 0.70084 0.00% 1.644 0.000 1.000 31.85 MB
PHP - refactor/remove-zend-signals (ZTS) 0.70013 0.71942 0.00281 0.40% 0.70114 0.02% 0.70056 -0.04% 6.020 3.857 0.000 31.82 MB

Wordpress 6.9 main page - 50 iterations, 20 warmups, 20 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@95bca15 (ZTS) 0.59993 0.60206 0.00055 0.09% 0.60086 0.00% 0.60083 0.00% 0.226 0.000 1.000 31.88 MB
PHP - refactor/remove-zend-signals (ZTS) 0.59936 0.60172 0.00040 0.07% 0.60005 -0.13% 0.60000 -0.14% 1.564 6.773 0.000 31.85 MB

bench.php - 50 iterations, 20 warmups, 2 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@95bca15 (ZTS) 0.48025 0.49930 0.00425 0.88% 0.48237 0.00% 0.48134 0.00% 3.770 0.000 1.000 31.88 MB
PHP - refactor/remove-zend-signals (ZTS) 0.47901 0.48809 0.00245 0.51% 0.48091 -0.30% 0.48011 -0.25% 2.325 6.491 0.000 31.85 MB

@dunglas
dunglas force-pushed the refactor/remove-zend-signals branch from 6af2297 to 91c1b42 Compare November 23, 2026 10:35

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ext/opcache/zend_shared_alloc.c Resolved
Comment thread ext/pcntl/pcntl.c Resolved
Comment thread ext/pcntl/pcntl.c Resolved
Comment thread ext/pcntl/pcntl.c Resolved
Comment thread ext/opcache/zend_shared_alloc.c Resolved
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.
@dunglas
dunglas force-pushed the refactor/remove-zend-signals branch from 91c1b42 to ae7e1d3 Compare November 23, 2026 15:58
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.
@dunglas
dunglas force-pushed the refactor/remove-zend-signals branch from ae7e1d3 to d751a25 Compare November 23, 2026 16:02
@dunglas

dunglas commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@arnaud-lb thank you very much for your review. Indeed, it looks safe to remove the feature entirely.
I updated the PR description to explain the rationale, and the patch accordingly. I also implemented your other suggestions!

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me! I will merge if there are no objections.

@bukka

bukka commented Nov 23, 2026

Copy link
Copy Markdown
Member

@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.

dunglas added a commit to dunglas/php-src that referenced this pull request Nov 24, 2026
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).
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 24, 2026
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.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 24, 2026
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.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 24, 2026
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.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 25, 2026
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.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 25, 2026
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.
@dunglas

dunglas commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

@bukka thanks, good catch! I opened #25785 and #25741 to fix this issue and similar ones, that happen even today. At least #25785 should be merged before merging this one. A review would be appreciated :)

dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
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).
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
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.
dunglas added a commit to dunglas/php-src that referenced this pull request Nov 27, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants