Skip to content

Commit 7ebe924

Browse files
authored
gh-144969: Document that the free threading GC can change ob_tid (#144972)
1 parent 0bbdb4e commit 7ebe924

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Include/internal/pycore_critical_section.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ extern "C" {
5050
// Asserts that the mutex for the given object is locked. The mutex must
5151
// be held by the top-most critical section otherwise there's the
5252
// possibility that the mutex would be swalled out in some code paths.
53+
//
54+
// NOTE: We use Py_REFCNT(op) != 1 instead of
55+
// !PyUnstable_Object_IsUniquelyReferenced(op) because the free threading
56+
// GC can change an object's ob_tid (it overwrites ob_tid and later
57+
// restores it from the mimalloc segment). This means
58+
// PyUnstable_Object_IsUniquelyReferenced() may spuriously return false
59+
// after a GC collection, even though the thread may still have exclusive
60+
// access to the object. The refcount check is a looser but still catches
61+
// most misuses.
5362
#ifdef Py_DEBUG
5463

5564
# define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op) \

InternalDocs/garbage_collector.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ pointer-sized (that is, eight bytes on a 64-bit platform).
153153

154154
The garbage collector also temporarily repurposes the `ob_tid` (thread ID)
155155
and `ob_ref_local` (local reference count) fields for other purposes during
156-
collections.
156+
collections. The `ob_tid` field is later restored from the containing
157+
mimalloc segment data structure. In some cases, such as when the original
158+
allocating thread exits, this can result in a different `ob_tid` value.
159+
Code should not rely on `ob_tid` being stable across operations that may
160+
trigger garbage collection.
157161

158162

159163
C APIs

0 commit comments

Comments
 (0)