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

shield() documentation should mention user needs to keep reference to the task #94972

Closed
graingert opened this issue Jul 18, 2022 · 7 comments
Closed
Labels
docs Documentation in the Doc dir topic-asyncio

Comments

@graingert
Copy link
Contributor

graingert commented Jul 18, 2022

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

@graingert graingert added the docs Documentation in the Doc dir label Jul 18, 2022
@graingert graingert changed the title sheild() documentation should mention user needs to keep reference to the task shield() documentation should mention user needs to keep reference to the task Jul 18, 2022
graingert added a commit to graingert/cpython that referenced this issue Jul 18, 2022
@kumaraditya303
Copy link
Contributor

Can you provide reproducible code where this happens?

@graingert
Copy link
Contributor Author

See also #93297

@graingert
Copy link
Contributor Author

graingert commented Jul 22, 2022

@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:

closed!
Traceback (most recent call last):
  File "/home/graingert/projects/foo.py", line 14, in async_fn
    await asyncio.get_running_loop().create_future()
GeneratorExit
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<async_fn() done, defined at /home/graingert/projects/foo.py:12> wait_for=<Future pending cb=[Task.task_wakeup()]>>
None
======================
closed!
Traceback (most recent call last):
  File "/home/graingert/projects/foo.py", line 14, in async_fn
    await asyncio.get_running_loop().create_future()
GeneratorExit
Task was destroyed but it is pending!
task: <Task pending name='Task-6' coro=<async_fn() done, defined at /home/graingert/projects/foo.py:12> wait_for=<Future pending cb=[Task.task_wakeup()]>>

@gvanrossum
Copy link
Member

Are you too busy to submit a PR that updates the docs?

@graingert
Copy link
Contributor Author

There's one linked here #94973

@gvanrossum
Copy link
Member

I think I get the same output on 3.11. Question, is import objgraph needed in the repro? (I had to delete it, no idea what it is.)

gvanrossum added a commit that referenced this issue Sep 10, 2022
…r task (#96724)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 10, 2022
…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>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 10, 2022
…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>
miss-islington added a commit that referenced this issue Sep 10, 2022
…r task (GH-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>
miss-islington added a commit that referenced this issue Sep 10, 2022
…r task (GH-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>
@graingert
Copy link
Contributor Author

I think I get the same output on 3.11. Question, is import objgraph needed in the repro? (I had to delete it, no idea what it is.)

Left in by accident. It's not needed to cause the error but I used it to help build it

ynkdir added a commit to ynkdir/py-win32more that referenced this issue Dec 28, 2024
Follow create_task() documentasion.

python/cpython#94972

run_coroutine_threadsafe() has same problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-asyncio
Projects
None yet
Development

No branches or pull requests

4 participants