Skip to content

Commit 99d2327

Browse files
[3.14] gh-160697: Process the QSBR queue from the eval breaker, not from _PyMem_FreeDelayed() (GH-160698) (#162331)
(cherry picked from commit e0d0f2e) Co-authored-by: Kirill Podoprigora <kirill.bast@gmail.com>
1 parent 8607e1d commit 99d2327

7 files changed

Lines changed: 85 additions & 22 deletions

File tree

‎Include/internal/pycore_ceval.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ _PyEval_SpecialMethodCanSuggest(PyObject *self, int oparg);
328328
#define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
329329
#define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
330330
#define _PY_EVAL_JIT_INVALIDATE_COLD_BIT (1U << 7)
331+
#define _PY_EVAL_PROCESS_DELAYED_BIT (1U << 8)
331332

332333
/* Reserve a few bits for future use */
333-
#define _PY_EVAL_EVENTS_BITS 8
334+
#define _PY_EVAL_EVENTS_BITS 9
334335
#define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
335336

336337
static inline void

‎Include/internal/pycore_qsbr.h‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ 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.
64+
// Unused, kept to preserve the struct layout.
6565
bool should_process;
6666

6767
// Is this thread state allocated?
@@ -127,14 +127,6 @@ _Py_qsbr_advance(struct _qsbr_shared *shared);
127127
extern uint64_t
128128
_Py_qsbr_shared_next(struct _qsbr_shared *shared);
129129

130-
// Return true if deferred memory frees held by QSBR should be processed to
131-
// determine if they can be safely freed.
132-
static inline bool
133-
_Py_qsbr_should_process(struct _qsbr_thread_state *qsbr)
134-
{
135-
return qsbr->should_process;
136-
}
137-
138130
// Have the read sequences advanced to the given goal? If this returns true,
139131
// it safe to reclaim any memory tagged with the goal (or earlier goal).
140132
extern bool
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_object.h" // _PyDebugAllocatorStats() definition
67
#include "pycore_obmalloc.h"
@@ -1218,7 +1219,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size)
12181219
if (size > QSBR_FREE_MEM_LIMIT) {
12191220
qsbr->deferred_count = 0;
12201221
qsbr->deferred_memory = 0;
1221-
qsbr->should_process = true;
1222+
_Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
12221223
return true;
12231224
}
12241225
qsbr->deferred_count++;
@@ -1227,7 +1228,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size)
12271228
qsbr->deferred_memory > QSBR_FREE_MEM_LIMIT) {
12281229
qsbr->deferred_count = 0;
12291230
qsbr->deferred_memory = 0;
1230-
qsbr->should_process = true;
1231+
_Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
12311232
return true;
12321233
}
12331234
return false;
@@ -1305,13 +1306,6 @@ free_delayed(uintptr_t ptr, size_t size)
13051306
buf->array[buf->wr_idx].ptr = ptr;
13061307
buf->array[buf->wr_idx].qsbr_goal = seq;
13071308
buf->wr_idx++;
1308-
1309-
if (buf->wr_idx == WORK_ITEMS_PER_CHUNK) {
1310-
// Normally the processing of delayed items is done from the eval
1311-
// breaker. Processing here is a safety measure to ensure too much
1312-
// work does not accumulate.
1313-
_PyMem_ProcessDelayed((PyThreadState *)tstate);
1314-
}
13151309
#endif
13161310
}
13171311

@@ -1419,7 +1413,7 @@ _PyMem_ProcessDelayed(PyThreadState *tstate)
14191413
PyInterpreterState *interp = tstate->interp;
14201414
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
14211415

1422-
tstate_impl->qsbr->should_process = false;
1416+
_Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PROCESS_DELAYED_BIT);
14231417

14241418
// Process thread-local work
14251419
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
@@ -1388,7 +1388,7 @@ _Py_HandlePending(PyThreadState *tstate)
13881388
_Py_brc_merge_refcounts(tstate);
13891389
}
13901390
/* Process deferred memory frees held by QSBR */
1391-
if (_Py_qsbr_should_process(((_PyThreadStateImpl *)tstate)->qsbr)) {
1391+
if ((breaker & _PY_EVAL_PROCESS_DELAYED_BIT) != 0) {
13921392
_PyMem_ProcessDelayed(tstate);
13931393
}
13941394
#endif

‎Python/instrumentation.c‎

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

20142014
/* We share the eval-breaker with flags, so the monitoring
2015-
* version goes in the top 24 bits */
2015+
* version goes in the bits above _PY_EVAL_EVENTS_BITS */
20162016
#define MONITORING_VERSION_INCREMENT (1 << _PY_EVAL_EVENTS_BITS)
20172017

20182018
int

0 commit comments

Comments
 (0)