Skip to content

Commit 0fb8263

Browse files
committed
Fix free-threaded build
1 parent 7b12a41 commit 0fb8263

5 files changed

Lines changed: 85 additions & 6 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
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):
22+
class Finalizer:
23+
def __del__(self):
24+
callback()
25+
26+
holders = [Holder() for _ in range(self.ROUNDS)]
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+
for n, h in enumerate(holders):
36+
h.__dict__ = {}
37+
if n % 2:
38+
h.__dict__ = {}
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), len(list(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 & 4 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
@@ -1513,10 +1514,8 @@ free_delayed(uintptr_t ptr, size_t size)
15131514
buf->wr_idx++;
15141515

15151516
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);
1517+
_Py_set_eval_breaker_bit((PyThreadState *)tstate,
1518+
_PY_EVAL_PROCESS_DELAYED_BIT);
15201519
}
15211520
#endif
15221521
}
@@ -1626,6 +1625,7 @@ _PyMem_ProcessDelayed(PyThreadState *tstate)
16261625
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
16271626

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

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

‎Python/ceval_gil.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,9 @@ _Py_HandlePending(PyThreadState *tstate)
13891389
_Py_brc_merge_refcounts(tstate);
13901390
}
13911391
/* Process deferred memory frees held by QSBR */
1392-
if (_Py_qsbr_should_process(((_PyThreadStateImpl *)tstate)->qsbr)) {
1392+
if ((breaker & _PY_EVAL_PROCESS_DELAYED_BIT) != 0 ||
1393+
_Py_qsbr_should_process(((_PyThreadStateImpl *)tstate)->qsbr))
1394+
{
13931395
_PyMem_ProcessDelayed(tstate);
13941396
}
13951397
#endif

0 commit comments

Comments
 (0)