From 1367bbb6eac00ec77b0102ae6c2f3e2b9657af58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 2 Nov 2026 11:00:05 +0100 Subject: [PATCH 1/3] Run RSHUTDOWN for activated modules when a later RINIT bails out 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. --- Zend/zend_API.c | 31 ++++++--- .../tests/rinit_bailout_releases_lock.phpt | 63 +++++++++++++++++++ ext/zend_test/php_test.h | 1 + ext/zend_test/test.c | 7 +++ 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 ext/session/tests/rinit_bailout_releases_lock.phpt diff --git a/Zend/zend_API.c b/Zend/zend_API.c index da7e06040d2f..a697276c143e 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3410,17 +3410,32 @@ void module_registry_unload(const zend_module_entry *module) ZEND_API void zend_activate_modules(void) /* {{{ */ { - zend_module_entry **p = module_request_startup_handlers; + zend_module_entry ** volatile p = module_request_startup_handlers; - while (*p) { - zend_module_entry *module = *p; + zend_try { + while (*p) { + zend_module_entry *module = *p; - if (module->request_startup_func(module->type, module->module_number)==FAILURE) { - zend_error(E_WARNING, "request_startup() for %s module failed", module->name); - exit(1); + if (module->request_startup_func(module->type, module->module_number)==FAILURE) { + zend_error(E_WARNING, "request_startup() for %s module failed", module->name); + exit(1); + } + p++; } - p++; - } + } zend_catch { + /* The request shutdown skips RSHUTDOWN unless every RINIT returned, + * release what the modules activated so far hold (e.g. session locks) */ + while (p > module_request_startup_handlers) { + zend_module_entry *module = *--p; + + if (module->request_shutdown_func) { + zend_try { + module->request_shutdown_func(module->type, module->module_number); + } zend_end_try(); + } + } + zend_bailout(); + } zend_end_try(); } /* }}} */ diff --git a/ext/session/tests/rinit_bailout_releases_lock.phpt b/ext/session/tests/rinit_bailout_releases_lock.phpt new file mode 100644 index 000000000000..7f6ba5cd029e --- /dev/null +++ b/ext/session/tests/rinit_bailout_releases_lock.phpt @@ -0,0 +1,63 @@ +--TEST-- +A request whose startup bails out after session.auto_start releases the session lock +--EXTENSIONS-- +session +zend_test +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECT-- +[] +[rinitbailout] diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index 9f06d960c4ff..a2d7ff708125 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -61,6 +61,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) bool rearm_vm_interrupt; bool print_stderr_mshutdown; bool warn_on_startup; + zend_long bailout_in_rinit; zend_long limit_copy_file_range; int observe_opline_in_zendmm; zend_mm_heap* zend_orig_heap; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 2d106337c5cb..51ca9b9603db 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -1407,6 +1407,7 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.rearm_vm_interrupt", "0", PHP_INI_SYSTEM, OnUpdateBool, rearm_vm_interrupt, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_ENTRY("zend_test.bailout_in_rinit", "0", PHP_INI_SYSTEM, OnUpdateLong, bailout_in_rinit, zend_zend_test_globals, zend_test_globals) #ifdef HAVE_COPY_FILE_RANGE STD_PHP_INI_ENTRY("zend_test.limit_copy_file_range", "-1", PHP_INI_ALL, OnUpdateLong, limit_copy_file_range, zend_zend_test_globals, zend_test_globals) #endif @@ -1659,6 +1660,12 @@ PHP_MSHUTDOWN_FUNCTION(zend_test) PHP_RINIT_FUNCTION(zend_test) { + /* number of requests whose startup fails */ + if (ZT_G(bailout_in_rinit) > 0) { + ZT_G(bailout_in_rinit)--; + zend_bailout(); + } + ALLOC_HASHTABLE(ZT_G(global_weakmap)); zend_hash_init(ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0); ZT_G(observer_nesting_depth) = 0; From 9bf387ef8cef1be95ebdef563b3df2eafd8c6475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 21 Nov 2026 08:16:38 +0100 Subject: [PATCH 2/3] Test a RINIT bailout with session.use_trans_sid --- ...rinit_bailout_releases_lock_trans_sid.phpt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt diff --git a/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt b/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt new file mode 100644 index 000000000000..206d22796f46 --- /dev/null +++ b/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt @@ -0,0 +1,47 @@ +--TEST-- +A request whose startup bails out after session.auto_start with session.use_trans_sid +--EXTENSIONS-- +session +zend_test +--SKIPIF-- + +--INI-- +default_socket_timeout=5 +--FILE-- + +--CLEAN-- + +--EXPECT-- +string(0) "" +string(20) "rinitbailouttranssid" From 2f1a0f5a3b65d7a1a0b8f979735a9b5c74d1c1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 21 Nov 2026 08:47:40 +0100 Subject: [PATCH 3/3] Defer RSHUTDOWN after a RINIT bailout 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. --- Zend/zend_API.c | 71 +++++++++++++------ ext/zend_test/php_test.h | 1 + ext/zend_test/test.c | 21 ++++++ .../rinit_bailout_rshutdown_after_output.phpt | 47 ++++++++++++ main/main.c | 4 +- 5 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt diff --git a/Zend/zend_API.c b/Zend/zend_API.c index a697276c143e..84255dee5339 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -47,6 +47,10 @@ static zend_module_entry **modules_dl_loaded; static zend_class_entry **class_cleanup_handlers; +/* Where zend_activate_modules() stopped: NULL if it did not run, the + * terminating NULL if every RINIT returned, else the RINIT that bailed out */ +ZEND_TLS zend_module_entry **rinit_stop = NULL; + ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */ { zval *param_ptr; @@ -3410,40 +3414,61 @@ void module_registry_unload(const zend_module_entry *module) ZEND_API void zend_activate_modules(void) /* {{{ */ { - zend_module_entry ** volatile p = module_request_startup_handlers; + zend_module_entry **p = module_request_startup_handlers; - zend_try { - while (*p) { - zend_module_entry *module = *p; + while (*p) { + zend_module_entry *module = *p; - if (module->request_startup_func(module->type, module->module_number)==FAILURE) { - zend_error(E_WARNING, "request_startup() for %s module failed", module->name); - exit(1); - } - p++; + /* still points to this module if its RINIT bails out */ + rinit_stop = p; + if (module->request_startup_func(module->type, module->module_number)==FAILURE) { + zend_error(E_WARNING, "request_startup() for %s module failed", module->name); + exit(1); } - } zend_catch { - /* The request shutdown skips RSHUTDOWN unless every RINIT returned, - * release what the modules activated so far hold (e.g. session locks) */ - while (p > module_request_startup_handlers) { - zend_module_entry *module = *--p; + p++; + } - if (module->request_shutdown_func) { - zend_try { - module->request_shutdown_func(module->type, module->module_number); - } zend_end_try(); - } - } - zend_bailout(); - } zend_end_try(); + rinit_stop = p; } /* }}} */ +/* Skips the modules whose RINIT never ran or bailed out */ +static void zend_deactivate_modules_after_rinit_bailout(const zend_module_entry *bailed_out) +{ + zend_module_entry *module; + bool rinit_ran = false; + + ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { + if (module == bailed_out) { + rinit_ran = true; + continue; + } + if (!module->request_shutdown_func || (!rinit_ran && module->request_startup_func)) { + continue; + } + zend_try { + module->request_shutdown_func(module->type, module->module_number); + } zend_catch { + /* module shutdown is incomplete */ + _exit(1); + } zend_end_try(); + } ZEND_HASH_FOREACH_END(); +} + ZEND_API void zend_deactivate_modules(void) /* {{{ */ { + zend_module_entry **stop = rinit_stop; + + if (!stop) { + return; + } + rinit_stop = NULL; + EG(current_execute_data) = NULL; /* we're no longer executing anything */ - if (EG(full_tables_cleanup)) { + if (*stop) { + zend_deactivate_modules_after_rinit_bailout(*stop); + } else if (EG(full_tables_cleanup)) { zend_module_entry *module; ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index a2d7ff708125..78f511d38584 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -62,6 +62,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) bool print_stderr_mshutdown; bool warn_on_startup; zend_long bailout_in_rinit; + zend_string *rinit_bailout_putenv; zend_long limit_copy_file_range; int observe_opline_in_zendmm; zend_mm_heap* zend_orig_heap; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 51ca9b9603db..5c0dbac00336 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -1408,6 +1408,7 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("zend_test.rearm_vm_interrupt", "0", PHP_INI_SYSTEM, OnUpdateBool, rearm_vm_interrupt, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_ENTRY("zend_test.bailout_in_rinit", "0", PHP_INI_SYSTEM, OnUpdateLong, bailout_in_rinit, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_ENTRY("zend_test.rinit_bailout_putenv", "", PHP_INI_SYSTEM, OnUpdateStr, rinit_bailout_putenv, zend_zend_test_globals, zend_test_globals) #ifdef HAVE_COPY_FILE_RANGE STD_PHP_INI_ENTRY("zend_test.limit_copy_file_range", "-1", PHP_INI_ALL, OnUpdateLong, limit_copy_file_range, zend_zend_test_globals, zend_test_globals) #endif @@ -1658,11 +1659,31 @@ PHP_MSHUTDOWN_FUNCTION(zend_test) return SUCCESS; } +/* Called by the output flush of the request shutdown, before any RSHUTDOWN */ +static zend_result zend_test_rinit_bailout_putenv_handler(void **handler_context, php_output_context *output_context) +{ + zval func, arg, retval; + + ZVAL_STRINGL(&func, "putenv", sizeof("putenv") - 1); + ZVAL_STR_COPY(&arg, ZT_G(rinit_bailout_putenv)); + call_user_function(NULL, NULL, &func, &retval, 1, &arg); + zval_ptr_dtor(&retval); + zval_ptr_dtor(&arg); + zval_ptr_dtor(&func); + + /* pass the output through */ + return FAILURE; +} + PHP_RINIT_FUNCTION(zend_test) { /* number of requests whose startup fails */ if (ZT_G(bailout_in_rinit) > 0) { ZT_G(bailout_in_rinit)--; + if (ZT_G(rinit_bailout_putenv) && ZSTR_LEN(ZT_G(rinit_bailout_putenv))) { + php_output_handler_start(php_output_handler_create_internal( + ZEND_STRL("zend_test_rinit_bailout_putenv"), zend_test_rinit_bailout_putenv_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS)); + } zend_bailout(); } diff --git a/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt b/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt new file mode 100644 index 000000000000..2c4f0e7a5ccd --- /dev/null +++ b/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt @@ -0,0 +1,47 @@ +--TEST-- +After a RINIT bailout, RSHUTDOWN runs after the output handlers are flushed +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +[] +[bool(false) +] diff --git a/main/main.c b/main/main.c index 4d1612a09c78..c7c7c336c361 100644 --- a/main/main.c +++ b/main/main.c @@ -1956,9 +1956,7 @@ void php_request_shutdown(void *dummy) } zend_end_try(); /* 5. Call all extensions RSHUTDOWN functions */ - if (PG(modules_activated)) { - zend_deactivate_modules(); - } + zend_deactivate_modules(); /* 6. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */ zend_try {