8000 gh-124068: Fix reference leak with generators in the free-threaded build · python/cpython@8d220b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d220b7

Browse files
committed
gh-124068: Fix reference leak with generators in the free-threaded build
If the generator is already cleared, then most fields in the generator's frame are not valid other than f_executable. The invalid fields may contain dangling pointers and should not be used.
1 parent 4f25e28 commit 8d220b7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Python/gc_free_threading.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,20 @@ frame_disable_deferred_refcounting(_PyInterpreterFrame *frame)
186186
// Convert locals, variables, and the executable object to strong
187187
// references from (possibly) deferred references.
188188
assert(frame->stackpointer != NULL);
189+
assert(frame->owner == FRAME_OWNED_BY_FRAME_OBJECT ||
190+
frame->owner == FRAME_OWNED_BY_GENERATOR);
191+
189192
frame->f_executable = PyStackRef_AsStrongReference(frame->f_executable);
193+
194+
if (frame->owner == FRAME_OWNED_BY_GENERATOR) {
195+
PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame);
196+
if (gen->gi_frame_state == FRAME_CLEARED) {
197+
// gh-124068: if the generator is cleared, then most fields other
198+
// than f_executable are not valid.
199+
return;
200+
}
201+
}
202+
190203
for (_PyStackRef *ref = frame->localsplus; ref < frame->stackpointer; ref++) {
191204
if (!PyStackRef_IsNull(*ref) && PyStackRef_IsDeferred(*ref)) {
192205
*ref = PyStackRef_AsStrongReference(*ref);

0 commit comments

Comments
 (0)
0