Skip to content

[RFC] Request lifecycle hooks for worker SAPIs - #25443

Draft
dunglas wants to merge 9 commits into
php:masterfrom
dunglas:request-boundary-hooks
Draft

dunglas wants to merge 9 commits into
php:masterfrom
dunglas:request-boundary-hooks

Conversation

@dunglas

@dunglas dunglas commented Nov 13, 2026

Copy link
Copy Markdown
Member

…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.
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.

1 participant