From aa5ed69a2957406428f87061f049a7654700ce61 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Thu, 18 Jun 2026 17:43:11 +0800 Subject: [PATCH 1/3] Fix missing memory errors in _interpqueuesmodule.c --- .../2026-06-18-17-31-02.gh-issue-151126.HubUYi.rst | 3 +++ Modules/_interpqueuesmodule.c | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-18-17-31-02.gh-issue-151126.HubUYi.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-18-17-31-02.gh-issue-151126.HubUYi.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-18-17-31-02.gh-issue-151126.HubUYi.rst new file mode 100644 index 00000000000000..6714451b45fb1f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-18-17-31-02.gh-issue-151126.HubUYi.rst @@ -0,0 +1,3 @@ +Fix crashes when interpreter queues run out of memory while being listed or +shared between interpreters. These cases now raise a proper +:exc:`MemoryError`. diff --git a/Modules/_interpqueuesmodule.c b/Modules/_interpqueuesmodule.c index 9979cd3457e101..c900c0256efc18 100644 --- a/Modules/_interpqueuesmodule.c +++ b/Modules/_interpqueuesmodule.c @@ -1045,6 +1045,7 @@ _queues_list_all(_queues *queues, int64_t *p_count) struct queue_id_and_info *ids = PyMem_NEW(struct queue_id_and_info, (Py_ssize_t)(queues->count)); if (ids == NULL) { + PyErr_NoMemory(); goto done; } _queueref *ref = queues->head; @@ -1328,6 +1329,7 @@ _queueid_xid_new(int64_t qid) struct _queueid_xid *data = PyMem_RawMalloc(sizeof(struct _queueid_xid)); if (data == NULL) { _queues_decref(queues, qid); + PyErr_NoMemory(); return NULL; } data->qid = qid; From 250808bc6353c20fc61fa542b762534976c13a77 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Thu, 25 Jun 2026 18:12:39 +0800 Subject: [PATCH 2/3] Update _interpqueuesmodule.c --- Modules/_interpqueuesmodule.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Modules/_interpqueuesmodule.c b/Modules/_interpqueuesmodule.c index c900c0256efc18..9b4a94f5f7915f 100644 --- a/Modules/_interpqueuesmodule.c +++ b/Modules/_interpqueuesmodule.c @@ -1042,7 +1042,7 @@ _queues_list_all(_queues *queues, int64_t *p_count) { struct queue_id_and_info *qids = NULL; PyThread_acquire_lock(queues->mutex, WAIT_LOCK); - struct queue_id_and_info *ids = PyMem_NEW(struct queue_id_and_info, + struct queue_id_and_info *ids = PyMem_New(struct queue_id_and_info, (Py_ssize_t)(queues->count)); if (ids == NULL) { PyErr_NoMemory(); @@ -1322,7 +1322,14 @@ static void * _queueid_xid_new(int64_t qid) { _queues *queues = _get_global_queues(); - if (_queues_incref(queues, qid) < 0) { + int err = _queues_incref(queues, qid); + if (err < 0) { + PyObject *mod = PyImport_ImportModule(MODULE_NAME_STR); + if (mod == NULL) { + return NULL; + } + (void)handle_queue_error(err, mod, qid); + Py_DECREF(mod); return NULL; } From d44c06128b14dbbdca7b3328a3e26db5fb458741 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Thu, 25 Jun 2026 19:34:04 +0800 Subject: [PATCH 3/3] Address review comments --- Modules/_interpqueuesmodule.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Modules/_interpqueuesmodule.c b/Modules/_interpqueuesmodule.c index 9b4a94f5f7915f..db2ecd19e9c3d0 100644 --- a/Modules/_interpqueuesmodule.c +++ b/Modules/_interpqueuesmodule.c @@ -1324,8 +1324,13 @@ _queueid_xid_new(int64_t qid) _queues *queues = _get_global_queues(); int err = _queues_incref(queues, qid); if (err < 0) { - PyObject *mod = PyImport_ImportModule(MODULE_NAME_STR); + assert(err == ERR_QUEUE_NOT_FOUND); + PyObject *mod = _get_current_module(); if (mod == NULL) { + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_SystemError, + "missing " MODULE_NAME_STR " module"); + } return NULL; } (void)handle_queue_error(err, mod, qid);