@@ -1636,18 +1636,28 @@ PyThreadState_Clear(PyThreadState *tstate)
16361636
16371637 int verbose = _PyInterpreterState_GetConfig (tstate -> interp )-> verbose ;
16381638
1639- if (verbose && tstate -> current_frame != NULL ) {
1640- /* bpo-20526: After the main thread calls
1641- _PyInterpreterState_SetFinalizing() in Py_FinalizeEx()
1642- (or in Py_EndInterpreter() for subinterpreters),
1643- threads must exit when trying to take the GIL.
1644- If a thread exit in the middle of _PyEval_EvalFrameDefault(),
1645- tstate->frame is not reset to its previous value.
1646- It is more likely with daemon threads, but it can happen
1647- with regular threads if threading._shutdown() fails
1648- (ex: interrupted by CTRL+C). */
1649- fprintf (stderr ,
1650- "PyThreadState_Clear: warning: thread still has a frame\n" );
1639+ if (tstate -> current_frame != NULL ) {
1640+ _PyInterpreterFrame * frame = tstate -> current_frame ;
1641+ if (verbose ) {
1642+ /* bpo-20526: After the main thread calls
1643+ _PyInterpreterState_SetFinalizing() in Py_FinalizeEx()
1644+ (or in Py_EndInterpreter() for subinterpreters),
1645+ threads must exit when trying to take the GIL.
1646+ If a thread exit in the middle of _PyEval_EvalFrameDefault(),
1647+ tstate->frame is not reset to its previous value.
1648+ It is more likely with daemon threads, but it can happen
1649+ with regular threads if threading._shutdown() fails
1650+ (ex: interrupted by CTRL+C). */
1651+ fprintf (stderr ,
1652+ "PyThreadState_Clear: warning: thread still has a frame\n" );
1653+ }
1654+ do {
1655+ if (frame -> owner == FRAME_OWNED_BY_GENERATOR ) {
1656+ PyGenObject * gen = _PyGen_GetGeneratorFromFrame (frame );
1657+ gen -> gi_frame_state = FRAME_ZOMBIE ;
1658+ }
1659+ frame = frame -> previous ;
1660+ } while (frame != NULL );
16511661 }
16521662
16531663 /* At this point tstate shouldn't be used any more,
0 commit comments