Skip to content
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

gh-115184: Fix refleak tracking issues in free-threaded build #115188

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Feb 8, 2024

  • We did not account for abandoned segments, which could miss blocks in programs that use multiple threads.
  • The mimalloc heap traversal needed to call _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.
  • get_num_global_allocated_blocks should pause other threads to ensure that traversing the mimalloc heaps is safe.

- 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.
@bedevere-bot
Copy link

🤖 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.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Feb 8, 2024
#ifdef Py_REF_DEBUG
_Py_AddRefTotal(_PyInterpreterState_GET(), extra);
#endif

_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0);
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The possible cases are:

  1. We are the owning thread and gave up ownership (typical)
  2. We are the queueing thread and the owning thread has exited (gh-110481: Implement inter-thread queue for biased reference counting #114824)
  3. 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.

Copy link
Contributor

@DinoV DinoV left a comment

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 31633f4 into python:main Feb 9, 2024
54 checks passed
@colesbury colesbury deleted the gh-115184-refleak branch February 9, 2024 14:23
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
…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.
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.

3 participants