Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkerStopped event is not called for graceful shutdowns via SIGTERM #970

Closed
calvinalkan opened this issue Dec 4, 2024 · 3 comments
Closed

Comments

@calvinalkan
Copy link

Octane Version

2.5.9

Laravel Version

11.31.1

PHP Version

8.3.13

What server type are you using?

FrankenPHP

Server Version

1.3.1

Database Driver & Version

sqlite

Description

When the ocante:frankenphp command receives a SIGTERM signal, it gracefully stops the frankenphp server via the caddy admin API. That works fine.

However, the frankenphp-worker.php script does not receive this signal, and it will simply be terminated.

try {
    // omitting for brevity

    while ($requestCount < $maxRequests && frankenphp_handle_request($handleRequest)) {
        $requestCount++;
    }
} finally {
   // THIS PART IS NEVER CALLED.
    $worker?->terminate();
    
    gc_collect_cycles();
}

It appears that the worker hangs during the call to frankenphp_handle_request()

This is problematic, because $worker->terminate is responsible for firing the WorkerStopped event which is commonly used for clean-up tasks.

// default octane.php config
  WorkerStopping::class => [
            CloseMonologHandlers::class,
        ],
// sentry for laravel

    protected static $octaneEventHandlerMap = [
         // ommited for brevity
        Octane\WorkerStopping::class => 'octaneWorkerStopping',
    ];

I'm not sure if this is an issue in frankenphp_handle_request, or in the integration with octane. Maybe frankenphp_handle_request should not block when the caddy server has been stopped (@dunglas)?

Or perhaps the octane CLI command should somehow communicate to the worker script that no new requests will be incoming.

In practice, this means that using WorkerStopping is unreliable for any form of zero-downtime deployment with graceful shutdowns.

Steps To Reproduce

Add this line to frankenphp-worker.php

file_put_contents(dirname(__DIR__,4).'/storage/logs/worker.log', "finally\n", FILE_APPEND);

Start frankenphp via octane:frankenphp and then send a SIGTERM to the process.

The log message will not be printed.

@calvinalkan
Copy link
Author

I opened a bug report in frankenphp:

dunglas/frankenphp#1226

Copy link

github-actions bot commented Dec 9, 2024

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@calvinalkan
Copy link
Author

I'll close this as it's a bug in frankenphp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants