Repository navigation
Conversation
arnaud-lb
approved these changes
Nov 27, 2026
arnaud-lb
left a comment
Member
There was a problem hiding this comment.
Looks good to me except for one comment, but this is worth a second review.
I found additional cases, for a separate PR:
zend_accel_hash_update()changesentry->dataandentry->indirectseparately, so a kill may leave only one of them updated. Maybe we can turn that into a single write by tagging theentry->datapointer and removing theindirectfield.- Function JIT,
zend_jit_collect_calls()temporarily stores process-local data intozend_func_info. A kill before cleanup would leave that visible to other requests. zend_jit_stop_counter_handlers()setsZCSG(jit_counters_stopped)before completing- accel_restart_is_active() clears
restart_in_progresswhen the lock is free, so an NTS request that passed RINIT and didn't compile anything yet may access the half-reset cache (will be fixed by GH-25509).
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.
dunglas
force-pushed
the
fix/opcache-kill-safety
branch
from
November 27, 2026 13:37
b0ea33c to
652f848
Compare
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and return info of every function the trace goes through in the func_info of the function in shared memory, from process-local arena memory, and clears them at the end. If the process was killed in between, by SIGKILL or the OOM killer, these func_infos kept pointers into the arena of the dead process. The next trace entering one of these functions took its SSA as already built and used it, and other compilations read its stale return info, until the server was restarted. Record each func_info in a list in shared memory before writing to it, and reset the listed ones at the start of the next compilation. Same problem as the function JIT one reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
zend_accel_hash_update() turns the direct entry of a key into an indirect one when the key now resolves to another file, for example after a file was replaced with a symlink. It wrote entry->data and entry->indirect separately. If the process was killed in between, by SIGKILL or the OOM killer, the entry kept indirect set to 0 with data pointing to another hash entry, and every later lookup of the key read that entry as a persistent script and crashed. Lock-free readers could also see the two fields out of sync while the update was in progress. Tag the low bit of data instead of storing a separate flag, so the update is a single store, and load the tagged pointer once in readers. Reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
When the JIT runs out of root traces, zend_jit_stop_counter_handlers() restores the original VM handlers of every cached script. It set jit_counters_stopped before doing so. If the process was killed in the loop, by SIGKILL or the OOM killer, later calls returned early and the remaining counting handlers stayed until the next restart: each hot loop or function still entered the tracer once before being blacklisted, and the cold ones kept paying for the counters. Set the flag after the loop, which can safely run again. Reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
With opcache.jit triggers that compile functions at run time (first execution, profiling or hot counters), zend_jit_collect_calls() builds the call graph of the function in its func_info in shared memory, from process-local arena memory, and zend_jit_cleanup_func_info() clears it once the function is compiled. If the process was killed in between, by SIGKILL or the OOM killer, the func_info kept pointers into the arena of the dead process. Compiling a caller of that function then followed them from zend_jit_cleanup_func_info() and crashed, and type inference read the stale return info of the callee. Reset the func_info of the function before building its call graph, and those of its direct callees right after, as the compilation reads no other. Reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and return info of every function the trace goes through in the func_info of the function in shared memory, from process-local arena memory, and clears them at the end. If the process was killed in between, by SIGKILL or the OOM killer, these func_infos kept pointers into the arena of the dead process. The next trace entering one of these functions took its SSA as already built and used it, and other compilations read its stale return info, until the server was restarted. Record each func_info in a list in shared memory before writing to it, and reset the listed ones at the start of the next compilation. Same problem as the function JIT one reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
When the JIT runs out of root traces, zend_jit_stop_counter_handlers() restores the original VM handlers of every cached script. It set jit_counters_stopped before doing so. If the process was killed in the loop, by SIGKILL or the OOM killer, later calls returned early and the remaining counting handlers stayed until the next restart: each hot loop or function still entered the tracer once before being blacklisted, and the cold ones kept paying for the counters. Set the flag after the loop, which can safely run again. Reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
With opcache.jit triggers that compile functions at run time (first execution, profiling or hot counters), zend_jit_collect_calls() builds the call graph of the function in its func_info in shared memory, from process-local arena memory, and zend_jit_cleanup_func_info() clears it once the function is compiled. If the process was killed in between, by SIGKILL or the OOM killer, the func_info kept pointers into the arena of the dead process. Compiling a caller of that function then followed them from zend_jit_cleanup_func_info() and crashed, and type inference read the stale return info of the callee. Reset the func_info of the function before building its call graph, and those of its direct callees right after, as the compilation reads no other. Reported by Arnaud Le Blanc in php#25785.
dunglas
added a commit
to dunglas/php-src
that referenced
this pull request
Nov 27, 2026
While compiling a trace, the tracing JIT builds the SSA, call graph and return info of every function the trace goes through in the func_info of the function in shared memory, from process-local arena memory, and clears them at the end. If the process was killed in between, by SIGKILL or the OOM killer, these func_infos kept pointers into the arena of the dead process. The next trace entering one of these functions took its SSA as already built and used it, and other compilations read its stale return info, until the server was restarted. Record each func_info in a list in shared memory before writing to it, and reset the listed ones at the start of the next compilation. Same problem as the function JIT one reported by Arnaud Le Blanc in php#25785.
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.
While I was working on the removal of Zend Signals (#25627), @bukka found an atomicity problem with interned strings (#25627). This PR applies his suggested fix and fixes similar issues I found while looking for other cases.
These bugs already happen today with SIGKILL and the OOM killer, and with SIGTERM on builds without Zend Signals (default in ZTS Docker images, ZTS HomebrewPHP, FrankenPHP static binaries etc).
That's why I opened this as a bug fix against PHP-8.4.