Skip to content

GH-125603: Don't count executing generators and coroutines as referrers in gc.gc_referrers. #125640

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,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)

#ifdef __cplusplus
}
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,9 +1211,8 @@ async def main():
# can't use assertRaises because that clears frames
exc = excs.exceptions[0]
self.assertIsNotNone(exc)
self.assertListEqual(gc.get_referrers(exc), [main_coro])
main_coro = main()
asyncio.run(main_coro)
self.assertListEqual(gc.get_referrers(exc), [])
asyncio.run(main())


if __name__ == '__main__':
Expand Down
22 changes: 6 additions & 16 deletions Lib/test/test_asyncio/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# license: PSFL.

import weakref
import sys
import gc
import asyncio
import contextvars
Expand Down Expand Up @@ -30,15 +29,6 @@ def get_error_types(eg):
return {type(exc) for exc in eg.exceptions}


def no_other_refs():
# due to gh-124392 coroutines now refer to their locals
coro = asyncio.current_task().get_coro()
frame = sys._getframe(1)
while coro.cr_frame != frame:
coro = coro.cr_await
return [coro]


def set_gc_state(enabled):
was_enabled = gc.isenabled()
if enabled:
Expand Down Expand Up @@ -942,7 +932,7 @@ class _Done(Exception):
exc = e

self.assertIsNotNone(exc)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])


async def test_exception_refcycles_errors(self):
Expand All @@ -960,7 +950,7 @@ class _Done(Exception):
exc = excs.exceptions[0]

self.assertIsInstance(exc, _Done)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])


async def test_exception_refcycles_parent_task(self):
Expand All @@ -982,7 +972,7 @@ async def coro_fn():
exc = excs.exceptions[0].exceptions[0]

self.assertIsInstance(exc, _Done)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])


async def test_exception_refcycles_parent_task_wr(self):
Expand All @@ -1006,7 +996,7 @@ async def coro_fn():

self.assertIsNone(task_wr())
self.assertIsInstance(exc, _Done)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])

async def test_exception_refcycles_propagate_cancellation_error(self):
"""Test that TaskGroup deletes propagate_cancellation_error"""
Expand All @@ -1021,7 +1011,7 @@ async def test_exception_refcycles_propagate_cancellation_error(self):
exc = e.__cause__

self.assertIsInstance(exc, asyncio.CancelledError)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])

async def test_exception_refcycles_base_error(self):
"""Test that TaskGroup deletes self._base_error"""
Expand All @@ -1038,7 +1028,7 @@ class MyKeyboardInterrupt(KeyboardInterrupt):
exc = e

self.assertIsNotNone(exc)
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
self.assertListEqual(gc.get_referrers(exc), [])

async def test_name(self):
name = None
Expand Down
3 changes: 2 additions & 1 deletion Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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_CLEARED) {
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 Expand Up @@ -390,6 +390,7 @@ gen_close(PyObject *self, PyObject *args)

if (gen->gi_frame_state == FRAME_CREATED) {
gen->gi_frame_state = FRAME_COMPLETED;
_PyFrame_ClearLocals(&gen->gi_iframe);
Py_RETURN_NONE;
}
if (FRAME_STATE_FINISHED(gen->gi_frame_state)) {
Expand Down
36 changes: 24 additions & 12 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "pycore_critical_section.h" // _PyCriticalSection_Resume()
#include "pycore_dtoa.h" // _dtoa_state_INIT()
#include "pycore_emscripten_trampoline.h" // _Py_EmscriptenTrampoline_Init()
#include "pycore_frame.h" // FRAME_ZOMBIE
#include "pycore_freelist.h" // _PyObject_ClearFreeLists()
#include "pycore_genobject.h" // _PyGen_GetGeneratorFromFrame()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interpframe.h" // _PyThreadState_HasStackSpace()
#include "pycore_object.h" // _PyType_InitCache()
Expand Down Expand Up @@ -1623,18 +1625,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().

}

if (verbose && tstate->current_exception != NULL) {
Expand Down
Loading