Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ typedef enum _framestate {
FRAME_SUSPENDED_YIELD_FROM = -1,
FRAME_EXECUTING = 0,
FRAME_COMPLETED = 1,
FRAME_CLEARED = 4
FRAME_CLEARED = 4,
FRAME_ZOMBIE = 5, /* For generators left on the stack of cleared threads */
} PyFrameState;

#define FRAME_STATE_SUSPENDED(S) ((S) == FRAME_SUSPENDED || (S) == FRAME_SUSPENDED_YIELD_FROM)
#define FRAME_STATE_FINISHED(S) ((S) >= FRAME_COMPLETED)
#define FRAME_STATE_FINISHED(S) ((S) > FRAME_EXECUTING)

enum _frameowner {
FRAME_OWNED_BY_THREAD = 0,
Expand Down
2 changes: 1 addition & 1 deletion Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ gen_traverse(PyObject *self, visitproc visit, void *arg)
PyGenObject *gen = _PyGen_CAST(self);
Py_VISIT(gen->gi_name);
Py_VISIT(gen->gi_qualname);
if (gen->gi_frame_state < FRAME_EXECUTING) {
if (gen->gi_frame_state < FRAME_EXECUTING || gen->gi_frame_state == FRAME_ZOMBIE) {
_PyInterpreterFrame *frame = &gen->gi_iframe;
assert(frame->frame_obj == NULL ||
frame->frame_obj->f_frame->owner == FRAME_OWNED_BY_GENERATOR);
Expand Down
34 changes: 22 additions & 12 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,18 +1636,28 @@ PyThreadState_Clear(PyThreadState *tstate)

int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;

if (verbose && tstate->current_frame != NULL) {
/* bpo-20526: After the main thread calls
_PyInterpreterState_SetFinalizing() in Py_FinalizeEx()
(or in Py_EndInterpreter() for subinterpreters),
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");
if (tstate->current_frame != NULL) {
_PyInterpreterFrame *frame = tstate->current_frame;
if (verbose) {
/* bpo-20526: After the main thread calls
_PyInterpreterState_SetFinalizing() in Py_FinalizeEx()
(or in Py_EndInterpreter() for subinterpreters),
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");
}
do {
if (frame->owner == FRAME_OWNED_BY_GENERATOR) {
PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame);
gen->gi_frame_state = FRAME_ZOMBIE;
}
frame = frame->previous;
} while (frame != NULL);
Comment on lines +1643 to +1649
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not sufficient to mark generators as zombies in PyThreadState_Clear(). We call PyThreadState_Clear() one at a time on each non-main thread:

  • At that point, they are already unlinked from the interpreter's list of PyThreadStates
  • The PyThreadState_Clear() calls makes escaping calls (Py_CLEAR()) that can trigger the GC

So the GC may still see generators that are marked as "running", but aren't in any accessible PyThreadState's frame stack.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote "At that point, they are already unlinked from the interpreter's list of PyThreadStates", but I don't think that's accurate. Thread states are unlinked in PyThreadState_Delete or PyThreadState_DeleteCurrent, and those happen after PyThreadState_Clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... my Oct 22, 2024 comment was in relation to interpreter shutdown, which reverses the typical order. During shutdown thread states are:

  1. Unlinked
  2. Cleared
  3. Deleted

Whereas during normal thread exit the order is:

  1. Cleared
  2. Unlinked
  3. Deleted

Specifically, in _Py_Finalize() we:

  1. First we unlink all the remaining thread states (except the main thread). This is typically for daemon threads, but if you press Ctrl-C it can include non-daemon threads as well:

PyThreadState *list = _PyThreadState_RemoveExcept(tstate);

  1. Then we call _PyThreadState_DeleteList(), which calls PyThreadState_Clear() on each thread one at a time, which can execute arbitrary code via finalizers and weakrefs.

_PyThreadState_DeleteList(list, /*is_after_fork=*/0);

cpython/Python/pystate.c

Lines 1904 to 1922 in 1150321

void
_PyThreadState_DeleteList(PyThreadState *list, int is_after_fork)
{
// The world can't be stopped because we PyThreadState_Clear() can
// call destructors.
assert(!_PyRuntime.stoptheworld.world_stopped);
PyThreadState *p, *next;
for (p = list; p; p = next) {
next = p->next;
PyThreadState_Clear(p);
if (is_after_fork) {
free_threadstate((_PyThreadStateImpl *)p);
}
else {
decref_threadstate((_PyThreadStateImpl *)p);
}
}
}

I think things should be okay if we mark the generators as zombies from _PyThreadState_RemoveExcept() as well as from PyThreadState_Clear().

}

/* At this point tstate shouldn't be used any more,
Expand Down