Skip to content

fix: system ID for Apache graceful restart - #24184

Merged
morrisonlevi merged 1 commit into
php:PHP-8.4from
morrisonlevi:system-id-reinitialization
Oct 9, 2026
Merged

morrisonlevi merged 1 commit into
php:PHP-8.4from
morrisonlevi:system-id-reinitialization

Conversation

@morrisonlevi

@morrisonlevi morrisonlevi commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Apache forks workers from a parent process. During a graceful restart, PHP shuts down and starts up again in that same parent process. New workers are then forked from the parent.

However, finalized was not reset to 0 during subsequent startups, so all calls to zend_add_system_entropy() would fail. This could cause the process state to diverge from the system ID used to validate OPcache's file cache.

For example, zend_get_resource_handle() would still allocate a handle, but its entropy would not contribute to the system ID. Enabling, disabling, or reordering extensions or features across graceful restarts could therefore change resource-slot assignments without a corresponding change to the system ID.

Reset finalized when initializing the system ID so each startup can register fresh entropy.


I think this fix should definitely go into PHP 8.4+ but I think that RMs should consider pulling it for 8.2-8.3. It is not a dangerous change, it's simple, and easy to understand (well, assuming you already understand Apache graceful and system ID), and causes really nasty memory bugs when it's hit.

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

Technically this looks right. You should contact the RMs if you think this should be backported.

Apache forks workers from a parent process. During a graceful
restart, PHP shuts down and starts up again in that same parent
process.

However, `finalized` was not reset to 0 during subsequent startups,
so all calls to `zend_add_system_entropy()` would fail. This could
cause the process state to diverge from the system ID used to
validate OPcache's file cache.

For example, `zend_get_resource_handle()` would still allocate a
handle, but its entropy would not contribute to the system ID.
Enabling, disabling, or reordering extensions or features across
graceful restarts could therefore change resource-slot assignments
without a corresponding change to the system ID.

Reset `finalized` when initializing the system ID so each startup
can register fresh entropy.
@morrisonlevi
morrisonlevi force-pushed the system-id-reinitialization branch from 91f888f to 6599499 Compare October 9, 2026 14:40
@morrisonlevi
morrisonlevi merged commit 695f826 into php:PHP-8.4 Oct 9, 2026
18 checks passed
@ndossche

ndossche commented Oct 9, 2026

Copy link
Copy Markdown
Member

@morrisonlevi this broke the build in some branches due to changes to INI functions. Please make a look.

arnaud-lb added a commit to nicolas-grekas/php-src that referenced this pull request Oct 12, 2026
* PHP-8.4: (33 commits)
  Update NEWS
  ext/mbstring: Fix truncated encoding names resolving to full names (php#23974)
  Backport PCRE2 security fixes to bundled 10.40
  Fix backport
  ext/pdo: Avoid a duplicate warning for a rejected statement class
  phpdbg: Fix wrong command names
  Backport phpGH-24241 to 8.4
  [ci skip] Update EXTENSIONS and CODEOWNERS for ext/readline
  Fix throwing behaviour with recursive arrays in VM
  Fix phpGH-24229: Use SHUT_WR when closing FastCGI connections (php#24230)
  Add regression test for phpGH-16821 (php#24228)
  Fix use-after-free when removing a failing zlib.inflate filter (php#24176)
  Fix phpGH-24218: Invalid opcode for count() of a literal array without SCCP
  fix: system ID for Apache graceful restart (php#24184)
  ext/standard: Validate the bcrypt cost before reading it
  zend_alloc: move a small block shrunk to the size of the bin below
  Fix phpGH-23979: Nullsafe operator must not flush delayed oplines of an enclosing function
  ext/zip: Reject ZipArchive mutators during close() (php#24025)
  Fix OSS-Fuzz #568005340: FETCH_DIM_FUNC_ARG partial conversion
  Fix too wide type inference for ASSIGN_DIM_OP
  ...
lyrixx added a commit to lyrixx/php-wasm-builder that referenced this pull request Nov 4, 2026
…ater

Each run starts PHP and stops it: what opcache compiled was gone with it,
and opcache could not start anyway, WASM has no shared memory. With
OPCACHE, opcache() for a stack, it keeps what it compiles in /opcache, in
the WASM filesystem (file_cache_only), that the next runs read:

- runtime: the first run that needs a file compiles it;
- precompiled: the warm stage compiles every PHP file of the embedded
  directory, and the cache is embedded. The system_id that keys the cache
  does not depend on the binary: the build for node compiles for the one
  for the web.

Two problems of PHP, worked around in run.c:

- The first start of the engine gets another system_id than the next ones
  (zend_system_id.c never resets "finalized"): every run takes the one of
  the first. PHP fixes it after 8.5.12 (php/php-src#24184).
- Without shared memory, opcache leaves smm_shared_globals pointing to a
  variable of a function that returned, that a later run with the file
  cache reads: it corrupted the memory (php/php-src#25195). It is reset
  before each run. opcache is disabled too, unless it has its files.

What is embedded is dated 1 s after the epoch: opcache caches it from the
first run, and does not cache a file written less than 2 s before, that
could be written over in the same second.
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.

3 participants