Short answer: not immediately. If the container running your flow/worker is killed, Prefect can’t set a terminal state right away because the engine never got a chance to report one. The run will stay “Running” until something detects the missing heartbeats and marks it “Crashed”. It will not become “Cancelled” unless you or an automation explicitly cancel it.
Why you’re seeing zombies
- When a flow is running, it emits heartbeats back to the API. If the container dies, heartbeats stop.
- Without a crash-detection automation, the API won’t automatically change the state and the run can sit in “Running”.
- In Prefect 3.x, the typical pattern is to use an Automation that marks runs as “Crashed” when heartbeats are missing for some window.
How to prevent this going forward (Prefect 3.x)
- Add a “zombie detection” automation in your local server:
- Trigger: flow run heartbeat missing for X seconds/minutes (e.g., ~90s).
- Condition: state is Running.
- Action: Change flow run state → Crashed.
- Heartbeats are emitted roughly every ~30s by default, so a 90–180s window is common. You can tune the threshold to your environment.
Docs
- Detect zombie flows:
Detect zombie flows
- States overview:
Flow and task states
Clean up existing zombies
- From the CLI:
- List running flow runs
prefect flow-run ls --state Running
- Cancel one by id (moves to Cancelling/Cancelled)
prefect flow-run cancel <flow_run_id>
- Delete one by id (removes from history)
prefect flow-run delete <flow_run_id>
A couple of notes
- “Cancelled” requires an explicit cancel action (you or an automation). A hard container kill won’t auto-cancel.
- If you add a flow-level timeout, it will cancel long-running flows while the engine is alive. It won’t help if the infra is killed, but it can prevent other stuck cases.
If you’d like, I can share step-by-step UI instructions to add the “heartbeat missing → Crashed” automation in your local server. Also, which Prefect version are you on and which worker type are you running in the container (e.g., Process worker)?