From d6e2981de86ae9e9e024f9656aa719046297844c Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 19 Sep 2025 10:41:09 -0400 Subject: [PATCH 1/3] gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (cherry picked from commit d06113c7a7cac76a28847702685e601b79f71bf8) --- Lib/test/test_interpreters/test_stress.py | 9 +++++++++ ...2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst | 2 ++ Python/pylifecycle.c | 15 +++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst diff --git a/Lib/test/test_interpreters/test_stress.py b/Lib/test/test_interpreters/test_stress.py index fae2f38cb5534b..a39c9352f728d3 100644 --- a/Lib/test/test_interpreters/test_stress.py +++ b/Lib/test/test_interpreters/test_stress.py @@ -7,6 +7,7 @@ # Raise SkipTest if subinterpreters not supported. import_helper.import_module('_interpreters') from test.support import interpreters +from test.support.interpreters import InterpreterError from .utils import TestBase @@ -74,6 +75,14 @@ def run(): start.set() support.gc_collect() + def test_create_interpreter_no_memory(self): + import _interpreters + _testcapi = import_helper.import_module("_testcapi") + + with self.assertRaises(InterpreterError): + _testcapi.set_nomemory(0, 1) + _interpreters.create() + if __name__ == '__main__': # Test needs to be a package, so we can do relative imports. diff --git a/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst b/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst new file mode 100644 index 00000000000000..950853a696f315 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst @@ -0,0 +1,2 @@ +Fix crash when calling :func:`concurrent.interpreters.create` when the +process is out of memory. diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 01fd36e52fc11e..8cc6bd0fa78906 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2301,18 +2301,17 @@ new_interpreter(PyThreadState **tstate_p, interpreters: disable PyGILState_Check(). */ runtime->gilstate.check_enabled = 0; - PyInterpreterState *interp = PyInterpreterState_New(); + // XXX Might new_interpreter() have been called without the GIL held? + PyThreadState *save_tstate = _PyThreadState_GET(); + PyThreadState *tstate = NULL; + PyInterpreterState *interp; + status = _PyInterpreterState_New(save_tstate, &interp); if (interp == NULL) { - *tstate_p = NULL; - return _PyStatus_OK(); + goto error; } _PyInterpreterState_SetWhence(interp, whence); interp->_ready = 1; - // XXX Might new_interpreter() have been called without the GIL held? - PyThreadState *save_tstate = _PyThreadState_GET(); - PyThreadState *tstate = NULL; - /* From this point until the init_interp_create_gil() call, we must not do anything that requires that the GIL be held (or otherwise exist). That applies whether or not the new @@ -2388,7 +2387,7 @@ new_interpreter(PyThreadState **tstate_p, *tstate_p = NULL; if (tstate != NULL) { Py_EndInterpreter(tstate); - } else { + } else if (interp != NULL) { PyInterpreterState_Delete(interp); } if (save_tstate != NULL) { From b754d75b990efc12459b037cd1dc80abf123b950 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 19 Sep 2025 10:43:10 -0400 Subject: [PATCH 2/3] [3.13] gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (cherry picked from commit d06113c7a7cac76a28847702685e601b79f71bf8) Co-authored-by: Peter Bierma From 614accf4ccfca7563ad7f7ee283027eb49905866 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 19 Sep 2025 10:46:52 -0400 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst --- .../next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst b/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst index 950853a696f315..07485fcf9f1a58 100644 --- a/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst +++ b/Misc/NEWS.d/next/Library/2025-09-19-09-36-42.gh-issue-112729.mmty0_.rst @@ -1,2 +1,2 @@ -Fix crash when calling :func:`concurrent.interpreters.create` when the +Fix crash when calling ``_interpreters.create`` when the process is out of memory.