Skip to content
Merged
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
gh-155978: Fix leak in update_slot_after_setattr()
On free-threaded builds, update_slot_after_setattr() stack-allocates a
single chunk to queue slot updates, assuming one setattr never needs
more.  However, update_subclasses() queues one update per affected
subclass and so can require heap-allocated chunks.  Free those chunks
after the slots update.
  • Loading branch information
nascheme committed Aug 17, 2026
commit 3d9921b4899d4971df88b5a3d89fbd5345afbd0b
20 changes: 20 additions & 0 deletions Lib/test/test_free_threading/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,26 @@ def wrapper():
for reader in readers:
reader.join()

def test_setattr_many_subclasses(self):
# gh-155978: Updating a special method queues a slot update for every
# affected subclass. Keep enough subclasses alive to require
# heap-allocated queue chunks in addition to the stack chunk.
class Base:
pass

subclasses = [type(f"Sub{i}", (Base,), {}) for i in range(100)]

def custom_repr(self):
return "custom repr"

Base.__repr__ = custom_repr
self.assertTrue(all(repr(cls()) == "custom repr"
for cls in subclasses))

del Base.__repr__
self.assertTrue(all(repr(cls()) != "custom repr"
for cls in subclasses))

def test_concurrent_setattr_deadlock(self):
# gh-155400: two threads assigning to a special method of the same
# class could deadlock. One thread held the type lock and waited for
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a memory leak in the free-threaded build when setting or deleting a
special method (such as ``__repr__``) on a class that has many subclasses.
21 changes: 13 additions & 8 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6598,24 +6598,29 @@ static int
update_slot_after_setattr(PyTypeObject *type, PyObject *name)
{
#ifdef Py_GIL_DISABLED
// stack allocate one chunk since that's all we need
assert(SLOT_UPDATE_CHUNK_SIZE >= MAX_EQUIV);
slot_update_chunk_t chunk = {0};
// Stack allocate the first chunk. It is usually the only one needed but
// updates are queued for subclasses as well, so more chunks are needed if
// the type has more than SLOT_UPDATE_CHUNK_SIZE subclasses.
slot_update_t queued_updates = {&chunk};

if (update_slot(type, name, &queued_updates) < 0) {
return -1;
}
if (queued_updates.head->n > 0) {
int res = update_slot(type, name, &queued_updates);
if (res == 0 && queued_updates.head->n > 0) {
apply_type_slot_updates(&queued_updates);
ASSERT_TYPE_LOCK_HELD();
// should never allocate another chunk
assert(chunk.prev == NULL);
}
slot_update_chunk_t *cur = queued_updates.head;
while (cur != &chunk) {
slot_update_chunk_t *prev = cur->prev;
PyMem_Free(cur);
cur = prev;
}
return res;
#else
update_slot(type, name, NULL);
#endif
return 0;
#endif
}

static int
Expand Down
Loading