Skip to content

Commit fa2f9b6

Browse files
[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 6e9a900) Co-authored-by: John <me@joh.ng>
1 parent 32d59ce commit fa2f9b6

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎Lib/test/test_free_threading/test_gc.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from test import support
1111
from test.support import threading_helper
12+
from test.support.script_helper import assert_python_ok
1213

1314

1415
class MyObj:
@@ -131,6 +132,35 @@ def run(self):
131132

132133
self.assertTrue(C().run())
133134

135+
def test_debug_collectable_python_stderr(self):
136+
# The DEBUG_COLLECTABLE output is written with sys.stderr.write(),
137+
# which may run Python code or wait for another thread (e.g. for the
138+
# lock of a buffered stream), so it must not be written while the
139+
# world is stopped. Debug builds assert that the world is not stopped
140+
# when Python code runs, so use a sys.stderr written in Python.
141+
code = """if 1:
142+
import gc
143+
import io
144+
import sys
145+
146+
class Stderr(io.StringIO):
147+
def write(self, s):
148+
return super().write(s)
149+
150+
sys.stderr = Stderr()
151+
gc.set_debug(gc.DEBUG_COLLECTABLE)
152+
a = []
153+
a.append(a)
154+
del a
155+
gc.collect()
156+
gc.set_debug(0)
157+
output = sys.stderr.getvalue()
158+
sys.stderr = sys.__stderr__
159+
print(output)
160+
"""
161+
_, out, _ = assert_python_ok("-c", code)
162+
self.assertRegex(out, b"gc: collectable <list 0x[0-9A-Fa-f]+>")
163+
134164
def test_merge_brc_queue_of_detached_thread(self):
135165
# GH-157838: objects queued for merging by a thread that is detached
136166
# (blocked in a lock acquire, sleep, etc.) are merged and freed on its
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a deadlock in the garbage collector of the :term:`free-threaded build`
2+
when :data:`gc.DEBUG_COLLECTABLE` is set and another thread is writing to
3+
:data:`sys.stderr`. The debug output is now written after the other threads
4+
are resumed.

‎Python/gc_free_threading.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,18 +2142,22 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
21422142
gc_visit_heaps(interp, &validate_alive_bits, &state->base);
21432143
#endif
21442144

2145+
// The debug output is written with sys.stderr.write(), which may run
2146+
// Python code, so it is printed below after the world is started.
2147+
bool debug_print = (interp->gc.debug & _PyGC_DEBUG_COLLECTABLE) != 0;
2148+
2149+
// Find weakref callbacks we will honor (but do not call them).
2150+
find_weakref_callbacks(state);
2151+
_PyEval_StartTheWorld(interp);
2152+
21452153
// Print debugging information.
2146-
if (interp->gc.debug & _PyGC_DEBUG_COLLECTABLE) {
2154+
if (debug_print) {
21472155
PyObject *op;
21482156
WORKSTACK_FOR_EACH(&state->unreachable, op) {
21492157
debug_cycle("collectable", op);
21502158
}
21512159
}
21522160

2153-
// Find weakref callbacks we will honor (but do not call them).
2154-
find_weakref_callbacks(state);
2155-
_PyEval_StartTheWorld(interp);
2156-
21572161
// Deallocate any object from the refcount merge step
21582162
cleanup_worklist(&state->objs_to_decref);
21592163

0 commit comments

Comments
 (0)