Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_interpreters/test_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Raise SkipTest if subinterpreters not supported.
import_helper.import_module('_interpreters')
from concurrent import interpreters
from concurrent.interpreters import InterpreterError
from .utils import TestBase


Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when calling :func:`concurrent.interpreters.create` when the
process is out of memory.
15 changes: 7 additions & 8 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2298,18 +2298,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
Expand Down Expand Up @@ -2385,7 +2384,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) {
Expand Down
Loading