Skip to content

gh-159449: Fix a free-threaded asyncio race that corrupts the task lists - #159483

Merged
kumaraditya303 merged 13 commits into
python:mainfrom
jblespiau:fix-asyncio-task-unregister-race
Nov 5, 2026
Merged

kumaraditya303 merged 13 commits into
python:mainfrom
jblespiau:fix-asyncio-task-unregister-race

Conversation

@jblespiau

@jblespiau jblespiau commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

When a thread exits with pending asyncio Tasks, PyThreadState_Clear() moves them onto the shared interp->asyncio_tasks_head list, but each Task still records the _Py_ThreadId() of the thread that created it. unregister_task() skips the stop-the-world pause whenever that id matches the current thread. That also happens for a later thread that reuses the exited thread's pthread_t, and for destructors that run later in PyThreadState_Clear(). The resulting unlocked llist_remove() calls on the shared list race with each other and can leave an already-removed node (next == NULL) linked into it. asyncio.all_tasks(), which asyncio.run() calls on shutdown, then crashes with SIGSEGV in _asyncio_all_tasks().

Record the unique id (PyThreadState.id) of the creating thread state instead, and only take the lock-free path while that thread state is not being cleared.

Also drop the assertion in add_tasks_llist() that every linked Task is pending. A Task's state is set before unregister_task() unlinks it, and that removal can be waiting for the stop-the-world pause during which the list is walked. add_one_task() already filters out Tasks that are done.

3.14 fix is in #159451 that we can reopen if this one is accepted.

…ask lists

When a thread exits with pending asyncio Tasks, PyThreadState_Clear() moves them
onto the shared interp->asyncio_tasks_head list, but each Task still records the
_Py_ThreadId() of the thread that created it. unregister_task() skips the
stop-the-world pause whenever that id matches the current thread. That also
happens for a later thread that reuses the exited thread's pthread_t, and for
destructors that run later in PyThreadState_Clear(). The resulting unlocked
llist_remove() calls on the shared list race with each other and can leave an
already-removed node (next == NULL) linked into it. asyncio.all_tasks(), which
asyncio.run() calls on shutdown, then crashes with SIGSEGV in
_asyncio_all_tasks().

Record the unique id (PyThreadState.id) of the creating thread state instead,
and only take the lock-free path while that thread state is not being cleared.

Also drop the assertion in add_tasks_llist() that every linked Task is pending.
A Task's state is set before unregister_task() unlinks it, and that removal can
be waiting for the stop-the-world pause during which the list is walked.
add_one_task() already filters out Tasks that are done.
@python-cla-bot

python-cla-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@jblespiau

Copy link
Copy Markdown
Contributor Author

Hi,

is there anything I could do to help unblock this PR? I don't know what is the expected review time for such bug-fixes. I don't know if they are reviewed in bulk every N-weeks or if we don't hear back after a few days it's a bad sign.

@ngoldbaum

Copy link
Copy Markdown
Contributor

@kumaraditya303 I think this is related to #159073.

@jblespiau you're going to need to update this to account for changes made there.

@jblespiau

Copy link
Copy Markdown
Contributor Author

Updated and rebased on HEAD.

Comment thread Modules/_asynciomodule.c Resolved
@jblespiau

Copy link
Copy Markdown
Contributor Author

I think it's done, and I rebased on head again. Github is showing me it's "processing updates".

if this version still need changes, maybe it would be easier if a Python maintainer takes over as they are more familar with the tooling, tests, code, etc. As there is a reproducer, it should already help quite a lot.

the test passes after the CL, and fails on the assert Assertion 'task->task_state == STATE_PENDING' failed (Aborted). from head, and with this removed, it fails with Fatal Python error: Segmentation fault.

@jblespiau

Copy link
Copy Markdown
Contributor Author

I don't know why, but what I see inthe PR isn't what I have in the branch in my fork:

main...jblespiau:cpython:fix-asyncio-task-unregister-race

@kumaraditya303
kumaraditya303 force-pushed the fix-asyncio-task-unregister-race branch from 5faa64f to 12ea214 Compare November 5, 2026 04:40
@kumaraditya303 kumaraditya303 added topic-asyncio needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 5, 2026
Comment thread Modules/_asynciomodule.c Resolved
@kumaraditya303
kumaraditya303 merged commit aaaf61c into python:main Nov 5, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @jblespiau for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @jblespiau and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker aaaf61ca62f0924d7b5134c2d237313d8be2215c 3.14
@bedevere-app

bedevere-app Bot commented Nov 5, 2026

Copy link
Copy Markdown

GH-161379 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 5, 2026
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Nov 5, 2026
…the task lists (pythonGH-159483)

(cherry picked from commit aaaf61c)

Co-authored-by: Jean-Baptiste Lespiau <jeanbaptiste.lespiau@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@bedevere-app

bedevere-app Bot commented Nov 5, 2026

Copy link
Copy Markdown

GH-161380 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 5, 2026
kumaraditya303 added a commit that referenced this pull request Nov 5, 2026
…sk lists (GH-159483) (#161379)

gh-159449: Fix free-threaded asyncio race that corrupts the task lists (GH-159483)
(cherry picked from commit aaaf61c)

Co-authored-by: Jean-Baptiste Lespiau <jeanbaptiste.lespiau@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull request Nov 5, 2026
…sk lists (GH-159483) (#161380)

(cherry picked from commit aaaf61c)

Co-authored-by: Jean-Baptiste Lespiau <jeanbaptiste.lespiau@gmail.com>
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 FreeBSD Refleaks 3.15 (tier-3) has failed when building commit 3619efa.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/2056/builds/487) and take a look at the build logs.
  4. Check if the failure is related to this commit (3619efa) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/2056/builds/487

Failed tests:

  • test.test_asyncio.test_ssl

Failed subtests:

  • test_remote_shutdown_receives_trailing_data - test.test_asyncio.test_ssl.TestSSL.test_remote_shutdown_receives_trailing_data

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd14.refleak/build/Lib/test/test_asyncio/test_ssl.py", line 1430, in test_remote_shutdown_receives_trailing_data
    self.loop.run_until_complete(client(srv.addr))
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.15.opsec-fbsd14.refleak/build/Lib/asyncio/base_events.py", line 730, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/home/buildbot/buildarea/3.15.opsec-fbsd14.refleak/build/Lib/test/test_asyncio/test_ssl.py", line 1414, in client
    await future
  File "/home/buildbot/buildarea/3.15.opsec-fbsd14.refleak/build/Lib/test/test_asyncio/test_ssl.py", line 1422, in wrapper
    meth(sock)
    ~~~~^^^^^^
  File "/home/buildbot/buildarea/3.15.opsec-fbsd14.refleak/build/Lib/test/test_asyncio/test_ssl.py", line 1368, in server
    self.assertEqual(data_len, CHUNK * SIZE)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 0 != 4194304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants