[php-src] PR #25509: Admit NTS requests to OPcache SHM only in RINIT
| From: | pavimus | Date: | Tue, 17 Nov 2026 06:28:45 +0000 |
| Subject: | [php-src] PR #25509: Admit NTS requests to OPcache SHM only in RINIT | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40713@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25509
Author: pavimus
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; Ðn´+ÅáŒÉ…*ÛH
> ‡Ž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](https://claude.com/claude-code)