Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
e6a57678
Unverified
Commit
e6a57678
authored
2 years ago
by
Yury Selivanov
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
gh-93297: Make asyncio task groups prevent child tasks from being GCed (#93299)
parent
70cfe56c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Lib/asyncio/taskgroups.py
+6
-13
6 additions, 13 deletions
Lib/asyncio/taskgroups.py
Misc/NEWS.d/next/Library/2022-05-27-13-18-18.gh-issue-93297.e2zuHz.rst
+1
-0
1 addition, 0 deletions
...ext/Library/2022-05-27-13-18-18.gh-issue-93297.e2zuHz.rst
with
7 additions
and
13 deletions
Lib/asyncio/taskgroups.py
+
6
−
13
View file @
e6a57678
...
...
@@ -3,8 +3,6 @@
__all__
=
[
"
TaskGroup
"
]
import
weakref
from
.
import
events
from
.
import
exceptions
from
.
import
tasks
...
...
@@ -19,8 +17,7 @@ def __init__(self):
self
.
_loop
=
None
self
.
_parent_task
=
None
self
.
_parent_cancel_requested
=
False
self
.
_tasks
=
weakref
.
WeakSet
()
self
.
_unfinished_tasks
=
0
self
.
_tasks
=
set
()
self
.
_errors
=
[]
self
.
_base_error
=
None
self
.
_on_completed_fut
=
None
...
...
@@ -29,8 +26,6 @@ def __repr__(self):
info
=
[
''
]
if
self
.
_tasks
:
info
.
append
(
f
'
tasks=
{
len
(
self
.
_tasks
)
}
'
)
if
self
.
_unfinished_tasks
:
info
.
append
(
f
'
unfinished=
{
self
.
_unfinished_tasks
}
'
)
if
self
.
_errors
:
info
.
append
(
f
'
errors=
{
len
(
self
.
_errors
)
}
'
)
if
self
.
_aborting
:
...
...
@@ -93,7 +88,7 @@ async def __aexit__(self, et, exc, tb):
# can be cancelled multiple times if our parent task
# is being cancelled repeatedly (or even once, when
# our own cancellation is already in progress)
while
self
.
_
unfinished_
tasks
:
while
self
.
_tasks
:
if
self
.
_on_completed_fut
is
None
:
self
.
_on_completed_fut
=
self
.
_loop
.
create_future
()
...
...
@@ -114,7 +109,7 @@ async def __aexit__(self, et, exc, tb):
self
.
_on_completed_fut
=
None
assert
self
.
_
unfinished_tasks
==
0
assert
not
self
.
_
tasks
if
self
.
_base_error
is
not
None
:
raise
self
.
_base_error
...
...
@@ -141,7 +136,7 @@ async def __aexit__(self, et, exc, tb):
def
create_task
(
self
,
coro
,
*
,
name
=
None
,
context
=
None
):
if
not
self
.
_entered
:
raise
RuntimeError
(
f
"
TaskGroup
{
self
!r}
has not been entered
"
)
if
self
.
_exiting
and
self
.
_
unfinished_tasks
==
0
:
if
self
.
_exiting
and
not
self
.
_
tasks
:
raise
RuntimeError
(
f
"
TaskGroup
{
self
!r}
is finished
"
)
if
context
is
None
:
task
=
self
.
_loop
.
create_task
(
coro
)
...
...
@@ -149,7 +144,6 @@ def create_task(self, coro, *, name=None, context=None):
task
=
self
.
_loop
.
create_task
(
coro
,
context
=
context
)
tasks
.
_set_task_name
(
task
,
name
)
task
.
add_done_callback
(
self
.
_on_task_done
)
self
.
_unfinished_tasks
+=
1
self
.
_tasks
.
add
(
task
)
return
task
...
...
@@ -169,10 +163,9 @@ def _abort(self):
t
.
cancel
()
def
_on_task_done
(
self
,
task
):
self
.
_unfinished_tasks
-=
1
assert
self
.
_unfinished_tasks
>=
0
self
.
_tasks
.
discard
(
task
)
if
self
.
_on_completed_fut
is
not
None
and
not
self
.
_
unfinished_
tasks
:
if
self
.
_on_completed_fut
is
not
None
and
not
self
.
_tasks
:
if
not
self
.
_on_completed_fut
.
done
():
self
.
_on_completed_fut
.
set_result
(
True
)
...
...
This diff is collapsed.
Click to expand it.
Misc/NEWS.d/next/Library/2022-05-27-13-18-18.gh-issue-93297.e2zuHz.rst
0 → 100644
+
1
−
0
View file @
e6a57678
Make asyncio task groups prevent child tasks from being GCed
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment