Repository navigation
gh-159449: Fix a free-threaded asyncio race that corrupts the task lists #159483
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
Open
jblespiau
wants to merge
9
commits into
python:main
Choose a base branch
from
jblespiau:fix-asyncio-task-unregister-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−5
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e75acc
gh-159449: Fix a free-threaded asyncio race that corrupts the task lists
jblespiau e60bb19
📜🤖 Added by blurb_it.
blurb-it[bot] acea81d
Remove extra blub-it file.
jblespiau e79f79e
Merge branch 'main' into fix-asyncio-task-unregister-race
jblespiau 78ffcac
Merge branch 'main' into fix-asyncio-task-unregister-race
jblespiau 58c83fc
Remove comments and rebase to head.
jblespiau 3f23308
Merge branch 'main' of https://github.com/python/cpython into fix-asy…
jblespiau 0def8d4
Restore main changes in _asynciomodule.c and remove comments
jblespiau 5a64c43
Restore original comments.
jblespiau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-10-15-27.gh-issue-159449.xsdzFf.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix a free-threaded asyncio race leading to segfaults. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -69,7 +69,7 @@ typedef struct TaskObj { | |||||
| struct llist_node task_node; | ||||||
| #ifdef Py_GIL_DISABLED | ||||||
| // thread id of the thread where this task was created | ||||||
| uintptr_t task_tid; | ||||||
| uint64_t task_tstate_id; | ||||||
| #endif | ||||||
| } TaskObj; | ||||||
|
|
||||||
|
|
@@ -2185,6 +2185,9 @@ register_task(_PyThreadStateImpl *ts, TaskObj *task) | |||||
| assert(task->task_node.prev != NULL); | ||||||
| return; | ||||||
| } | ||||||
| #ifdef Py_GIL_DISABLED | ||||||
| task->task_tstate_id = ts->base.id; | ||||||
| #endif | ||||||
| struct llist_node *head = &ts->asyncio_tasks_head; | ||||||
| llist_insert_tail(head, &task->task_node); | ||||||
| } | ||||||
|
|
@@ -2204,15 +2207,15 @@ static void | |||||
| unregister_task(TaskObj *task) | ||||||
| { | ||||||
| #ifdef Py_GIL_DISABLED | ||||||
| PyThreadState *tstate = _PyThreadState_GET(); | ||||||
| // check if we are in the same thread | ||||||
| // if so, we can avoid locking | ||||||
| if (task->task_tid == _Py_ThreadId()) { | ||||||
| if (task->task_tstate_id == tstate->id && !tstate->_status.finalizing) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
the finalizing check is unnecessary with the current design and causes a significant slowdown on teardown |
||||||
| unregister_task_safe(task); | ||||||
| } | ||||||
| else { | ||||||
| // we are in a different thread | ||||||
| // stop the world then check and remove the task | ||||||
| PyThreadState *tstate = _PyThreadState_GET(); | ||||||
| _PyEval_StopTheWorld(tstate->interp); | ||||||
| unregister_task_safe(task); | ||||||
| _PyEval_StartTheWorld(tstate->interp); | ||||||
|
|
@@ -2341,7 +2344,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, | |||||
|
|
||||||
| Py_CLEAR(self->task_fut_waiter); | ||||||
| #ifdef Py_GIL_DISABLED | ||||||
| self->task_tid = _Py_ThreadId(); | ||||||
| if (self->task_node.next == NULL) { | ||||||
| self->task_tstate_id = _PyThreadState_GET()->id; | ||||||
| } | ||||||
| #endif | ||||||
| self->task_must_cancel = 0; | ||||||
| self->task_log_destroy_pending = 1; | ||||||
|
|
@@ -3987,7 +3992,6 @@ add_tasks_llist(struct llist_node *head, PyListObject *tasks) | |||||
| struct llist_node *node; | ||||||
| llist_for_each_safe(node, head) { | ||||||
| TaskObj *task = llist_data(node, TaskObj, task_node); | ||||||
| assert(task->task_state == STATE_PENDING); | ||||||
| // The linked list holds borrowed references to task | ||||||
| // as such it is possible that the task is concurrently | ||||||
| // deallocated while added to this list. | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop’s ready queue still holds references to tls.tasks, so those tasks won’t be destroyed when the thread exits. They survive until loop.close(). Also, Task.cancel() only requests cancellation. Since the loop never runs, the tasks stay pending even after their coroutines are closed.
Could this follow the original reproducer more closely? I think that would test the change better.