Skip to content

Admit NTS requests to OPcache SHM only in RINIT - #25509

Merged
arnaud-lb merged 2 commits into
php:PHP-8.4from
pavimus:fix-nts-rinit-admission
Nov 27, 2026
Merged

arnaud-lb merged 2 commits into
php:PHP-8.4from
pavimus:fix-nts-rinit-admission

Conversation

@pavimus

@pavimus pavimus commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Prepared with an AI assistant (Claude), which also ran the reproduction and the tests on my machine; I reviewed the result. The text below is AI-generated (quoted, as asked in CONTRIBUTING.md).

Follow-up of #24276, as suggested by @arnaud-lb there: apply the RINIT-only admission to NTS. Windows is not changed here.

Without ZTS a request takes the reader lock lazily, on its first use of SHM, and a restart only checks with F_GETLK that no process holds it. Between RINIT and that first use the request is invisible to a restart, but it already reads SHM: persistent_compile_file() looks the script up in the shared hash before accel_activate_add(). If another process restarts the cache meanwhile, the request then takes the lock and runs the script it found, from memory that now belongs to other scripts. A restart can also begin between its F_GETLK check and a reader taking the lock.

Reproduced deterministically with gdb (NTS debug build, php -S with PHP_CLI_SERVER_WORKERS=2, gdb attached to all workers): a request for a cached hold.php is held in persistent_compile_file() after the hash lookup, before the admission block; meanwhile opcache_reset() is called and b.php is requested. Without the change the restart runs in another worker, b.php is stored in the reset memory at the same address, and the held request then outputs b.php's output instead of hold.php's (3/3 runs). With the change the held request is a reader since RINIT: the restart waits for it, b.php runs uncached meanwhile, the held request prints hold.php's output, and the restart runs after it (3/3).

The change does what ZTS does since #24276:

  • a request becomes a reader only in RINIT (F_SETLKW F_RDLCK on byte 1, accel_reader_enter()), and stays one until accel_post_deactivate(); accel_activate_add() fails outside of Windows, so a request that was not admitted does not start to use SHM later;
  • a restart takes F_SETLK F_WRLCK on byte 1 for its duration (accel_restart_begin() / accel_restart_end(), formerly ZTS only), so it waits for the readers and requests wait in RINIT while it runs. If readers remain, accel_is_inactive() still applies force_restart_timeout.

ZTS behaves as before (the helpers are shared, accel_zts_lock_reader_byte() is renamed accel_lock_reader_byte()).

One visible difference: a process with opcache.enable_cli=1 holds the reader lock for the whole request even if it never compiles a file (e.g. php -r), as under ZTS.

Tests: ext/opcache and Zend/tests on NTS and ZTS debug builds, 0 failures; gh8739.phpt 10/10 on both. Load with OPcache restarts (Apache prefork with 64 NTS mod_php processes, wrk, OOM restarts or opcache_reset() every few seconds): 10 runs, 52 restarts, no crashes or failed requests; throughput within noise of the unpatched build.

The change merges into PHP-8.5 and PHP-8.6 cleanly; on master it conflicts with the C11 atomics and the restart hook, I can open a version for master if needed. The unification you suggested for master (removing the lazy locks) could follow.

🤖 Generated with Claude Code

Without ZTS a request took the reader lock lazily, on its first use of SHM,
and a restart only checked that no process held it (F_GETLK). A request is
therefore invisible to a restart between RINIT and its first compilation,
although persistent_compile_file() already looks the script up in the
shared hash before taking the lock: another process can clean SHM
meanwhile, and the request then uses the script it found. A restart could
also start between its F_GETLK check and the lock taken by a reader.

Do as under ZTS since phpGH-8739: a request becomes a reader only in RINIT and
stays one until accel_post_deactivate(), and a restart takes the write lock
on the reader byte for its duration, so readers wait for it in RINIT.
* PHP-8.4: (91 commits)
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  [skip ci] Use USE_TRACKED_ALLOC=1 for Symfony Preloading test
  [skip ci] Fix Symfony test without USE_TRACKED_ALLOC
  [skip ci] Skip assign_dim_undef_op_data_008.phpt when JIT is unavailable
  ...
@arnaud-lb
arnaud-lb merged commit 3f6f0d4 into php:PHP-8.4 Nov 27, 2026
1 check passed
arnaud-lb added a commit that referenced this pull request Nov 27, 2026
* PHP-8.6:
  Admit NTS requests to OPcache SHM only in RINIT (#25509)
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4:
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 27, 2026
* PHP-8.5:
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
arnaud-lb added a commit to lazerg/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (262 commits)
  ext/pdo_sqlite: crash when a collation callback closes the statement cursor
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
  ext/pgsql: pg_meta_data() numeric column names not integer keys with opcache
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  ...
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.

2 participants