Skip to content

[3.8] bpo-20526: Fix PyThreadState_Clear(): don't decref frame (GH-19120) #19136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
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
1 change: 1 addition & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct _ts {
struct _ts *next;
PyInterpreterState *interp;

/* Borrowed reference to the current frame (it can be NULL) */
struct _frame *frame;
int recursion_depth;
char overflowed; /* The stack has overflowed. Allow 50 more calls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :c:func:`PyThreadState_Clear()`. ``PyThreadState.frame`` is a borrowed
reference, not a strong reference: ``PyThreadState_Clear()`` must not call
``Py_CLEAR(tstate->frame)``.
12 changes: 10 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,19 @@ PyThreadState_Clear(PyThreadState *tstate)
{
int verbose = tstate->interp->config.verbose;

if (verbose && tstate->frame != NULL)
if (verbose && tstate->frame != NULL) {
/* bpo-20526: After the main thread calls
_PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
exit when trying to take the GIL. If a thread exit in the middle of
_PyEval_EvalFrameDefault(), tstate->frame is not reset to its
previous value. It is more likely with daemon threads, but it can
happen with regular threads if threading._shutdown() fails
(ex: interrupted by CTRL+C). */
fprintf(stderr,
"PyThreadState_Clear: warning: thread still has a frame\n");
}

Py_CLEAR(tstate->frame);
/* Don't clear tstate->frame: it is a borrowed reference */

Py_CLEAR(tstate->dict);
Py_CLEAR(tstate->async_exc);
Expand Down