Giacomo Chiarella
03/24/2025, 8:36 AMflow_runs = await client.read_flow_runs(flow_run_filter=FlowRunFilter(id={"any_": [flow_run_id]}), limit=1)
state_updates = {}
state_updates.setdefault("name", "Cancelled")
state_updates.setdefault("type", StateType.CANCELLED)
state = flow_runs[0].state.copy(update=state_updates)
await client.set_flow_run_state(flow_run_id=flow_run_id, state=state, force=True)
I’ve noticed although the flow run state is set correctly, the task runs are not cancelled, they are actually still executed. I’ve read from doc that cancelling the flow run like that actually forces the state to be set in Prefect database but it does not stop the tasks. Here where I’ve got that info.
The agent type is Process. I’m on Prefect 3.2.7.
How to cancel a flow run execution and all its task runs? Is it something I should manage? Is there an out-of-the-box or Prefect way than retrieve all task runs of the flow run and loop over the task run list cancelling them?Giacomo Chiarella
03/24/2025, 5:42 PMtr_state_updates = {}
tr_state_updates.setdefault("name", "Cancelled")
tr_state_updates.setdefault("type", StateType.CANCELLED)
state = flow_runs[0].state.copy(update=tr_state_updates)
await client.set_task_run_state(task_run_id=task_run.id, state=state, force=True)
tasks are still executed. Anyone can help?Giacomo Chiarella
03/24/2025, 5:58 PMChinh Dinh Minh
03/25/2025, 2:59 AMGiacomo Chiarella
03/25/2025, 7:54 AMChinh Dinh Minh
03/25/2025, 7:56 AMGiacomo Chiarella
03/25/2025, 7:57 AMChinh Dinh Minh
03/25/2025, 8:05 AMChinh Dinh Minh
03/25/2025, 8:06 AMstate = flow_runs[0].state.copy(update=tr_state_updates)
Fails if no state found in a flow run. In your above code, in case flow_run[0].state
is None, it will fail to copy.Chinh Dinh Minh
03/25/2025, 8:07 AMGiacomo Chiarella
03/25/2025, 8:08 AMChinh Dinh Minh
03/25/2025, 2:08 PMGiacomo Chiarella
03/25/2025, 2:15 PM