frankenphp icon indicating copy to clipboard operation
frankenphp copied to clipboard

How are shutdown functions called?

Open withinboredom opened this issue 3 years ago • 1 comments

Consider the following code:

register_shutdown_function(function() { error_log('outside'); });

do {
    $running = frankenphp_handle_request(function (): void {
        register_shutdown_function(function() { error_log('in request'); });
    });
} while ($running);

Nothing is output in the logs that I can see (tested SIGTERM, SIGINT to stop the server):

  1. Are shutdown functions called per request/worker/ever? (I could probably do a db insert or something to see for sure.)
  2. Should shutdown functions be contextual on workers (such as in the example)?

A number of frameworks/libraries use shutdown functions to trigger things after a request (I've abused them to process queues, I think WordPress uses them to process cron jobs, error reporting, etc). It'd also be useful to do things when the worker itself dies (outside the loop) though, but in most cases shut down functions are registered with an expectation of request ending.

withinboredom avatar Oct 23 '22 11:10 withinboredom

Currently, they should be called only when the worker stops. IMHO this is expected. It's possible to do something after the request like that: https://github.com/php-runtime/runtime/blob/main/src/frankenphp-symfony/src/Runner.php#L41

Instead of abusing register_shutdown_function(), I think that we recommend users to use something like PHP Runtimes. Maybe could we also implement what you proposed in #62 to ease the migration?

Do you have an opinion on this @Nyholm?

dunglas avatar Oct 23 '22 18:10 dunglas