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

New asyncio rule: directly passing coroutine to gather, shield, wait_for, wait, or as_completed #319

Open
jakkdl opened this issue Nov 17, 2024 · 1 comment
Labels
enhancement New feature or request postponed Low priority, blocked, or similar.

Comments

@jakkdl
Copy link
Member

jakkdl commented Nov 17, 2024

Tasks without saved reference can be garbage collected, and several functions in asyncio automatically converts coroutines into tasks - which means it's ~impossible to save a strong reference to those tasks. See e.g. https://docs.python.org/3/library/asyncio-task.html#asyncio.shield

The rule implementation would be fairly straightforward, just check for calls in the parameter list: asyncio.shield(anything()). This will give false alarms if anything is not a coroutine, but instead a sync function that creates a task, saves it globally, and returns it, but I suspect that's a minority of cases. We could save the names of any sync funcs to reduce the false alarm rate, but that wouldn't work when the functions are imported from other files.

We could also use type-checking to catch

a = my_coro()
asyncio.wait_for(a)

On py311+ asyncio.wait errors if directly passed coroutines.

There appears to be movement in making the event loop save strong references, but that'll probably only affect py3.14/py3.15 or later python/cpython#121264

@jakkdl jakkdl added the enhancement New feature or request label Nov 17, 2024
@graingert
Copy link
Member

Even with strong references it's possible to leak an exception from the task when it gets lost by gather/shield/wait etc

@jakkdl jakkdl added the postponed Low priority, blocked, or similar. label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request postponed Low priority, blocked, or similar.
Projects
None yet
Development

No branches or pull requests

2 participants