https://github.com/python/cpython/commit/5e0747db2f5a063657a117e88b58a20624c848d1
commit: 5e0747db2f5a063657a117e88b58a20624c848d1
branch: main
author: Amrutha <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-06-23T16:31:47-04:00
summary:

gh-151842: Fix crash upon OOM in `_interpreters.capture_exception` (GH-151843)

files:
A Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst
M Modules/_interpretersmodule.c
M Python/crossinterp.c

diff --git 
a/Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst 
b/Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst
new file mode 100644
index 00000000000000..8a2ecf3c40032f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-06-30-13-00-00.gh-issue-151842.OOM31g.rst
@@ -0,0 +1,2 @@
+Fix a crash in :func:`!_interpreters.capture_exception` when
+:exc:`MemoryError` happens. Patch by Amrutha Modela.
diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c
index e7a91ced48f176..d024dee906ded3 100644
--- a/Modules/_interpretersmodule.c
+++ b/Modules/_interpretersmodule.c
@@ -1541,7 +1541,9 @@ _interpreters_capture_exception_impl(PyObject *module, 
PyObject *exc_arg)
     }
 
 finally:
-    _PyXI_FreeExcInfo(info);
+    if (info != NULL) {
+        _PyXI_FreeExcInfo(info);
+    }
     if (exc != exc_arg) {
         if (PyErr_Occurred()) {
             PyErr_SetRaisedException(exc);
diff --git a/Python/crossinterp.c b/Python/crossinterp.c
index 6b489bf03f86ec..ed77c1be646e27 100644
--- a/Python/crossinterp.c
+++ b/Python/crossinterp.c
@@ -1689,6 +1689,7 @@ _PyXI_NewExcInfo(PyObject *exc)
     }
     _PyXI_excinfo *info = PyMem_RawCalloc(1, sizeof(_PyXI_excinfo));
     if (info == NULL) {
+        PyErr_NoMemory();
         return NULL;
     }
     const char *failure;
@@ -1709,6 +1710,7 @@ _PyXI_NewExcInfo(PyObject *exc)
 void
 _PyXI_FreeExcInfo(_PyXI_excinfo *info)
 {
+    assert(info != NULL);
     _PyXI_excinfo_clear(info);
     PyMem_RawFree(info);
 }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to