gh-130202: Fix bug in `_PyObject_ResurrectEnd` in free threaded build by colesbury · Pull Request #130281 · python/cpython · GitHub
Skip to content

gh-130202: Fix bug in _PyObject_ResurrectEnd in free threaded build - #130281

Merged
colesbury merged 2 commits into
python:mainfrom
colesbury:gh-130202-resurrect
Feb 25, 2025
Merged

colesbury merged 2 commits into
python:mainfrom
colesbury:gh-130202-resurrect

Conversation

@colesbury

@colesbury colesbury commented Feb 18, 2025

Copy link
Copy Markdown
Contributor

This fixes a fairly subtle bug involving finalizers and resurrection in debug free threaded builds: if _PyObject_ResurrectEnd returns 1 (i.e., the object was resurrected by a finalizer), it's not safe to access the object because it might still be deallocated. For example:

  • The finalizer may have exposed the object to another thread. That thread may hold the last reference and concurrently deallocate it any time after _PyObject_ResurrectEnd() returns 1.
  • _PyObject_ResurrectEnd() may call _Py_brc_queue_object(), which may internally deallocate the object immediately if the owning thread is dead.

Therefore, it's important not to access the object after it's resurrected. We only violate this in two cases, and only in debug builds:

  • We assert that the object is tracked appropriately. This is now moved up betewen the finalizer and the _PyObject_ResurrectEnd() call.

  • The --with-trace-refs builds may need to remember the object if it's resurrected. This is now handled by _PyObject_ResurrectStart() and _PyObject_ResurrectEnd().

Note that --with-trace-refs is currently disabled in --disable-gil builds because the refchain hash table isn't thread-safe, but this refactoring avoids an additional thread-safety issue.

@colesbury colesbury added skip news 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section topic-free-threading labels Feb 18, 2025
@colesbury colesbury added 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section and removed 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Feb 18, 2025
@colesbury

This comment was marked as resolved.

@bedevere-bot

This comment was marked as resolved.

… build

This fixes a fairly subtle bug involving finalizers and resurrection in
debug free threaded builds: if `_PyObject_ResurrectEnd` returns `1`
(i.e., the object was resurrected by a finalizer), it's not safe to
access the object because it might still be deallocated. For example:

 * The finalizer may have exposed the object to another thread. That
   thread may hold the last reference and concurrently deallocate it any
   time after `_PyObject_ResurrectEnd()` returns `1`.
 * `_PyObject_ResurrectEnd()` may call `_Py_brc_queue_object()`, which
   may internally deallocate the object immediately if the owning thread
   is dead.

Therefore, it's important not to access the object after it's
resurrected. We only violate this in two cases, and only in debug
builds:

 * We assert that the object is tracked appropriately. This is now moved
   up betewen the finalizer and the `_PyObject_ResurrectEnd()` call.

 * The `--with-trace-refs` builds may need to remember the object if
   it's resurrected. This is now handled by `_PyObject_ResurrectStart()`
   and `_PyObject_ResurrectEnd()`.

Note that `--with-trace-refs` is currently disabled in `--disable-gil`
builds because the refchain hash table isn't thread-safe, but this
refactoring avoids an additional thread-safety issue.
@colesbury colesbury removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 18, 2025
@colesbury
colesbury marked this pull request as ready for review February 18, 2025 21:42
@colesbury colesbury added 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section and removed 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Feb 18, 2025

@mpage mpage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@colesbury
colesbury merged commit f963239 into python:main Feb 25, 2025
@colesbury
colesbury deleted the gh-130202-resurrect branch February 25, 2025 17:03
@bedevere-bot

Copy link
Copy Markdown

@freakboy3742

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants