Skip to content

Commit f932321

Browse files
authored
gh-140306: Fix memory leaks in cross-interpreter data handling (GH-140307)
1 parent bad8d6d commit f932321

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leaks in cross-interpreter channel operations and shared
2+
namespace handling.

Modules/_interpchannelsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ _channelitem_clear_data(_channelitem *item, int removed)
580580
{
581581
if (item->data != NULL) {
582582
// It was allocated in channel_send().
583-
(void)_release_xid_data(item->data, XID_IGNORE_EXC & XID_FREE);
583+
(void)_release_xid_data(item->data, XID_IGNORE_EXC | XID_FREE);
584584
item->data = NULL;
585585
}
586586

Modules/_interpqueuesmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ _queueitem_clear_data(_queueitem *item)
436436
return;
437437
}
438438
// It was allocated in queue_put().
439-
(void)_release_xid_data(item->data, XID_IGNORE_EXC & XID_FREE);
439+
(void)_release_xid_data(item->data, XID_IGNORE_EXC | XID_FREE);
440440
item->data = NULL;
441441
}
442442

Python/crossinterp.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,8 @@ _release_xid_data(_PyXIData_t *xidata, int rawfree)
11531153
{
11541154
PyObject *exc = PyErr_GetRaisedException();
11551155
int res = rawfree
1156-
? _PyXIData_Release(xidata)
1157-
: _PyXIData_ReleaseAndRawFree(xidata);
1156+
? _PyXIData_ReleaseAndRawFree(xidata)
1157+
: _PyXIData_Release(xidata);
11581158
if (res < 0) {
11591159
/* The owning interpreter is already destroyed. */
11601160
_PyXIData_Clear(NULL, xidata);
@@ -1805,6 +1805,15 @@ _PyXI_InitFailureUTF8(_PyXI_failure *failure,
18051805
int
18061806
_PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj)
18071807
{
1808+
*failure = (_PyXI_failure){
1809+
.code = code,
1810+
.msg = NULL,
1811+
.msg_owned = 0,
1812+
};
1813+
if (obj == NULL) {
1814+
return 0;
1815+
}
1816+
18081817
PyObject *msgobj = PyObject_Str(obj);
18091818
if (msgobj == NULL) {
18101819
return -1;
@@ -1813,7 +1822,7 @@ _PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj)
18131822
// That happens automatically in _capture_current_exception().
18141823
const char *msg = _copy_string_obj_raw(msgobj, NULL);
18151824
Py_DECREF(msgobj);
1816-
if (PyErr_Occurred()) {
1825+
if (msg == NULL) {
18171826
return -1;
18181827
}
18191828
*failure = (_PyXI_failure){

0 commit comments

Comments
 (0)