[3.14] gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (GH-139168)

gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164)
(cherry picked from commit d06113c7a7)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-10-07 19:30:23 +02:00 committed by GitHub
parent a69bdab541
commit 08bea299bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View file

@ -7,6 +7,7 @@
# Raise SkipTest if subinterpreters not supported. # Raise SkipTest if subinterpreters not supported.
import_helper.import_module('_interpreters') import_helper.import_module('_interpreters')
from concurrent import interpreters from concurrent import interpreters
from concurrent.interpreters import InterpreterError
from .utils import TestBase from .utils import TestBase
@ -74,6 +75,14 @@ def run():
start.set() start.set()
support.gc_collect() 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__': if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports. # Test needs to be a package, so we can do relative imports.

View file

@ -0,0 +1,2 @@
Fix crash when calling :func:`concurrent.interpreters.create` when the
process is out of memory.

View file

@ -2298,17 +2298,16 @@ new_interpreter(PyThreadState **tstate_p,
interpreters: disable PyGILState_Check(). */ interpreters: disable PyGILState_Check(). */
runtime->gilstate.check_enabled = 0; runtime->gilstate.check_enabled = 0;
PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
*tstate_p = NULL;
return _PyStatus_OK();
}
_PyInterpreterState_SetWhence(interp, whence);
interp->_ready = 1;
// XXX Might new_interpreter() have been called without the GIL held? // XXX Might new_interpreter() have been called without the GIL held?
PyThreadState *save_tstate = _PyThreadState_GET(); PyThreadState *save_tstate = _PyThreadState_GET();
PyThreadState *tstate = NULL; PyThreadState *tstate = NULL;
PyInterpreterState *interp;
status = _PyInterpreterState_New(save_tstate, &interp);
if (interp == NULL) {
goto error;
}
_PyInterpreterState_SetWhence(interp, whence);
interp->_ready = 1;
/* From this point until the init_interp_create_gil() call, /* From this point until the init_interp_create_gil() call,
we must not do anything that requires that the GIL be held we must not do anything that requires that the GIL be held
@ -2385,7 +2384,7 @@ new_interpreter(PyThreadState **tstate_p,
*tstate_p = NULL; *tstate_p = NULL;
if (tstate != NULL) { if (tstate != NULL) {
Py_EndInterpreter(tstate); Py_EndInterpreter(tstate);
} else { } else if (interp != NULL) {
PyInterpreterState_Delete(interp); PyInterpreterState_Delete(interp);
} }
if (save_tstate != NULL) { if (save_tstate != NULL) {