https://github.com/python/cpython/commit/fa2f9b61af009d53fddd12e6788646cf823fe394 commit: fa2f9b61af009d53fddd12e6788646cf823fe394 branch: 3.15 author: Kumar Aditya <[email protected]> committer: kumaraditya303 <[email protected]> date: 2026-11-15T08:22:53Z summary:
[3.15] gh-161366: Fix deadlock with FT GC debug collectables print (G… (#162007) [3.15] gh-161366: Fix deadlock with FT GC debug collectables print (GH-161444) (cherry picked from commit 6e9a900916e32f4565f0ecebf7b6f48f5a60bc66) Co-authored-by: John <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-11-05-21-00-57.gh-issue-161366.ouxAvv.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 f8dfedc725be3c5..100dcb989035970 100644 --- a/Lib/test/test_free_threading/test_gc.py +++ b/Lib/test/test_free_threading/test_gc.py @@ -9,6 +9,7 @@ from test import support from test.support import threading_helper +from test.support.script_helper import assert_python_ok class MyObj: @@ -131,6 +132,35 @@ def run(self): self.assertTrue(C().run()) + def test_debug_collectable_python_stderr(self): + # The DEBUG_COLLECTABLE output is written with sys.stderr.write(), + # which may run Python code or wait for another thread (e.g. for the + # lock of a buffered stream), so it must not be written while the + # world is stopped. Debug builds assert that the world is not stopped + # when Python code runs, so use a sys.stderr written in Python. + code = """if 1: + import gc + import io + import sys + + class Stderr(io.StringIO): + def write(self, s): + return super().write(s) + + sys.stderr = Stderr() + gc.set_debug(gc.DEBUG_COLLECTABLE) + a = [] + a.append(a) + del a + gc.collect() + gc.set_debug(0) + output = sys.stderr.getvalue() + sys.stderr = sys.__stderr__ + print(output) + """ + _, out, _ = assert_python_ok("-c", code) + self.assertRegex(out, b"gc: collectable <list 0x[0-9A-Fa-f]+>") + 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-11-05-21-00-57.gh-issue-161366.ouxAvv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-05-21-00-57.gh-issue-161366.ouxAvv.rst new file mode 100644 index 000000000000000..94098a0cf5195cb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-05-21-00-57.gh-issue-161366.ouxAvv.rst @@ -0,0 +1,4 @@ +Fix a deadlock in the garbage collector of the :term:`free-threaded build` +when :data:`gc.DEBUG_COLLECTABLE` is set and another thread is writing to +:data:`sys.stderr`. The debug output is now written after the other threads +are resumed. diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index d41977ada6619f4..5e1ceaa447bf1ee 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2142,18 +2142,22 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, gc_visit_heaps(interp, &validate_alive_bits, &state->base); #endif + // The debug output is written with sys.stderr.write(), which may run + // Python code, so it is printed below after the world is started. + bool debug_print = (interp->gc.debug & _PyGC_DEBUG_COLLECTABLE) != 0; + + // Find weakref callbacks we will honor (but do not call them). + find_weakref_callbacks(state); + _PyEval_StartTheWorld(interp); + // Print debugging information. - if (interp->gc.debug & _PyGC_DEBUG_COLLECTABLE) { + if (debug_print) { PyObject *op; WORKSTACK_FOR_EACH(&state->unreachable, op) { debug_cycle("collectable", op); } } - // Find weakref callbacks we will honor (but do not call them). - find_weakref_callbacks(state); - _PyEval_StartTheWorld(interp); - // Deallocate any object from the refcount merge step cleanup_worklist(&state->objs_to_decref); _______________________________________________ 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]
