FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-39877: Fix PyEval_RestoreThread() for daemon threads by vstinner · Pull Request #18808 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the :c:func:`PyEval_RestoreThread` function for daemon threads. It does no
longer crash randomly at Python exit: access directly ``_PyRuntime`` variable
rather than using ``tstate->interp->runtime``, since tstate can be dangling
pointer.
22 changes: 14 additions & 8 deletions Python/ceval.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
}

static inline void
exit_thread_if_finalizing(PyThreadState *tstate)
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
/* _Py_Finalizing is protected by the GIL */
if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
drop_gil(&runtime->ceval, tstate);
Expand Down Expand Up @@ -285,7 +284,7 @@ PyEval_AcquireLock(void)
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
}
take_gil(ceval, tstate);
exit_thread_if_finalizing(tstate);
exit_thread_if_finalizing(runtime, tstate);
}

void
Expand All @@ -305,13 +304,16 @@ PyEval_AcquireThread(PyThreadState *tstate)
{
assert(tstate != NULL);

_PyRuntimeState *runtime = tstate->interp->runtime;
/* bpo-39877: Access directly _PyRuntime rather than using
tstate->interp->runtime: see PyEval_RestoreThread()
for the rationale. */
_PyRuntimeState *runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;

/* Check someone has called PyEval_InitThreads() to create the lock */
assert(gil_created(&ceval->gil));
take_gil(ceval, tstate);
exit_thread_if_finalizing(tstate);
exit_thread_if_finalizing(runtime, tstate);
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
}
Expand Down Expand Up @@ -384,13 +386,17 @@ PyEval_RestoreThread(PyThreadState *tstate)
{
assert(tstate != NULL);

_PyRuntimeState *runtime = tstate->interp->runtime;
/* bpo-39877: Access directly _PyRuntime rather than using
tstate->interp->runtime to support calls from Python daemon threads
after Py_Finalize() has been called. tstate can be a dangling pointer:
point to freed memory (PyThreadState). */
_PyRuntimeState *runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
assert(gil_created(&ceval->gil));

int err = errno;
take_gil(ceval, tstate);
exit_thread_if_finalizing(tstate);
exit_thread_if_finalizing(runtime, tstate);
errno = err;

_PyThreadState_Swap(&runtime->gilstate, tstate);
Expand Down Expand Up @@ -1244,7 +1250,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
take_gil(ceval, tstate);

/* Check if we should make a quick exit. */
exit_thread_if_finalizing(tstate);
exit_thread_if_finalizing(runtime, tstate);

if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("ceval: orphan tstate");
Expand Down

Back | FazBrowse Home | New Git URL