Skip to content

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

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

dunglas wants to merge 7 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.
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