Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Asyncio task created without holding a reference to it in Workflow #18058

Open
josephgrigg opened this issue Mar 8, 2025 · 3 comments
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized

Comments

@josephgrigg
Copy link

Bug Description

Similar to #17801, but in another location. An asyncio task is created using asyncio.create_task, but the reference to the task isn't kept. Opening a separate github issue for this since the previous one is closed.

Important Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done.
https://docs.python.org/3/library/asyncio-task.html#creating-tasks

Wouldn't this be an issue here in Workflow? The task returned by asyncio.create_task is not being used.

Originally posted by @josephgrigg in #17801

Version

0.12.23.post2

Steps to Reproduce

Chance of this happening randomly when garbage collection occurs while the task is still running.

Relevant Logs/Tracbacks

@josephgrigg josephgrigg added bug Something isn't working triage Issue needs to be triaged/prioritized labels Mar 8, 2025
Copy link

dosubot bot commented Mar 8, 2025

Yes, there is indeed an issue in the Workflow where the task created by asyncio.create_task is not being used, which can lead to the task being garbage collected before it completes. This is a known problem that can cause undefined behavior and errors like 'Task exception was never retrieved'. To address this, you should store a reference to the task to ensure it is not garbage collected prematurely. Alternatively, you can use await directly if immediate execution is appropriate, or use TaskGroups in Python 3.11+ to manage task lifetimes automatically [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@logan-markewich
Copy link
Collaborator

tbh this has never been an issue, but I guess so?

@logan-markewich
Copy link
Collaborator

Feel free to submit a PR, if not I'll get around to it tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized
Projects
None yet
Development

No branches or pull requests

2 participants