镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Make the garbage collector thread-safe in --disable-gil builds #112529

Description

@colesbury

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-gil builds.

My intention is to implement these as a series of smaller changes.

  • Garbage collection is guarded by gcstate->collecting. This need to be made thread-safe.
  • The --disable-gil builds should stop-the-world when finding garbage, but not when calling finalizers or destructors. (depends on Implement stop-the-world functionality (for --disable-gil builds) #111964)
  • The --disable-gil builds should find GC-enabled objects by traversing the mimalloc heaps, because the _gc_next/_gc_prev lists 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).
  • The --disable-gil builds should probably use only a single generation to avoid frequent stop-the-world pauses
  • Eventually, we can get rid of the GC pre-header in --disable-gil builds to save memory. (This will require updating the trashcan mechanism.)

Remaining work:

  • Fix scheduling of free-threaded GC (currently scheduled as if there is a young generation)
  • Update Python docs
  • Refactor out common code from gc.c and gc_free_threaded.c

See also #111964

Linked PRs

Activity

  1. self-assigned this
    on Nov 29, 2023
  2. added a commit that references this issue on Nov 29, 2023
  3. added a commit that references this issue on Dec 11, 2023
  4. added 6 commits that reference this issue on Jan 5, 2024
  5. added 2 commits that reference this issue on Jan 16, 2024
  6. added a commit that references this issue on Jan 16, 2024
  7. 36 remaining items

  8. added a commit that references this issue on Mar 25, 2024
  9. added 2 commits that reference this issue on Mar 29, 2024
  10. added 3 commits that reference this issue on Apr 17, 2024
  11. colesbury commented on May 7, 2024

    @colesbury
    ContributorAuthor

    I'm going to close this as the free-threaded GC is implemented now. We can open a new issue for refactoring out the common code.

  12. added 4 commits that reference this issue on Sep 2, 2024
  13. added a commit that references this issue on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions