Skip to content

[3.11] gh-94972: document that shield users need to keep a reference to their task (GH-96724) #96737

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

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/asyncio-future.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Future Functions
preferred way for creating new Tasks.

Save a reference to the result of this function, to avoid
a task disappearing mid execution.
a task disappearing mid-execution.

.. versionchanged:: 3.5.1
The function accepts any :term:`awaitable` object.
Expand Down
17 changes: 13 additions & 4 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ Creating Tasks
.. important::

Save a reference to the result of this function, to avoid
a task disappearing mid execution. The event loop only keeps
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.
may get garbage collected at any time, even before it's done.
For reliable "fire-and-forget" background tasks, gather them in
a collection::

Expand Down Expand Up @@ -520,7 +520,8 @@ Shielding From Cancellation

The statement::

res = await shield(something())
task = asyncio.create_task(something())
res = await shield(task)

is equivalent to::

Expand All @@ -539,11 +540,19 @@ Shielding From Cancellation
the ``shield()`` function should be combined with a try/except
clause, as follows::

task = asyncio.create_task(something())
try:
res = await shield(something())
res = await shield(task)
except CancelledError:
res = None

.. important::

Save a reference to tasks passed to 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.

.. versionchanged:: 3.10
Removed the *loop* parameter.

Expand Down
11 changes: 9 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ def shield(arg):

The statement

res = await shield(something())
task = asyncio.create_task(something())
res = await shield(task)

is exactly equivalent to the statement

Expand All @@ -864,10 +865,16 @@ def shield(arg):
If you want to completely ignore cancellation (not recommended)
you can combine shield() with a try/except clause, as follows:

task = asyncio.create_task(something())
try:
res = await shield(something())
res = await shield(task)
except CancelledError:
res = None

Save a reference to tasks passed to 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.
"""
inner = _ensure_future(arg)
if inner.done():
Expand Down