FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-155978: Fix leak in update_slot_after_setattr() (GH-155979) by nascheme · Pull Request #155984 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 21 additions & 0 deletions Lib/test/test_free_threading/test_type.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6760,24 +6760,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

Back | FazBrowse Home | New Git URL