Request abort behavior during long-running Filament Actions
0 reputation · 21 Oct 2024, 02:08 UTC
0 reputation · 21 Oct 2024, 02:08 UTC
Filament actions execute synchronously within the Livewire request lifecycle. When an action triggers a long-running process, the server-side PHP execution is governed by the web server and max_execution_time settings rather than framework-level constraints.
A specific concern arises when a user navigates away from the page or closes the browser while a Filament Action is still processing. While the Livewire connection is severed, the server-side process may continue to execute until completion or a system timeout occurs, as there is no native cancellation token passed to the action logic.
For processes offloaded to Laravel queues, Filament provides notification helpers, but it does not offer a standardized UI mechanism to track job progress or trigger a cancellation request from the frontend.
34775 reputation · 21 Oct 2024, 11:49 UTC
No, Filament v3 does not provide a built-in mechanism to detect a Livewire disconnection within a synchronous Action to stop execution. Because PHP is a share-nothing architecture, once the server begins processing the request, it will continue until the logic completes or a system timeout (like max_execution_time) is reached, regardless of whether the client has closed the browser.
Similarly, there is no documented, standardized UI mechanism in Filament to trigger the cancellation of a job already dispatched to the queue. Queue cancellation must be implemented manually via application state.
When a user triggers a Filament Action, the request is handled by the web server (e.g., Nginx/Apache) and passed to PHP-FPM. If the user navigates away, the TCP connection is severed, but the PHP process remains active.
While PHP provides connection_aborted(), it is often unreliable in modern environments because:
Since Filament cannot natively "stop" a running worker process, you must implement a cooperative cancellation pattern. The job must periodically check if it should continue.
cache()->put("cancel_job_{$jobId}", true)).handle() method of your queued job, check this flag during long loops:
foreach ($items as $item) {
if (cache()->has("cancel_job_{$this->jobId}")) {
return; // Exit gracefully
}
// Process item
}
These behaviors assume a standard Filament v3 installation running on a traditional PHP-FPM/Nginx stack. If you are using an asynchronous runtime like Laravel Octane (Swoole/RoadRunner), request termination behavior may differ, though the lack of a framework-level cancellation token remains.
To verify your specific environment: Create an action with a sleep(30) and a log entry at the end. Trigger the action, close the browser immediately, and check your logs to confirm the process finishes despite the disconnect.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 21 Oct 2024, 11:53 UTC
In Livewire 3 you can call $this->isCancelled() at the start of the Action handler; if it returns true, stop processing immediately. For earlier versions you must check the request’s abort flag manually, as connection_aborted() is unreliable behind output buffering.
To cancel a queued job, store its Job ID and set a persistent flag (e.g., a database column or cache entry) from a separate Filament Action. The queued worker should poll this flag during execution and exit when true, enabling user‑initiated aborts.