Hi internals,
Following the recent pre-RFC discussion
(https://news-web.php.net/php.internals/132960), I would like to open the discussion on the "Request lifecycle hooks for SAPIs with a worker mode" RFC:
https://wiki.php.net/rfc/request_lifecycle_hooks
As presented, this RFC proposes two optional module hooks, available for extensions (no userland change), which are called between requests served by a "worker" script.
I crafted a patch adding the hooks and support PHP functions. It implements the hooks in the built-in standard, session and filter extensions: https://github.com/dunglas/php-src/tree/request-boundary-hooks
Here is also a patch showing how to use this new engine feature in FrankenPHP: https://github.com/php/frankenphp/compare/main...experiment/php-request-boundary-hooks
Best,
Kévin Dunglas
Hey Kevin,
thanks for working on this. It would be really nice, from an extension perspective, to have an explicit way to detect the logical request boundaries in an explicit way.
I had one question when looking at the FrankenPHP example at https://github.com/php/frankenphp/compare/main...experiment/php-request-boundary-hooks It seems that the lifecycle is actually starting with REND and finishing with RBEGIN? Is that intended, I can see why it is the way it is, it just feels odd.
So for example:
<?php
$handler = static function () {
echo "request handled";
};
while (frankenphp_handle_request($handler)) {}
?>
The lifecycle of this code would look like:
- RINIT (plus all the things before)
- FrankenPHP would execute till the call to frankenphp_handle_request()
- then REND (to "shutdown"/end the dummy bootstrap request)
- wait for an actual HTTP request and then we enter the RBEGIN -> REND loop as long as requests are served to that worker
- if that worker is about to shut down, frankenphp_handle_request() would return false, but also do another call to RBEGIN, just none to REND anymore before RSHUTDOWN
Kind regards
Florian
Quoted text · 10 lines
Hi internals,
Following the recent pre-RFC discussion
(https://news-web.php.net/php.internals/132960), I would like to open the discussion on the "Request lifecycle hooks for SAPIs with a worker mode" RFC:
https://wiki.php.net/rfc/request_lifecycle_hooksAs presented, this RFC proposes two optional module hooks, available for extensions (no userland change), which are called between requests served by a "worker" script.
I crafted a patch adding the hooks and support PHP functions. It implements the hooks in the built-in standard, session and filter extensions: https://github.com/dunglas/php-src/tree/request-boundary-hooks
Here is also a patch showing how to use this new engine feature in FrankenPHP: https://github.com/php/frankenphp/compare/main...experiment/php-request-boundary-hooks
Best,
Kévin Dunglas
Hi Florian,
Thanks for taking the time to read the RFC and review the patches.
Yes, it’s intended, and it’s already how FrankenPHP behaves today.
Because of how the engine works internally, the "dummy" request that boots the worker script and the "logical" requests handled in the loop don’t overlap: they run one after the other.
The full sequence in FrankenPHP is:
- RINIT ("dummy" request)
- userland bootstrap code
- main loop, frankenphp_handle_request() waits for a request (its first call first runs REND to clean the request-specific state tied to the "dummy" request):
- RBEGIN (logical HTTP request)
- callback
- REND (logical HTTP request)
- when the worker must stop, frankenphp_handle_request() calls RBEGIN and returns false: this recreates a "dummy" request, because the code after the loop can still write output, and php_request_shutdown() needs an active request
- userland shutdown code
- RSHUTDOWN ("dummy" request)
For extensions, the rules are the same as today: what is set up in RINIT or RBEGIN must be released in the next REND or RSHUTDOWN. A debugger or a profiler will likely keep running the same code as today in RSHUTDOWN, and an adapted version of it in the new REND. That’s by design: existing extensions keep working unchanged, and a logical request is now simply one that starts with RBEGIN and ends with REND. Best,
Kévin
Hey Kévin,
Thanks, could this be documented in the RFC, because from how I read it, it shows the RBEGIN -> REND pairs inside the loop. It does not show the initial RINIT -> REND or final RBEGIN -> RSHUTDOWN transitions.
That should be made explicit in the RFC, basically just the lifecycle you wrote about in your email. Another SAPI with worker mode could interpret the RFC as RINIT -> (RBEGIN -> REND) -> RSHUTDOWN. Otherwise, I like that we could have an explicit hook to detect requests handled inside a worker!
Kind regards
Florian
Hi Florian,
I updated the RFC (now 0.2) to include the lifecycle sequence.
I also addressed a relevant question asked on LinkedIn (https://www.linkedin.com/feed/update/urn:li:activity:7525919414685384704/?commentUrn=urn%3Ali%3Acomment%3A%28activity%3A7525919414685384704%2C7525924489574899712%29&dashCommentUrn=urn%3Ali%3Afsd_comment%3A%287525924489574899712%2Curn%3Ali%3Aactivity%3A7525919414685384704%29&skipRedirect=true) about failing hooks: php_request_begin() and php_request_end() now return FAILURE when a hook fails, and the SAPI should then end the script, as a failing RINIT ends the process.
The RFC now also clarifies what happens when request-dependent functions are called between two logical requests: the state they set is released before the next request starts, and session_start() and session_id() refuse to run there.
Both patches now match the RFC.
Best
Hi
Following the recent pre-RFC discussion
(https://news-web.php.net/php.internals/132960), I would like to open the discussion on the "Request lifecycle hooks for SAPIs with a worker mode" RFC:
https://wiki.php.net/rfc/request_lifecycle_hooks
Thank you for your RFC. I have a few remarks:
-
I want to point out this PR from Arnaud, it seems related to this RFC: https://github.com/php/php-src/pull/25610 (I also DM'd Arnaud for him to comment himself).
-
In the “Engine Functions” section:
Releasing the request body takes two small changes: php://input streams hold a reference to the body, and freeing the body, for instance when the script closes it, clears SG(request_info).request_body.
From this statement it is not clear to me what happens when a script holds an open php://input stream resource across a REND + RBEGIN boundary. Will the existing resource return EOF after that? Will it silently hold data from the next request? This should be spelled out more explicitly.
- I wonder if the initial “userland bootstrap” request should also get a RBEGIN call directly after RINIT. Is there a reason this is not done? This would also resolve the point that Florian mentioned. From what I understand, extensions would currently need to duplicate some of the set-up logic between RINIT and RBEGIN (or call their own RBEGIN from RINIT).
For session.auto_start in particular it would be quite nice to move that into RBEGIN only. If I remember correctly, php -l creates sessions if session.auto_start is enabled. This is at minimum surprising. Similarly for Tideways I need to jump through some hoops to exclude requests that don't actually execute code (including preloading in FPM) to prevent empty traces from being created for them. If I could switch to RBEGIN this would make things easier, I would hope.
- I don't think the RFC Impact and BC incompatible changes sections are fully accurate as written. If other extensions (including built-in extensions) make use of the new hooks and move code from RINIT to RBEGIN than extensions that remain RINIT-only might miss some information from the structures that they've previously relied on. Similarly if extensions rely on RBEGIN to work correctly, they will will break for SAPIs that don't call RBEGIN.
Best regards
Tim Düsterhus
Hey,
Quoted text · 5 lines
Hi
Following the recent pre-RFC discussion
(https://news-web.php.net/php.internals/132960), I would like to open the discussion on the "Request lifecycle hooks for SAPIs with a worker mode" RFC:
https://wiki.php.net/rfc/request_lifecycle_hooksThank you for your RFC. I have a few remarks:
I like the premise of the RFC as well!
Quoted text · 5 lines
- In the “Engine Functions” section:
Releasing the request body takes two small changes: php://input streams hold a reference to the body, and freeing the body, for instance when the script closes it, clears SG(request_info).request_body.
From this statement it is not clear to me what happens when a script holds an open
php://inputstream resource across a REND + RBEGIN boundary. Will the existing resource return EOF after that? Will it silently hold data from the next request? This should be spelled out more explicitly.
- I wonder if the initial “userland bootstrap” request should also get a RBEGIN call directly after RINIT. Is there a reason this is not done? This would also resolve the point that Florian mentioned. From what I understand, extensions would currently need to duplicate some of the set-up logic between RINIT and RBEGIN (or call their own RBEGIN from RINIT).
For
session.auto_startin particular it would be quite nice to move that intoRBEGINonly. If I remember correctly,php -lcreates sessions ifsession.auto_startis enabled. This is at minimum surprising. Similarly for Tideways I need to jump through some hoops to exclude requests that don't actually execute code (including preloading in FPM) to prevent empty traces from being created for them. If I could switch to RBEGIN this would make things easier, I would hope.
Is there actually the one model which encapsulates all needs? If you have RINIT+RBEGIN -> (REND+??)RBEGIN -> [REND -> RBEGIN] -> REND(+RBEGIN??) -> REND -> RSHUDTOWN, how am I going to know that the initial RBEGIN is handling an user request? What happens to state which is supposed to be request-specific, but at the same time needs to somehow persist from start to end of execution.
It's all a bit odd, and don't feel like there is an actual good solution for this without nesting.
And the Scope RFC would actually properly solve it. With the Scope RFC you also don't need a RINIT -> REND -> [RBEGIN -> REND] -> RBEGIN -> RSHUTDOWN, but it properly nests. All you need is "scope start, scope end" then. And we could have RINIT -> (main) Scope start -> [nested scope start, end interleaved possibly] -> (main) scope end -> RSHUTDOWN.
One further thing [I know it conflates a bit with scope RFC discussion] I'm concerned about, in the scope RFC is "when is it ready". For the main scope it's not an issue, but when a scope is created, it is have its parents contents. But we want to check the superglobals, whatever, for e.g. ext/session with auto_start to consume, or stuff like datadog or tideways which are interested in the request data as well. Also, not every scope (as proposed in the scope RFC) is something you'd necessarily consider a "request" (e.g. you might create a scope to isolate some code which modifies superglobals, output buffers, whatever, but without actually it being a new request).
So, if the scope RFC had a mechanism to initialize the scope, between creation, and processing, hooks like a "scope ready" could exist properly in that space.
Embracing scopes here also solves the 2. point with SG(request_info).request_body etc. which scopes would simply clearly backup / refcount.
- I don't think the RFC Impact and BC incompatible changes sections are fully accurate as written. If other extensions (including built-in extensions) make use of the new hooks and move code from RINIT to RBEGIN than extensions that remain RINIT-only might miss some information from the structures that they've previously relied on. Similarly if extensions rely on RBEGIN to work correctly, they will will break for SAPIs that don't call RBEGIN.
Yeah, I think if we implement this RFC as is, the result will be a bit chaotic in the gritty details; I only think proper nested scoping can solve this cleanly.
Best regards
Tim Düsterhus
To summarize: I think you (Kévin) should work with the scope RFC author to make sure that proper hooking into scope events exists rather than this proposal, which has, in my opinion, unsolvable issues. I don't think a flat model (one which considers the part before frankenphp_handle_request and after it two distinct parts of execution) will work out nicely. It would be a story of manually backing state up, trying to figure out whether a RBEGIN hook is now on the main level or actually a worker processing stuff etc.
Thanks,
Bob