You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The new_interpreter() function incorrectly returns _PyStatus_OK() on some error code paths #112729
Note that PyInterpreterState_New() currently never returns NULL -- it calls Py_FatalError() instead -- but we should probably still handle that case as well.
_PyThreadState_New can return NULL when out of memory.
It's possible to recover from Py_NewInterpreter() returning NULL, whereas calling Py_ExitStatusException() kills the process: it's not possible to recover from such error.
Note that PyInterpreterState_New() currently never returns NULL -- it calls Py_FatalError() instead -- but we should probably still handle that case as well.
You can read through the called _PyInterpreterState_New(). It only sets *pinterp to NULL if it also returns an exception status, which means that PyInterpreterState_New() never returns NULL.
Py_NewInterpreter() also never returns NULL. If _PyThreadState_New() fails, new_interpreter returns an exception status, which leads to Py_ExitStatusException(). And PyInterpreterState_New() doesn't return NULL (see above).
I looked at the code and it looks dangerous to change _PyInterpreterState_New() and PyInterpreterState_New() to return NULL on memory allocation error, simply because it wasn't possible before. Memory allocation failure is unlikely and so I'm ok with using _PyStatus_NO_MEMORY() in this case.
Activity
ericsnowcurrently commentedon Dec 8, 2023
@vstinner, looks like you made the relevant change 6 years ago in gh-4412. Do you have an objections to returning an error status in those two cases?
kumaraditya303 commentedon Aug 8, 2024
The proposed changes SGTM
vstinner commentedon Aug 8, 2024
Py_NewInterpreter() returns NULL if PyInterpreterState_New() or _PyThreadState_New() returns NULL. Sadly, this behavior is not documented: https://docs.python.org/dev/c-api/init.html#c.Py_NewInterpreter
It's possible to recover from Py_NewInterpreter() returning NULL, whereas calling Py_ExitStatusException() kills the process: it's not possible to recover from such error.
Are you sure about that?
colesbury commentedon Aug 8, 2024
@vstinner, please take a look at the current implementations. You can also test it yourself by injecting a
return NULLin_PyThreadState_New()PyInterpreterState_New()callsPy_ExitStatusExceptionon error. It doesn't returnNULL.cpython/Python/pystate.c
Lines 756 to 769 in 2d9d3a9
You can read through the called
_PyInterpreterState_New(). It only sets*pinterptoNULLif it also returns an exception status, which means thatPyInterpreterState_New()never returnsNULL.Py_NewInterpreter()also never returns NULL. If_PyThreadState_New()fails,new_interpreterreturns an exception status, which leads toPy_ExitStatusException(). AndPyInterpreterState_New()doesn't return NULL (see above).vstinner commentedon Aug 9, 2024
I looked at the code and it looks dangerous to change _PyInterpreterState_New() and PyInterpreterState_New() to return NULL on memory allocation error, simply because it wasn't possible before. Memory allocation failure is unlikely and so I'm ok with using _PyStatus_NO_MEMORY() in this case.
gh-112729: Correctly fail when the process is out of memory during in…
pythongh-112729: Correctly fail when the process is out of memory dur…
pythongh-112729: Correctly fail when the process is out of memory dur…
[3.13] pythongh-112729: Correctly fail when the process is out of mem…
ZeroIntensity commentedon Sep 19, 2025
Done at the sprint.
[3.13] gh-112729: Correctly fail when the process is out of memory du…
vstinner commentedon Sep 22, 2025
Thanks for the fix @ZeroIntensity, thanks for the bug report @colesbury.
[3.14] gh-112729: Correctly fail when the process is out of memory du…