Repository navigation
Conversation
…ript Worker SAPIs run one script that serves many requests, so RINIT and RSHUTDOWN don't run between them, and the SAPIs reset other extensions' globals by hand. php_request_end() and php_request_begin() run the output, SAPI and superglobal steps of php_request_shutdown() and php_request_startup() without ending the script, and call the new request_end_func and request_begin_func module hooks. php://input streams hold a reference to the request body, and freeing the body clears SG(request_info).request_body, so php_request_end() can release it.
…ame script The URL rewriter state drives the response output handler: without a reset, output_add_rewrite_var() never restarts the handler after the first request and its variables accumulate. Other state, such as strtok(), the stat cache or putenv() changes, is shared by the requests as in a long-running script.
…me script The session is written and closed and $_SESSION is removed, keeping the save handlers registered by the script. session.auto_start starts the session of the next request.
…ame script ext/filter keeps a copy of each input variable it parses, freed only by RSHUTDOWN. sapi_activate() drops these copies without freeing them, so a script serving several requests leaked them on each request.
php_rehash_environment() replaced the superglobals of the finished request once the output of the next one was active, so the destructors of the objects stored in them wrote to the next client's response, and data put back by those destructors leaked into the next request. php_request_begin() now releases them, and $_SESSION, before the output of the next request is active. The script still reads the superglobals of the finished request until then, as worker runtimes document.
Code running between two requests served by the same script, like the worker loop or a destructor, can set request state after php_request_end(). sapi_activate() dropped the header callback, headers, status line, mimetype and php://input body without releasing them, and the next request inherited the URL rewriter state and the session. php_request_begin() now releases the SAPI state before the output of the new request is active, and the standard request_begin hook resets the URL rewriter. session_start() and session_id() refuse to start or name a session when no request is active, as no client would get it. php_request_begin() sets SG(sapi_started) like php_request_startup().
A failing request_startup_func ends the process, while php_request_begin() and php_request_end() ignored failing hooks and the script kept serving requests with state its extensions couldn't set up or release. Both now return FAILURE when a hook fails or a step bails out, so the SAPI can end the script. zend_test gets request hooks that fail on demand.
A php://input stream kept across a request boundary read from the SAPI when the script used it during the next request, as sapi_activate() resets the read counters. It returned the next request's body and took those bytes from the stream the next request opens. The stream now reads from the SAPI only while its body is the body of the current request. A stream kept from a previous request returns what it already buffered, then EOF.
request_begin_func ran only between two requests of a worker script, so extensions needed the same setup in RINIT and RBEGIN, and could not rely on RBEGIN in SAPIs without a worker mode. php_execute_script() now calls the request_begin_func hooks before the userland code of the request runs, so linting, preloading and FPM status pages don't call them. The CLI, phpdbg and php_embed_init() call zend_request_begin_modules() before code they evaluate with zend_eval_string(), and dl() calls the hook of a module loaded after the request began. A failing hook keeps the script from running, with exit status 255 and HTTP response code 500. request_end_func runs once for each module that entered its request_begin_func, even if another module bailed out: from php_request_end() between two requests, or in php_request_shutdown() right before the same module's request_shutdown_func, so the session is still written at the place of the session module's shutdown. The session module doesn't retry a session.auto_start that RINIT already tried. php_request_begin() resets the URL rewriter state set between two requests itself, as the standard module's request_begin_func would now also run before the first userland code of every request.
dunglas
force-pushed
the
request-boundary-hooks
branch
from
November 27, 2026 21:16
34eaf32 to
b88d95f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RFC: https://wiki.php.net/rfc/request_lifecycle_hooks
Discussion: https://news-web.php.net/php.internals/133044