GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup by ngoldbaum · Pull Request #159073 · python/cpython

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Oct 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Centralize transfer of remaining tasks to the interpreter in free_threadstate(), before normal thread-state unlinking and GIL release. Keep memory deallocation separate for initialization failures and deferred daemon-thread cleanup.

Add regression tests for both thread-state and interpreter cleanup paths.

  • Issue: asyncio Task created from a finalizer during PyThreadState_Clear() registers into memory that is about to be freed (3.14 regression) #159041

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ngoldbaum
pythongh-159041: Fix asyncio task tracking during thread state cleanup
bb63e8c
Move lingering asyncio tasks to the interpreter only after all
finalizer-capable thread-state cleanup has completed. Also transfer tasks
created by finalizers during interpreter shutdown.

Add regression tests for both thread-state and interpreter cleanup paths.
@bedevere-app bedevere-app Bot mentioned this pull request Oct 9, 2026
asyncio Task created from a finalizer during PyThreadState_Clear() registers into memory that is about to be freed (3.14 regression) #159041
Closed
@ngoldbaum
pythongh-159041: Strengthen asyncio task cleanup checks
ddf0d98
@BHUVANSH855 BHUVANSH855 mentioned this pull request Oct 14, 2026
gh-159041: Fix asyncio task tracking during thread state clear #159319
Closed
ngoldbaum added 2 commits October 16, 2026 09:49
@ngoldbaum
pythongh-159041: Transfer asyncio tasks after late interpreter cleanup
a2a41a3
@ngoldbaum
Merge branch 'main' into fix-asyncio-ts-clear
43c91e5
@ngoldbaum
ngoldbaum marked this pull request as ready for review October 16, 2026 16:06
@ngoldbaum
ngoldbaum requested review from 1st1, FFY00, ZeroIntensity, asvetlov, ericsnowcurrently, kumaraditya303 and willingc as code owners October 16, 2026 16:06
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 16, 2026
@ngoldbaum

ngoldbaum commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Ping @colesbury @msullivan since you two have been reviewing asyncio changes recently and Kumar is busy.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum ngoldbaum changed the title gh-159041: Fix asyncio task tracking during thread state cleanup gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup Oct 16, 2026
ngoldbaum added 2 commits October 16, 2026 11:32
@ngoldbaum
pythongh-159041: Fix asyncio task tracking during detached interprete…
f37e91c
…r deletion
@ngoldbaum
pythongh-159041: Simplify the interpreter-clear regression test and m…
eb4cb96
…ove to more appropriate spot
kumaraditya303
kumaraditya303 reviewed Oct 17, 2026
View reviewed changes
Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

ngoldbaum added 3 commits October 17, 2026 09:37
@ngoldbaum
pythongh-159041: Transfer asyncio tasks only during thread state dele…
6a867a2
…tion
@ngoldbaum
pythongh-159041: Centralize asyncio task transfer during thread-state…
c897961
… cleanup
@ngoldbaum
pythongh-159041: Simplify asyncio task transfer during thread-state c…
ee84758
…leanup
kumaraditya303
kumaraditya303 approved these changes Oct 19, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 19, 2026
@kumaraditya303
kumaraditya303 merged commit 999855b into python:main Oct 19, 2026
60 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 19, 2026
@kumaraditya303

kumaraditya303 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303 kumaraditya303 added the topic-asyncio label Oct 19, 2026
@kumaraditya303 kumaraditya303 added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 19, 2026
@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

Sorry, @ngoldbaum 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 999855b07f9b795ca88ac3349060a06c116a1037 3.14
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned kumaraditya303 Oct 19, 2026
@bedevere-app

bedevere-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 19, 2026
kumaraditya303 pushed a commit that referenced this pull request Oct 19, 2026
@miss-islington @ngoldbaum
[3.15] gh-159041: Fix use-after-free in asyncio task tracking during …
faf16ef
…thread state cleanup (GH-159073) (#159694)

gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup (GH-159073)
(cherry picked from commit 999855b)

Co-authored-by: Nathan Goldbaum <[email protected]>
@bedevere-app

bedevere-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 19, 2026
kumaraditya303 added a commit that referenced this pull request Oct 19, 2026
@kumaraditya303 @ngoldbaum
[3.14] gh-159041: Fix use-after-free in asyncio task tracking during …
c542ecd
…thread state cleanup (#159073) (#159695)

(cherry picked from commit 999855b)

Co-authored-by: Nathan Goldbaum <[email protected]>
@bedevere-bot

bedevere-bot commented Oct 19, 2026

Copy link
Copy Markdown

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

Hi! The buildbot AMD64 CentOS9 Refleaks 3.x (tier-1) has failed when building commit 999855b.

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/853/builds/4406) and take a look at the build logs.
  4. Check if the failure is related to this commit (999855b) 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/853/builds/4406

Failed tests:

  • test_profiling

Failed subtests:

  • test_calling_conventions - test.test_profile.ProfileTest.test_calling_conventions

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

==

Click to see traceback logs
Traceback (most recent call last):
  File "/var/lib/buildbot/worker/cstratak-CentOS9-x86_64/3.x.cstratak-CentOS9-x86_64.refleak/build/Lib/test/test_profile.py", line 90, in test_calling_conventions
    self.assertIn(self.expected_max_output, res,
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        "Profiling {0!r} didn't report max:\n{1}".format(stmt, res))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: ':0(max)' not found in '         2 function calls in -0.001 seconds\n\n   Random listing order was used\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n        0    0.000             0.000          profile:0(profiler)\n        1   -0.001   -0.001   -0.001   -0.001 profile:0(max(*([0],), **dict(key=int)))\n        1    0.000    0.000    0.000    0.000 :0(exec)\n\n\n' : Profiling 'max(*([0],), **dict(key=int))' didn't report max:
         2 function calls in -0.001 seconds
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum ngoldbaum mentioned this pull request Oct 30, 2026
gh-159449: Fix a free-threaded asyncio race that corrupts the task lists #159483
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@kumaraditya303 kumaraditya303

Labels

topic-asyncio

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ngoldbaum @kumaraditya303 @bedevere-bot
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.