Skip to content

Commit 0044e19

Browse files
committed
gh-162752: Assert the type lock is kept while checking subclass slots
slots_are_generic() iterates tp_subclasses with a borrowed reference, which is only safe while TYPE_LOCK is held: a __bases__ assignment in another thread can empty and free that dict. Nothing fails visibly if the lock is dropped during the walk (the freed dict is usually reused), so assert that the dict is still the one being iterated. With this, test_slot_update_unchanged_race fails if the walk blocks. Co-authored with Claude Fable 5.2
1 parent 5d1926d commit 0044e19

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

‎Objects/typeobject.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12412,6 +12412,8 @@ slots_are_generic(PyTypeObject *type, PyObject *name, const uint8_t *dups,
1241212412
if (!overrides && !slots_are_generic(subclass, name, dups, refs)) {
1241312413
return 0;
1241412414
}
12415+
// Iterating is only safe if TYPE_LOCK was never released.
12416+
assert(lookup_tp_subclasses(type) == subclasses);
1241512417
}
1241612418
return 1;
1241712419
}

0 commit comments

Comments
 (0)