Skip to content

Add optional wait_if_running argument to gc.collect() #137682

Open

Description

@colesbury

Feature or enhancement

Background

The gc.collect() documentation states:

The effect of calling gc.collect() while the interpreter is already performing a collection is undefined.

In practice, gc.collect() will return immediately if it's already running, including re-entrant calls from the same thread (i.e., from object finalizers) and if the GC happens to be running in another thread. Note that even with the GIL, the GC isn't completely atomic: running tp_clear and object finalizes (i.e., __del__ functions) can release the GIL and allow another thread to run concurrently.

Motivation

Both CPython and third-party packages often use gc.collect() in tests to ensure that cycles are collected and, in the free threading build, any delayed reference count operations are processed when testing for things related to object lifetimes.

The problem is that these tests can be flaky if multiple threads are used, because if one thread happens to be running the GC, then the gc.collect() in another thread returns immediately without doing anything. This happens more frequently in the free threading build because there are more opportunities for interleaving between threads and because of things like biased reference counting that can lead to delayed object destructors.

Proposal

Add a keyword-only argument wait_if_running to gc.collect(). The default value is wait_if_running=False, which preserves the current behavior. If wait_if_running=True:

  • If the GC is running in a different thread, then the call will wait until that GC finishes and then run the GC.
  • If the GC is running in the caller's thread, then the call will raise an exception so as to avoid deadlock.

cc @hawkinsp

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Activity

  1. sergey-miryanov commented on Aug 12, 2025

    @sergey-miryanov
    Contributor

    Maybe it is worth to add a private function to a test.support, for example? I'm afraid that, once it becomes public, it will be used incorrectly and users will face nondeterministic freezes with gc.collect.

  2. ngoldbaum commented on Aug 12, 2025

    @ngoldbaum
    Contributor
  3. hawkinsp commented on Aug 13, 2025

    @hawkinsp
    Contributor

    Maybe it is worth to add a private function to a test.support, for example? I'm afraid that, once it becomes public, it will be used incorrectly and users will face nondeterministic freezes with gc.collect.

    This is something that end users may need to do in their tests, if they want to be sure that garbage collection ran at a particular program point. A private function does not help.

  4. ZeroIntensity commented on Aug 13, 2025

    @ZeroIntensity
    Member

    Maybe it is worth to add a private function to a test.support, for example? I'm afraid that, once it becomes public, it will be used incorrectly and users will face nondeterministic freezes with gc.collect.

    test.support (and the entire test module) isn't shipped to users. I think incorrect usage can be avoided by just documenting wait_if_running as being useful for multithreaded tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions