Skip to content

Commit e0d0f2e

Browse files
authored
gh-160697: Process the QSBR queue from the eval breaker, not from _PyMem_FreeDelayed() (#160698)
1 parent 1782f62 commit e0d0f2e

8 files changed

Lines changed: 93 additions & 31 deletions

File tree

‎Include/internal/pycore_ceval.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,10 @@ _PyEval_SpecialMethodCanSuggest(PyObject *self, int oparg);
350350
#define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
351351
#define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
352352
#define _PY_EVAL_JIT_INVALIDATE_COLD_BIT (1U << 7)
353+
#define _PY_EVAL_PROCESS_DELAYED_BIT (1U << 8)
353354

354355
/* Reserve a few bits for future use */
355-
#define _PY_EVAL_EVENTS_BITS 8
356+
#define _PY_EVAL_EVENTS_BITS 9
356357
#define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
357358

358359
static inline void

‎Include/internal/pycore_qsbr.h‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ struct _qsbr_thread_state {
6161
// and in a different thread.
6262
size_t deferred_page_memory;
6363

64-
// True if the deferred memory frees should be processed.
65-
bool should_process;
66-
6764
// Is this thread state allocated?
6865
bool allocated;
6966
struct _qsbr_thread_state *freelist_next;
@@ -142,14 +139,6 @@ _Py_qsbr_advance(struct _qsbr_shared *shared);
142139
extern uint64_t
143140
_Py_qsbr_shared_next(struct _qsbr_shared *shared);
144141

145-
// Return true if deferred memory frees held by QSBR should be processed to
146-
// determine if they can be safely freed.
147-
static inline bool
148-
_Py_qsbr_should_process(struct _qsbr_thread_state *qsbr)
149-
{
150-
return qsbr->should_process;
151-
}
152-
153142
// Have the read sequences advanced to the given goal? If this returns true,
154143
// it safe to reclaim any memory tagged with the goal (or earlier goal).
155144
extern bool

‎InternalDocs/qsbr.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,22 @@ its last advance:
116116
is larger than `QSBR_FREE_MEM_LIMIT` (1 MiB), when the accumulated deferred
117117
memory exceeds that limit, or when the count exceeds `QSBR_DEFERRED_LIMIT`
118118
(127, sized so a chunk of work items is processed before it overflows).
119-
Crossing any of these thresholds also sets a per-thread `should_process`
120-
flag, signalling that the deferred-free list should be drained.
119+
Crossing any of these thresholds also sets the thread's
120+
`_PY_EVAL_PROCESS_DELAYED_BIT` eval breaker bit, signalling that the
121+
deferred-free list should be drained.
121122

122123
* For mimalloc pages held by QSBR, the thread tracks `deferred_page_memory`
123124
and advances `wr_seq` when either the individual page or the accumulated
124125
page memory exceeds `QSBR_PAGE_MEM_LIMIT` (4096 * 20 bytes). Advancing
125126
promptly here matters because a held page cannot be reused for a different
126127
size class or by a different thread.
127128

128-
Processing of the deferred-free list normally happens from the eval breaker
129-
(rather than from inside `_PyMem_FreeDelayed`), which gives the global
130-
`rd_seq` a better chance to have advanced far enough that items can actually
131-
be freed. `_PyMem_ProcessDelayed` is still called from the free path as a
132-
safety valve when a work-item chunk fills up.
129+
Processing of the deferred-free list happens from the eval breaker rather
130+
than from inside `_PyMem_FreeDelayed`. This gives the global `rd_seq` a better
131+
chance to have advanced far enough that items can actually be freed. It is
132+
also required for correctness: items queued by `_PyObject_XDecRefDelayed` may
133+
run arbitrary code when they are released, and the callers of
134+
`_PyMem_FreeDelayed` may be in the middle of mutating a dict, list or type.
133135

134136
This optimization improves runtime speed but may increase peak memory usage
135137
by slightly delaying when memory can be reclaimed; the size-based thresholds
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import threading
2+
import unittest
3+
4+
from test.support import threading_helper
5+
6+
7+
class Holder:
8+
pass
9+
10+
11+
def make_shared(func, *args):
12+
t = threading.Thread(target=func, args=args)
13+
t.start()
14+
t.join()
15+
16+
17+
@threading_helper.requires_working_threading()
18+
class TestDelayedFreeReentrancy(unittest.TestCase):
19+
ROUNDS = 1000
20+
21+
def queue_finalizers(self, callback, count=ROUNDS):
22+
class Finalizer:
23+
def __del__(self):
24+
callback()
25+
26+
holders = [Holder() for _ in range(count)]
27+
for h in holders:
28+
h.__dict__ = {"f": Finalizer()}
29+
return holders
30+
31+
def test_dict_resize(self):
32+
d = {}
33+
make_shared(d.get, 0)
34+
holders = self.queue_finalizers(lambda: d.__setitem__(object(), 0),
35+
count=100)
36+
for h in holders:
37+
h.__dict__ = {}
38+
for _ in range(self.ROUNDS):
39+
for k in list(d):
40+
del d[k]
41+
for i in range(6):
42+
d[i] = i
43+
self.assertEqual(len(d), sum(1 for _ in d))
44+
45+
def test_list_resize(self):
46+
L = []
47+
make_shared(L.append, 0)
48+
holders = self.queue_finalizers(L.clear)
49+
for n, h in enumerate(holders):
50+
h.__dict__ = {}
51+
if n % 2:
52+
h.__dict__ = {}
53+
L.clear()
54+
for i in range(9):
55+
L.insert(len(L), i)
56+
self.assertEqual(len(L), len(list(L)))
57+
58+
def test_type_cache_invalidation(self):
59+
class C:
60+
x = None
61+
62+
holders = self.queue_finalizers(lambda: C.x)
63+
for h in holders:
64+
h.__dict__ = {}
65+
C.x
66+
new = object()
67+
C.x = new
68+
self.assertIs(C.x, new)
69+
70+
71+
if __name__ == "__main__":
72+
unittest.main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix crashes and hangs in the :term:`free-threaded build` when a destructor
2+
ran in the middle of a :class:`dict` resize, a :class:`list` resize or a type
3+
cache update. The thread's queue of delayed frees is now processed only from
4+
the eval breaker, instead of synchronously when the queue fills up.

‎Objects/obmalloc.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Python's malloc wrappers (see pymem.h) */
22

33
#include "Python.h"
4+
#include "pycore_ceval.h"
45
#include "pycore_interp.h" // _PyInterpreterState_HasFeature
56
#include "pycore_mmap.h" // _PyAnnotateMemoryMap()
67
#include "pycore_object.h" // _PyDebugAllocatorStats() definition
@@ -1424,7 +1425,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size)
14241425
if (size > QSBR_FREE_MEM_LIMIT) {
14251426
qsbr->deferred_count = 0;
14261427
qsbr->deferred_memory = 0;
1427-
qsbr->should_process = true;
1428+
_Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
14281429
return true;
14291430
}
14301431
qsbr->deferred_count++;
@@ -1433,7 +1434,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size)
14331434
qsbr->deferred_memory > QSBR_FREE_MEM_LIMIT) {
14341435
qsbr->deferred_count = 0;
14351436
qsbr->deferred_memory = 0;
1436-
qsbr->should_process = true;
1437+
_Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
14371438
return true;
14381439
}
14391440
return false;
@@ -1511,13 +1512,6 @@ free_delayed(uintptr_t ptr, size_t size)
15111512
buf->array[buf->wr_idx].ptr = ptr;
15121513
buf->array[buf->wr_idx].qsbr_goal = seq;
15131514
buf->wr_idx++;
1514-
1515-
if (buf->wr_idx == WORK_ITEMS_PER_CHUNK) {
1516-
// Normally the processing of delayed items is done from the eval
1517-
// breaker. Processing here is a safety measure to ensure too much
1518-
// work does not accumulate.
1519-
_PyMem_ProcessDelayed((PyThreadState *)tstate);
1520-
}
15211515
#endif
15221516
}
15231517

@@ -1625,7 +1619,7 @@ _PyMem_ProcessDelayed(PyThreadState *tstate)
16251619
PyInterpreterState *interp = tstate->interp;
16261620
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
16271621

1628-
tstate_impl->qsbr->should_process = false;
1622+
_Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
16291623

16301624
// Process thread-local work
16311625
process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, NULL, NULL);

‎Python/ceval_gil.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ _Py_HandlePending(PyThreadState *tstate)
13871387
_Py_brc_merge_refcounts(tstate);
13881388
}
13891389
/* Process deferred memory frees held by QSBR */
1390-
if (_Py_qsbr_should_process(((_PyThreadStateImpl *)tstate)->qsbr)) {
1390+
if ((breaker & _PY_EVAL_PROCESS_DELAYED_BIT) != 0) {
13911391
_PyMem_ProcessDelayed(tstate);
13921392
}
13931393
#endif

‎Python/instrumentation.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ check_tool(PyInterpreterState *interp, int tool_id)
20412041
}
20422042

20432043
/* We share the eval-breaker with flags, so the monitoring
2044-
* version goes in the top 24 bits */
2044+
* version goes in the bits above _PY_EVAL_EVENTS_BITS */
20452045
#define MONITORING_VERSION_INCREMENT (1 << _PY_EVAL_EVENTS_BITS)
20462046

20472047
int

0 commit comments

Comments
 (0)