Late state. When the work pool resumes, all late runs execute at once, potentially causing API rate limiting or resource exhaustion.
This guide shows how to use a Prefect automation to automatically cancel flow runs when they become late.
Create a deployment
First, create a deployment for your flow. You can use any deployment method - this example usesflow.serve():
my_flow.py
Create the automation
Create a YAML file that defines an automation to cancel late runs:cancel-late-runs.yaml
How it works
- trigger.expect: Watches for
prefect.flow-run.Lateevents emitted when the server marks a flow run as late - trigger.match: Matches all flow runs (
prefect.flow-run.*) - actions: Cancels the flow run associated with the triggering event
Target specific deployments
To only cancel late runs for specific deployments, add amatch_related filter with the deployment name:
cancel-late-runs.yaml
my-deployment with your actual deployment name.
When do runs become late?By default, Prefect marks a flow run as late if it hasn’t started within 90 seconds of its scheduled time. The server checks for late runs every 10 seconds.You can configure these values with:
PREFECT_API_SERVICES_LATE_RUNS_AFTER_SECONDS: seconds before marking a run late (default: 90)PREFECT_API_SERVICES_LATE_RUNS_LOOP_SECONDS: how often to check (default: 10)
What happens when runs are cancelled
When a flow run is automatically cancelled by this automation:- The flow run’s state changes from
LatetoCancelled - If a worker picks up the run before cancellation, the worker receives a cancellation signal
- The flow run appears in the UI with a
Cancelledstatus - No further execution occurs for that run