-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-115184: Fix refleak tracking issues in free-threaded build #115188
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
Conversation
- We did not account for abandoned segments, which could miss blocks in multithreaded runs. - The mimalloc heap traversal needed to call "_mi_page_free_collect" earlier in order to get an accurate count of blocks in use. - `_Py_DecRefSharedDebug` was missing a `_Py_IncRefTotal`, but this was mostly offset by a missing accounting in `_Py_ExplicitMergeRefcount`. - get_num_global_allocated_blocks should pause other threads to ensure that traversing the mimalloc heaps is safe.
🤖 New build scheduled with the buildbot fleet by @colesbury for commit 1b17640 🤖 If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again. |
#ifdef Py_REF_DEBUG | ||
_Py_AddRefTotal(_PyInterpreterState_GET(), extra); | ||
#endif | ||
|
||
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); |
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.
Do I understand this correctly that we're not racing here because we're either own the owning thread or the world is stopped for GC?
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 possible cases are:
- We are the owning thread and gave up ownership (typical)
- We are the queueing thread and the owning thread has exited (gh-110481: Implement inter-thread queue for biased reference counting #114824)
- We are the queueing thread and have stopped the world (due to OOM) (gh-110481: Implement inter-thread queue for biased reference counting #114824)
The GC could have used this code path, but it uses a different function that avoids atomics (because it's stopped the world).
The reason case (2) is thread-safe is a bit subtle:
- Queueing is done once, so only one thread will attempt it
- Although thread ids may be reused, the merge for (2) happens while holding a lock that would need to be acquired by any new thread that reuses the thread id.
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.
LGTM!
…ython#115188) Fixes a few issues related to refleak tracking in the free-threaded build: - Count blocks in abandoned segments - Call `_mi_page_free_collect` earlier during heap traversal in order to get an accurate count of blocks in use. - Add missing refcount tracking in `_Py_DecRefSharedDebug` and `_Py_ExplicitMergeRefcount`. - Pause threads in `get_num_global_allocated_blocks` to ensure that traversing the mimalloc heaps is safe.
_mi_page_free_collect
earlier in order to get an accurate count of live blocks._Py_DecRefSharedDebug
was missing a_Py_IncRefTotal
, but this was mostly offset by a missing accounting in_Py_ExplicitMergeRefcount
.