Skip to content

Shutdown already started modules when a later module init fails - #25157

Open
dunglas wants to merge 3 commits into
php:PHP-8.4from
dunglas:rinit-bailout-rshutdown
Open

dunglas wants to merge 3 commits into
php:PHP-8.4from
dunglas:rinit-bailout-rshutdown

Conversation

@dunglas

@dunglas dunglas commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

This prevents issues such as session files never closed with session.auto_start=1.

When a RINIT bails out, php_request_shutdown() skips every RSHUTDOWN, including those of the modules whose RINIT already ran. Their request state leaks into the next request. For instance, the session stays locked and every later request for it blocks in persistent SAPIs (mod_php, FrankenPHP, the CLI server).

zend_activate_modules() now records how far it got in a thread-local pointer. zend_deactivate_modules() then runs RSHUTDOWN at its usual step, after the output buffers are flushed, for every module except the one whose RINIT bailed out and the modules after it that have a RINIT. Modules without RINIT get their RSHUTDOWN as usual. There is no new API: zend_deactivate_modules() does nothing when no RINIT ran in the request. The cost on the normal path is one thread-local write per module with a RINIT.

Tests:

  • ext/session/tests/rinit_bailout_releases_lock.phpt: the session lock is released.
  • ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt (suggested by devnexen): same with session.use_trans_sid, whose output handler runs during the flush.
  • ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt: an output handler calling putenv() during the flush doesn't leak the variable to the next request.

Open question: exit instead?

A RINIT returning FAILURE already calls exit(1), and a bailout in RSHUTDOWN calls _exit(1) ("module shutdown is incomplete"). A RINIT bailout is the only case where the process keeps serving. The alternative to this patch is to exit there too:

  • For: it is consistent with the two other cases, and it also covers the module that bailed out, whose partial RINIT state this patch can't clean up (its RSHUTDOWN is skipped because it could free fields that were never initialized). It's a three-line change.
  • Against: in threaded SAPIs (mod_php with the worker or event MPM, Apache on Windows, FrankenPHP), every request in flight on other threads dies. A transient condition, such as a session backend being down with session.auto_start=1, becomes a crash loop instead of failed requests.

I went with recovery, but I'm happy to switch to exiting if you prefer.

Patch written with the assistance of Claude.

php_request_shutdown() only calls RSHUTDOWN once every RINIT returned,
so the modules activated before a failing one never released their
request state. With session.auto_start, the session stayed locked and
every later request for it blocked forever in persistent SAPIs.
Comment thread Zend/zend_API.c Resolved
Comment thread Zend/zend_API.c Resolved
dunglas added a commit to php/frankenphp that referenced this pull request Nov 20, 2026
After a RINIT bailout, php_request_shutdown() skips every RSHUTDOWN.
Since php/php-src@6b197ee4edf, mbstring's next RINIT frees the stale
detect order list, which corrupts the heap. The Homebrew debug build
used on macOS CI aborts with "zend_mm_heap corrupted (double free)".

Fixed upstream by php/php-src#25157.
@devnexen

Copy link
Copy Markdown
Member

can the following test be added ?

--TEST--
A request whose startup bails out after session.auto_start with session.use_trans_sid
--EXTENSIONS--
session
zend_test
--INI--
default_socket_timeout=5
--FILE--
<?php
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";

$args = [
    '-d', 'extension_dir=' . ini_get('extension_dir'),
    '-d', 'session.auto_start=1',
    '-d', 'session.use_trans_sid=1',
    '-d', 'session.use_cookies=0',
    '-d', 'session.use_only_cookies=0',
    '-d', 'session.save_path=' . __DIR__,
    '-d', 'zend_test.bailout_in_rinit=1',
];
$builtin = shell_exec(escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY) . ' -n -m');
foreach (['session', 'zend_test'] as $ext) {
    if (!preg_match("/^$ext$/m", $builtin)) {
        array_push($args, '-d', "extension=$ext");
    }
}

php_cli_server_start('echo session_id();', null, $args);

$url = 'http://' . PHP_CLI_SERVER_ADDRESS . '/?PHPSESSID=rinitbailouttranssid';
var_dump(@file_get_contents($url));
var_dump(@file_get_contents($url));
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/sess_rinitbailouttranssid');
?>
--EXPECT--
string(0) ""
string(20) "rinitbailouttranssid"

Calling RSHUTDOWN from zend_activate_modules() ran it before the output
buffers were flushed, so output handlers started by earlier RINITs ran
after their module had shut down: a handler calling putenv() leaked the
variable to the next request, and the session.use_trans_sid test failed.
It also skipped the modules without RINIT.

zend_activate_modules() now records how far it got, and
zend_deactivate_modules() runs RSHUTDOWN at its usual step for every
module except the one that bailed out and those whose RINIT never ran.
@dunglas
dunglas requested a review from bukka as a code owner November 21, 2026 07:47
dunglas added a commit to php/frankenphp that referenced this pull request Nov 21, 2026
Skips part of `TestRegisterExtension` on PHP 8.6 until
php/php-src#25157 is released.
@dunglas

dunglas commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

@devnexen done

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