gh-125859: Fix crash when gc.get_objects is called during GC (#125882)

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build.

Switch to `_PyObjectStack` to avoid corrupting the `struct worklist`
linked list maintained by the GC. Also, don't return objects that are frozen
(`gc.freeze()`) or in the process of being collected to more closely match
the behavior of the default build.
This commit is contained in:
Sam Gross 2024-10-24 09:33:11 -04:00 committed by GitHub
parent b61fece852
commit e545ead66c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 160 additions and 73 deletions

View file

@ -71,6 +71,16 @@ _PyObjectStack_Pop(_PyObjectStack *stack)
return obj;
}
static inline Py_ssize_t
_PyObjectStack_Size(_PyObjectStack *stack)
{
Py_ssize_t size = 0;
for (_PyObjectStackChunk *buf = stack->head; buf != NULL; buf = buf->prev) {
size += buf->n;
}
return size;
}
// Merge src into dst, leaving src empty
extern void
_PyObjectStack_Merge(_PyObjectStack *dst, _PyObjectStack *src);