https://prefect.io logo
Title
j

Joshua Grant

12/01/2022, 7:04 PM
Currently having issues with flows hanging when calling
get_run_context()
from within a flow. MRE in 🧵
1
from prefect import flow, task, get_run_logger
from prefect.context import get_run_context


@task
def print_task(obj):
    logger = get_run_logger()
    <http://logger.info|logger.info>(obj)


@flow
def test_flow():
    ctx = get_run_context()
    print_task(ctx)


if __name__ == "__main__":
    test_flow()
output is:
> python sample_flow.py
13:46:36.385 | INFO    | prefect.engine - Created flow run 'thankful-seagull' for flow 'test-flow'
This will not finish until Ctrl+C
z

Zanie

12/01/2022, 7:07 PM
This looks specifically like a bug in passing the run context from a flow to a task, not calling
get_run_context
.
The flow run context is full of states and task run tracking information — we’re probably stuck doing resolution of states to data nested in the object.
If you just pass the data you need, you should be fine — I’ll work on a fix for this though.
Note this definitely will not be supported if you’re using a remote runner like Dask. Why do you want to pass the flow run context to a task?
j

Joshua Grant

12/01/2022, 7:15 PM
We're currently migrating from 0.15.9 to 2.6.9 and have external tools that we send flow run information to.
z

Zanie

12/01/2022, 7:19 PM
What information do you need?
j

Joshua Grant

12/01/2022, 7:22 PM
currently we send flow_id and run time. flow_id is used in some calls and we have a flow killer that kills a flow if it has run over 1 hr.
There's probably a much easier way to do this now,
I mean, our external tool will have to be rewritten anyway as it highly utilizes GraphQL
instead of passing the entire context, I got the
flow_id
by passing
ctx.flow_run.flow_id
to the log statement.
14:30:49.797 | INFO    | prefect.engine - Created flow run 'sepia-hyena' for flow 'test-flow'
14:30:49.873 | INFO    | Flow run 'sepia-hyena' - Created task run 'print_task-3fb05284-0' for task 'print_task'
14:30:49.873 | INFO    | Flow run 'sepia-hyena' - Executing 'print_task-3fb05284-0' immediately...
14:30:49.890 | INFO    | Task run 'print_task-3fb05284-0' - 5c22623d-9c2e-4610-87c1-8fc6249938f7
14:30:49.900 | INFO    | Task run 'print_task-3fb05284-0' - Finished in state Completed()
14:30:49.910 | INFO    | Flow run 'sepia-hyena' - Finished in state Completed('All states completed.')
Thanks @Zanie
z

Zanie

12/01/2022, 7:38 PM
👍
@Marvin open “Passing the flow run context to a task causes deadlock”
Duno if the bot is good these days
🤖 1
l

Leon Kozlowski

02/02/2023, 8:56 PM
Was this ever solved, I'm seeing similar behavior as well and wasn't able to find an issue on github
j

Joshua Grant

02/02/2023, 9:06 PM
The way I solved it was to grab the information I need in the flow logic and pass that information along to tasks. So in my flow I would have:
@flow
def my_flow():
    ctx = get_run_context()
    flow_id = ctx.flow_run.flow_id
    my_task_results = my_task(flow_id)
l

Leon Kozlowski

02/02/2023, 9:06 PM
Got it - Thanks Joshua