Skip to content

Commit b1e4a24

Browse files
pythongh-76785: Use Pending Calls When Releasing Cross-Interpreter Data (pythongh-109556)
This fixes some crashes in the _xxinterpchannels module, due to a race between interpreters. (cherry picked from commit fd7e08a) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
1 parent e19af2c commit b1e4a24

7 files changed

Lines changed: 98 additions & 67 deletions

File tree

Include/cpython/pystate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
431431
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
432432
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
433433
PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
434+
PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *);
434435

435436
PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
436437

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern void _PyEval_FiniState(struct _ceval_state *ceval);
2626
PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp);
2727
PyAPI_FUNC(int) _PyEval_AddPendingCall(
2828
PyInterpreterState *interp,
29-
int (*func)(void *),
29+
_Py_pending_call_func func,
3030
void *arg,
3131
int mainthreadonly);
3232
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *interp);

Include/internal/pycore_ceval_state.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ extern "C" {
1313
#include "pycore_gil.h" // struct _gil_runtime_state
1414

1515

16+
typedef int (*_Py_pending_call_func)(void *);
17+
1618
struct _pending_calls {
1719
int busy;
1820
PyThread_type_lock lock;
@@ -24,7 +26,7 @@ struct _pending_calls {
2426
int async_exc;
2527
#define NPENDINGCALLS 32
2628
struct _pending_call {
27-
int (*func)(void *);
29+
_Py_pending_call_func func;
2830
void *arg;
2931
} calls[NPENDINGCALLS];
3032
int first;

Modules/_xxinterpchannelsmodule.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,34 @@ add_new_type(PyObject *mod, PyType_Spec *spec, crossinterpdatafunc shared)
161161
return cls;
162162
}
163163

164+
#define XID_IGNORE_EXC 1
165+
#define XID_FREE 2
166+
164167
static int
165-
_release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
168+
_release_xid_data(_PyCrossInterpreterData *data, int flags)
166169
{
170+
int ignoreexc = flags & XID_IGNORE_EXC;
167171
PyObject *exc;
168172
if (ignoreexc) {
169173
exc = PyErr_GetRaisedException();
170174
}
171-
int res = _PyCrossInterpreterData_Release(data);
175+
int res;
176+
if (flags & XID_FREE) {
177+
res = _PyCrossInterpreterData_ReleaseAndRawFree(data);
178+
}
179+
else {
180+
res = _PyCrossInterpreterData_Release(data);
181+
}
172182
if (res < 0) {
173183
/* The owning interpreter is already destroyed. */
174184
if (ignoreexc) {
175185
// XXX Emit a warning?
176186
PyErr_Clear();
177187
}
178188
}
189+
if (flags & XID_FREE) {
190+
/* Either way, we free the data. */
191+
}
179192
if (ignoreexc) {
180193
PyErr_SetRaisedException(exc);
181194
}
@@ -367,9 +380,8 @@ static void
367380
_channelitem_clear(_channelitem *item)
368381
{
369382
if (item->data != NULL) {
370-
(void)_release_xid_data(item->data, 1);
371383
// It was allocated in _channel_send().
372-
GLOBAL_FREE(item->data);
384+
(void)_release_xid_data(item->data, XID_IGNORE_EXC & XID_FREE);
373385
item->data = NULL;
374386
}
375387
item->next = NULL;
@@ -1440,14 +1452,12 @@ _channel_recv(_channels *channels, int64_t id, PyObject **res)
14401452
PyObject *obj = _PyCrossInterpreterData_NewObject(data);
14411453
if (obj == NULL) {
14421454
assert(PyErr_Occurred());
1443-
(void)_release_xid_data(data, 1);
1444-
// It was allocated in _channel_send().
1445-
GLOBAL_FREE(data);
1455+
// It was allocated in _channel_send(), so we free it.
1456+
(void)_release_xid_data(data, XID_IGNORE_EXC | XID_FREE);
14461457
return -1;
14471458
}
1448-
int release_res = _release_xid_data(data, 0);
1449-
// It was allocated in _channel_send().
1450-
GLOBAL_FREE(data);
1459+
// It was allocated in _channel_send(), so we free it.
1460+
int release_res = _release_xid_data(data, XID_FREE);
14511461
if (release_res < 0) {
14521462
// The source interpreter has been destroyed already.
14531463
assert(PyErr_Occurred());

Modules/_xxsubinterpretersmodule.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,17 @@ add_new_exception(PyObject *mod, const char *name, PyObject *base)
6060
add_new_exception(MOD, MODULE_NAME "." Py_STRINGIFY(NAME), BASE)
6161

6262
static int
63-
_release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
63+
_release_xid_data(_PyCrossInterpreterData *data)
6464
{
65-
PyObject *exc;
66-
if (ignoreexc) {
67-
exc = PyErr_GetRaisedException();
68-
}
65+
PyObject *exc = PyErr_GetRaisedException();
6966
int res = _PyCrossInterpreterData_Release(data);
7067
if (res < 0) {
7168
/* The owning interpreter is already destroyed. */
7269
_PyCrossInterpreterData_Clear(NULL, data);
73-
if (ignoreexc) {
74-
// XXX Emit a warning?
75-
PyErr_Clear();
76-
}
77-
}
78-
if (ignoreexc) {
79-
PyErr_SetRaisedException(exc);
70+
// XXX Emit a warning?
71+
PyErr_Clear();
8072
}
73+
PyErr_SetRaisedException(exc);
8174
return res;
8275
}
8376

@@ -147,7 +140,7 @@ _sharednsitem_clear(struct _sharednsitem *item)
147140
PyMem_RawFree((void *)item->name);
148141
item->name = NULL;
149142
}
150-
(void)_release_xid_data(&item->data, 1);
143+
(void)_release_xid_data(&item->data);
151144
}
152145

153146
static int
@@ -176,16 +169,16 @@ typedef struct _sharedns {
176169
static _sharedns *
177170
_sharedns_new(Py_ssize_t len)
178171
{
179-
_sharedns *shared = PyMem_NEW(_sharedns, 1);
172+
_sharedns *shared = PyMem_RawCalloc(sizeof(_sharedns), 1);
180173
if (shared == NULL) {
181174
PyErr_NoMemory();
182175
return NULL;
183176
}
184177
shared->len = len;
185-
shared->items = PyMem_NEW(struct _sharednsitem, len);
178+
shared->items = PyMem_RawCalloc(sizeof(struct _sharednsitem), len);
186179
if (shared->items == NULL) {
187180
PyErr_NoMemory();
188-
PyMem_Free(shared);
181+
PyMem_RawFree(shared);
189182
return NULL;
190183
}
191184
return shared;
@@ -197,8 +190,8 @@ _sharedns_free(_sharedns *shared)
197190
for (Py_ssize_t i=0; i < shared->len; i++) {
198191
_sharednsitem_clear(&shared->items[i]);
199192
}
200-
PyMem_Free(shared->items);
201-
PyMem_Free(shared);
193+
PyMem_RawFree(shared->items);
194+
PyMem_RawFree(shared);
202195
}
203196

204197
static _sharedns *

Python/ceval_gil.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ _PyEval_SignalReceived(PyInterpreterState *interp)
757757
/* Push one item onto the queue while holding the lock. */
758758
static int
759759
_push_pending_call(struct _pending_calls *pending,
760-
int (*func)(void *), void *arg)
760+
_Py_pending_call_func func, void *arg)
761761
{
762762
int i = pending->last;
763763
int j = (i + 1) % NPENDINGCALLS;
@@ -804,7 +804,7 @@ _pop_pending_call(struct _pending_calls *pending,
804804

805805
int
806806
_PyEval_AddPendingCall(PyInterpreterState *interp,
807-
int (*func)(void *), void *arg,
807+
_Py_pending_call_func func, void *arg,
808808
int mainthreadonly)
809809
{
810810
assert(!mainthreadonly || _Py_IsMainInterpreter(interp));
@@ -828,7 +828,7 @@ _PyEval_AddPendingCall(PyInterpreterState *interp,
828828
}
829829

830830
int
831-
Py_AddPendingCall(int (*func)(void *), void *arg)
831+
Py_AddPendingCall(_Py_pending_call_func func, void *arg)
832832
{
833833
/* Legacy users of this API will continue to target the main thread
834834
(of the main interpreter). */
@@ -872,7 +872,7 @@ _make_pending_calls(struct _pending_calls *pending)
872872
{
873873
/* perform a bounded number of calls, in case of recursion */
874874
for (int i=0; i<NPENDINGCALLS; i++) {
875-
int (*func)(void *) = NULL;
875+
_Py_pending_call_func func = NULL;
876876
void *arg = NULL;
877877

878878
/* pop one item off the queue while holding the lock */

Python/pystate.c

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,10 +2292,16 @@ _xidata_init(_PyCrossInterpreterData *data)
22922292
static inline void
22932293
_xidata_clear(_PyCrossInterpreterData *data)
22942294
{
2295-
if (data->free != NULL) {
2296-
data->free(data->data);
2295+
// _PyCrossInterpreterData only has two members that need to be
2296+
// cleaned up, if set: "data" must be freed and "obj" must be decref'ed.
2297+
// In both cases the original (owning) interpreter must be used,
2298+
// which is the caller's responsibility to ensure.
2299+
if (data->data != NULL) {
2300+
if (data->free != NULL) {
2301+
data->free(data->data);
2302+
}
2303+
data->data = NULL;
22972304
}
2298-
data->data = NULL;
22992305
Py_CLEAR(data->obj);
23002306
}
23012307

@@ -2440,40 +2446,32 @@ _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
24402446
return data->new_object(data);
24412447
}
24422448

2443-
typedef void (*releasefunc)(PyInterpreterState *, void *);
2444-
2445-
static void
2446-
_call_in_interpreter(PyInterpreterState *interp, releasefunc func, void *arg)
2449+
static int
2450+
_release_xidata_pending(void *data)
24472451
{
2448-
/* We would use Py_AddPendingCall() if it weren't specific to the
2449-
* main interpreter (see bpo-33608). In the meantime we take a
2450-
* naive approach.
2451-
*/
2452-
_PyRuntimeState *runtime = interp->runtime;
2453-
PyThreadState *save_tstate = NULL;
2454-
if (interp != current_fast_get(runtime)->interp) {
2455-
// XXX Using the "head" thread isn't strictly correct.
2456-
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
2457-
// XXX Possible GILState issues?
2458-
save_tstate = _PyThreadState_Swap(runtime, tstate);
2459-
}
2460-
2461-
// XXX Once the GIL is per-interpreter, this should be called with the
2462-
// calling interpreter's GIL released and the target interpreter's held.
2463-
func(interp, arg);
2452+
_xidata_clear((_PyCrossInterpreterData *)data);
2453+
return 0;
2454+
}
24642455

2465-
// Switch back.
2466-
if (save_tstate != NULL) {
2467-
_PyThreadState_Swap(runtime, save_tstate);
2468-
}
2456+
static int
2457+
_xidata_release_and_rawfree_pending(void *data)
2458+
{
2459+
_xidata_clear((_PyCrossInterpreterData *)data);
2460+
PyMem_RawFree(data);
2461+
return 0;
24692462
}
24702463

2471-
int
2472-
_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
2464+
static int
2465+
_xidata_release(_PyCrossInterpreterData *data, int rawfree)
24732466
{
2474-
if (data->free == NULL && data->obj == NULL) {
2467+
if ((data->data == NULL || data->free == NULL) && data->obj == NULL) {
24752468
// Nothing to release!
2476-
data->data = NULL;
2469+
if (rawfree) {
2470+
PyMem_RawFree(data);
2471+
}
2472+
else {
2473+
data->data = NULL;
2474+
}
24772475
return 0;
24782476
}
24792477

@@ -2484,15 +2482,42 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
24842482
// This function shouldn't have been called.
24852483
// XXX Someone leaked some memory...
24862484
assert(PyErr_Occurred());
2485+
if (rawfree) {
2486+
PyMem_RawFree(data);
2487+
}
24872488
return -1;
24882489
}
24892490

24902491
// "Release" the data and/or the object.
2491-
_call_in_interpreter(interp,
2492-
(releasefunc)_PyCrossInterpreterData_Clear, data);
2492+
if (interp == current_fast_get(interp->runtime)->interp) {
2493+
_xidata_clear(data);
2494+
if (rawfree) {
2495+
PyMem_RawFree(data);
2496+
}
2497+
}
2498+
else {
2499+
_Py_pending_call_func func = _release_xidata_pending;
2500+
if (rawfree) {
2501+
func = _xidata_release_and_rawfree_pending;
2502+
}
2503+
// XXX Emit a warning if this fails?
2504+
_PyEval_AddPendingCall(interp, func, data, 0);
2505+
}
24932506
return 0;
24942507
}
24952508

2509+
int
2510+
_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
2511+
{
2512+
return _xidata_release(data, 0);
2513+
}
2514+
2515+
int
2516+
_PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *data)
2517+
{
2518+
return _xidata_release(data, 1);
2519+
}
2520+
24962521
/* registry of {type -> crossinterpdatafunc} */
24972522

24982523
/* For now we use a global registry of shareable classes. An

0 commit comments

Comments
 (0)