Skip to content

free-threading GC does not properly track references to a running function #160682

Closed

Description

@johng

Bug report

def test_running_method_not_collected(self):
        # The class dict holds the only counted reference to the method.
        # The running frame's deferred reference must keep it alive.
        class C:
            def m(self):
                wr = weakref.ref(C.__dict__['m'])
                del C.m
                gc.collect()
                return wr() is not None

            def run(self):
                return self.m()

        self.assertTrue(C().run())

In the above gc.collect() cleans up C.m however m is still running in an instance so should not be cleaned up. The above is a bit of a contrived example but it shows the gap reliably.

In a more real world example this could show up in more common code with monkey patching, importlib.reload, repls. It should be a small fix to ensure frame->f_funcobj is properly accounted for in free threading GC.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 25, 2026
  2. changed the title [-]free-threading GC does properly track references to a running function[/-] [+]free-threading GC does not properly track references to a running function[/+] on Oct 26, 2026
  3. added 2 commits that reference this issue on Oct 26, 2026
  4. added a commit that references this issue on Oct 26, 2026
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions