Skip to content

Commit 63dd27d

Browse files
[3.13] gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (GH-139169)
* gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (cherry picked from commit d06113c)
1 parent 9809c30 commit 63dd27d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Lib/test/test_interpreters/test_stress.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Raise SkipTest if subinterpreters not supported.
88
import_helper.import_module('_interpreters')
99
from test.support import interpreters
10+
from test.support.interpreters import InterpreterError
1011
from .utils import TestBase
1112

1213

@@ -74,6 +75,14 @@ def run():
7475
start.set()
7576
support.gc_collect()
7677

78+
def test_create_interpreter_no_memory(self):
79+
import _interpreters
80+
_testcapi = import_helper.import_module("_testcapi")
81+
82+
with self.assertRaises(InterpreterError):
83+
_testcapi.set_nomemory(0, 1)
84+
_interpreters.create()
85+
7786

7887
if __name__ == '__main__':
7988
# Test needs to be a package, so we can do relative imports.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when calling ``_interpreters.create`` when the
2+
process is out of memory.

Python/pylifecycle.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,18 +2301,17 @@ new_interpreter(PyThreadState **tstate_p,
23012301
interpreters: disable PyGILState_Check(). */
23022302
runtime->gilstate.check_enabled = 0;
23032303

2304-
PyInterpreterState *interp = PyInterpreterState_New();
2304+
// XXX Might new_interpreter() have been called without the GIL held?
2305+
PyThreadState *save_tstate = _PyThreadState_GET();
2306+
PyThreadState *tstate = NULL;
2307+
PyInterpreterState *interp;
2308+
status = _PyInterpreterState_New(save_tstate, &interp);
23052309
if (interp == NULL) {
2306-
*tstate_p = NULL;
2307-
return _PyStatus_OK();
2310+
goto error;
23082311
}
23092312
_PyInterpreterState_SetWhence(interp, whence);
23102313
interp->_ready = 1;
23112314

2312-
// XXX Might new_interpreter() have been called without the GIL held?
2313-
PyThreadState *save_tstate = _PyThreadState_GET();
2314-
PyThreadState *tstate = NULL;
2315-
23162315
/* From this point until the init_interp_create_gil() call,
23172316
we must not do anything that requires that the GIL be held
23182317
(or otherwise exist). That applies whether or not the new
@@ -2388,7 +2387,7 @@ new_interpreter(PyThreadState **tstate_p,
23882387
*tstate_p = NULL;
23892388
if (tstate != NULL) {
23902389
Py_EndInterpreter(tstate);
2391-
} else {
2390+
} else if (interp != NULL) {
23922391
PyInterpreterState_Delete(interp);
23932392
}
23942393
if (save_tstate != NULL) {

0 commit comments

Comments
 (0)