https://github.com/python/cpython/commit/32d59ce356f0c885d09d0c2ee711e542bdccd3e3 commit: 32d59ce356f0c885d09d0c2ee711e542bdccd3e3 branch: 3.15 author: Kumar Aditya <[email protected]> committer: kumaraditya303 <[email protected]> date: 2026-11-15T13:01:20+05:30 summary:
[3.15] gh-160682: Track running function objects in free-threaded GC …(GH-160707) (#162005) [3.15] gh-160682: Track running function objects in free-threaded GC (GH-160707) (cherry picked from commit b3683c0ffdbed7e055b4b3196775eabae3d6f671) Co-authored-by: Vankudothu Adhi Naik <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst M Lib/test/test_free_threading/test_gc.py M Python/gc_free_threading.c diff --git a/Lib/test/test_free_threading/test_gc.py b/Lib/test/test_free_threading/test_gc.py index 7adc2a86db3f26..f8dfedc725be3c 100644 --- a/Lib/test/test_free_threading/test_gc.py +++ b/Lib/test/test_free_threading/test_gc.py @@ -118,6 +118,19 @@ def mutate(): b.wait() time.sleep(0.2) + def test_running_method_not_collected(self): + 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()) + def test_merge_brc_queue_of_detached_thread(self): # GH-157838: objects queued for merging by a thread that is detached # (blocked in a lock acquire, sleep, etc.) are merged and freed on its diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst new file mode 100644 index 00000000000000..5c783665e78965 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst @@ -0,0 +1,2 @@ +Fix a bug in the :term:`free-threaded build` where garbage collection could +prematurely collect the function object of a running method. diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 4babd7600c4fa3..d41977ada6619f 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -469,6 +469,7 @@ gc_visit_thread_stacks(PyInterpreterState *interp, struct collection_state *stat continue; } + gc_visit_stackref(f->f_funcobj); gc_visit_stackref(f->f_executable); while (top != f->localsplus) { --top; @@ -865,6 +866,10 @@ gc_visit_thread_stacks_mark_alive(PyInterpreterState *interp, gc_mark_args_t *ar } _PyStackRef *top = f->stackpointer; + if (gc_visit_stackref_mark_alive(args, f->f_funcobj) < 0) { + err = -1; + goto exit; + } if (gc_visit_stackref_mark_alive(args, f->f_executable) < 0) { err = -1; goto exit; _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
