-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
shield() documentation should mention user needs to keep reference to the task #94972
Comments
Can you provide reproducible code where this happens? |
See also #93297 |
@kumaraditya303 here's the repro on python3.10: import weakref
import asyncio
import gc
import logging
logger = logging.getLogger(__name__)
async def async_fn():
try:
await asyncio.get_running_loop().create_future()
except BaseException:
logger.exception("closed!")
raise
async def amain():
weak_task = weakref.ref(asyncio.create_task(async_fn()))
await asyncio.sleep(0.01)
gc.collect()
print(weak_task())
async def sheild_gc():
strong_shielded_fut = asyncio.shield(async_fn())
await asyncio.sleep(0)
asyncio.current_task().cancel()
try:
await strong_shielded_fut
except asyncio.CancelledError:
pass
gc.collect()
asyncio.run(amain())
print("======================")
asyncio.run(sheild_gc()) output:
|
Are you too busy to submit a PR that updates the docs? |
There's one linked here #94973 |
I think I get the same output on 3.11. Question, is |
…r task (#96724) Co-authored-by: Thomas Grainger <tagrain@gmail.com> Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
…o their task (pythonGH-96724) Co-authored-by: Thomas Grainger <tagrain@gmail.com> Co-authored-by: Guido van Rossum <gvanrossum@gmail.com> (cherry picked from commit 6281aff) Co-authored-by: Hendrik Makait <hendrik.makait@gmail.com>
…o their task (pythonGH-96724) Co-authored-by: Thomas Grainger <tagrain@gmail.com> Co-authored-by: Guido van Rossum <gvanrossum@gmail.com> (cherry picked from commit 6281aff) Co-authored-by: Hendrik Makait <hendrik.makait@gmail.com>
Left in by accident. It's not needed to cause the error but I used it to help build it |
Follow create_task() documentasion. python/cpython#94972 run_coroutine_threadsafe() has same problem.
Documentation
asyncio will only keep weak references to alive tasks (in _all_tasks). If a user does not keep a reference to a task and the task is not currently executing or sleeping, the user may get "Task was destroyed but it is pending!".
see also #88831
shield has the same issue and the documentation currently recommends the incorrect usage
The text was updated successfully, but these errors were encountered: