From 122ac6c439194363dd140b733a43d690e59e4f61 Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Sat, 18 Oct 2025 21:30:05 +0300 Subject: [PATCH 1/5] gh-140306: Fix memory leaks in cross-interpreter data handling --- .../2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst | 2 ++ Modules/_interpchannelsmodule.c | 2 +- Python/crossinterp.c | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst new file mode 100644 index 00000000000000..ede30f08f8084b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst @@ -0,0 +1,2 @@ +Fix memory leaks in cross-interpreter channel operations and shared +namespace handling diff --git a/Modules/_interpchannelsmodule.c b/Modules/_interpchannelsmodule.c index 274bfacfed874b..ef9cf01ecbec5e 100644 --- a/Modules/_interpchannelsmodule.c +++ b/Modules/_interpchannelsmodule.c @@ -580,7 +580,7 @@ _channelitem_clear_data(_channelitem *item, int removed) { if (item->data != NULL) { // It was allocated in channel_send(). - (void)_release_xid_data(item->data, XID_IGNORE_EXC & XID_FREE); + (void)_release_xid_data(item->data, XID_IGNORE_EXC | XID_FREE); item->data = NULL; } diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 16a23f0351cd26..be7113ea8a013e 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1153,8 +1153,8 @@ _release_xid_data(_PyXIData_t *xidata, int rawfree) { PyObject *exc = PyErr_GetRaisedException(); int res = rawfree - ? _PyXIData_Release(xidata) - : _PyXIData_ReleaseAndRawFree(xidata); + ? _PyXIData_ReleaseAndRawFree(xidata) + : _PyXIData_Release(xidata); if (res < 0) { /* The owning interpreter is already destroyed. */ _PyXIData_Clear(NULL, xidata); @@ -1814,6 +1814,7 @@ _PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj) const char *msg = _copy_string_obj_raw(msgobj, NULL); Py_DECREF(msgobj); if (PyErr_Occurred()) { + PyMem_RawFree((void *)msg); return -1; } *failure = (_PyXI_failure){ From 2eca13cf0afd0632efb8aca982ad5dd75978cabc Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Sun, 19 Oct 2025 01:16:28 +0300 Subject: [PATCH 2/5] Fix bitwise operation in _queueitem_clear_data --- Modules/_interpqueuesmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_interpqueuesmodule.c b/Modules/_interpqueuesmodule.c index 1db08628d29e01..417c5fbcee2645 100644 --- a/Modules/_interpqueuesmodule.c +++ b/Modules/_interpqueuesmodule.c @@ -436,7 +436,7 @@ _queueitem_clear_data(_queueitem *item) return; } // It was allocated in queue_put(). - (void)_release_xid_data(item->data, XID_IGNORE_EXC & XID_FREE); + (void)_release_xid_data(item->data, XID_IGNORE_EXC | XID_FREE); item->data = NULL; } From f67f2511831ffb0114e8b789eaf7f0ceabb46d29 Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Sun, 19 Oct 2025 12:34:03 +0300 Subject: [PATCH 3/5] add period --- .../2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst index ede30f08f8084b..2178c4960636cb 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-21-29-45.gh-issue-140306.xS5CcS.rst @@ -1,2 +1,2 @@ Fix memory leaks in cross-interpreter channel operations and shared -namespace handling +namespace handling. From f8855231a7497fb731bc782ee2a86847f1dd4a4b Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Sun, 19 Oct 2025 13:19:33 +0300 Subject: [PATCH 4/5] fix --- Python/crossinterp.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Python/crossinterp.c b/Python/crossinterp.c index be7113ea8a013e..1e6338d5977f05 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1805,6 +1805,15 @@ _PyXI_InitFailureUTF8(_PyXI_failure *failure, int _PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj) { + if (obj == NULL) { + *failure = (_PyXI_failure){ + .code = code, + .msg = NULL, + .msg_owned = 0, + }; + return 0; + } + PyObject *msgobj = PyObject_Str(obj); if (msgobj == NULL) { return -1; @@ -1813,8 +1822,7 @@ _PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj) // That happens automatically in _capture_current_exception(). const char *msg = _copy_string_obj_raw(msgobj, NULL); Py_DECREF(msgobj); - if (PyErr_Occurred()) { - PyMem_RawFree((void *)msg); + if (msg == NULL) { return -1; } *failure = (_PyXI_failure){ From ea1546bae17c1489bc34f291bc7b3f619d3dfcbf Mon Sep 17 00:00:00 2001 From: Shamil Date: Sun, 19 Oct 2025 14:06:45 +0300 Subject: [PATCH 5/5] Update Python/crossinterp.c Co-authored-by: Serhiy Storchaka --- Python/crossinterp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 1e6338d5977f05..542253c14de9b8 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1805,12 +1805,12 @@ _PyXI_InitFailureUTF8(_PyXI_failure *failure, int _PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj) { + *failure = (_PyXI_failure){ + .code = code, + .msg = NULL, + .msg_owned = 0, + }; if (obj == NULL) { - *failure = (_PyXI_failure){ - .code = code, - .msg = NULL, - .msg_owned = 0, - }; return 0; }