Skip to content

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

Merged
colesbury merged 1 commit into
python:mainfrom
colesbury:gh-115184-refleak
Feb 9, 2024
Merged

colesbury merged 1 commit into
python:mainfrom
colesbury:gh-115184-refleak

Conversation

@colesbury

@colesbury colesbury commented Feb 8, 2024 •

Copy link
Copy Markdown
Contributor
  • 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
Copy Markdown

🤖 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
Comment thread Objects/object.c
_Py_AddRefTotal(_PyInterpreterState_GET(), extra);
#endif

_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0);

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.

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
Copy Markdown
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.

@DinoV DinoV 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 31633f4 into python:main Feb 9, 2024
@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

3 participants