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
Make the garbage collector thread-safe in --disable-gil builds
#112529
Labels
3.13
new features, bugs and security fixes
topic-free-threading
type-feature
A feature request or enhancement
Comments
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Nov 29, 2023
The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
pablogsal
pushed a commit
that referenced
this issue
Dec 11, 2023
* gh-112529: Use atomic operations for `gcstate->collecting` The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
…ator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
…ator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
…ator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 5, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 16, 2024
…ator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 16, 2024
DinoV
pushed a commit
that referenced
this issue
Jan 16, 2024
…113747) * gh-112529: Track if debug allocator is used as underlying allocator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled. * Simplify _PyMem_DebugEnabled
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 16, 2024
The free-threaded build's garbage collector implementation will need to find GC objects by traversing mimalloc heaps. This hooks up the allocation calls with the correct heaps by using a thread-local "current_obj_heap" variable.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 16, 2024
The free-threaded build's garbage collector implementation will need to find GC objects by traversing mimalloc heaps. This hooks up the allocation calls with the correct heaps by using a thread-local "current_obj_heap" variable.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
…ator The GC implementation for free-threaded builds will need to accurately detect if the debug allocator is used because it affects the offset of the Python object from the beginning of the memory allocation. The current implementation of `_PyMem_DebugEnabled` only considers if the debug allocator is the outer-most allocator; it doesn't handle the case of "hooks" like tracemalloc being used on top of the debug allocator. This change enables more accurate detection of the debug allocator by tracking when debug hooks are enabled.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
The free-threaded build's garbage collector implementation will need to find GC objects by traversing mimalloc heaps. This hooks up the allocation calls with the correct heaps by using a thread-local "current_obj_heap" variable.
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
colesbury
added a commit
to colesbury/cpython
that referenced
this issue
Jan 18, 2024
corona10
pushed a commit
that referenced
this issue
Jan 20, 2024
…h-114157) * gh-112529: Use GC heaps for GC allocations in free-threaded builds The free-threaded build's garbage collector implementation will need to find GC objects by traversing mimalloc heaps. This hooks up the allocation calls with the correct heaps by using a thread-local "current_obj_heap" variable. * Refactor out setting heap based on type
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
3.13
new features, bugs and security fixes
topic-free-threading
type-feature
A feature request or enhancement


Feature or enhancement
Python's cyclic garbage collector relies on the global interpreter lock for thread-safety. There are a number of changes needed to make the GC thread-safe in
--disable-gilbuilds.My intention is to implement these as a series of smaller changes.
gcstate->collecting. This need to be made thread-safe.--disable-gilbuilds should stop-the-world when finding garbage, but not when calling finalizers or destructors. (depends on Implement stop-the-world functionality (for--disable-gilbuilds) #111964)--disable-gilbuilds should find GC-enabled objects by traversing the mimalloc heaps, because the_gc_next/_gc_prevlists are not thread-safe. (Note it's safe to use the lists during stop-the-world pauses; we just can't maintain them safely during normal Python execution).--disable-gilbuilds should probably use only a single generation to avoid frequent stop-the-world pauses--disable-gilbuilds to save memory. (This will require updating the trashcan mechanism.)See also #111964
Linked PRs
gcstate->collecting#112533The text was updated successfully, but these errors were encountered: