Hi everyone! I’m cancelling a flow using the follo...
# ask-community
g
Hi everyone! I’m cancelling a flow using the following code
Copy code
flow_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?
even by manually setting each flow run task using
Copy code
tr_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?
found an issue already file in github
c
What version are you running?
g
Prefect 3.2.7
c
Please update to 3.2.14
g
is it fixed?
c
The error is about
Copy code
state = 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.
g
ah so it is because I’m cancelling the flow run before the task runs?
c
It's because by some reason the flow run does not have a state associated with it. Maybe conflict in states, or missing state from scratch. Hope this help.
g
thank you @Chinh Dinh Minh! I will try to upgrade