Skip to content
Closed
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
28 changes: 28 additions & 0 deletions Lib/test/test_free_threading/test_races.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,34 @@ def emit_warning():

do_race(modify_filters, emit_warning)

def test_brc_queue_sys_intern_race(self):
# 1 million strings will take Thread A significant time to process in map()
# giving Thread B plenty of time to drop references to strings that Thread A
# hasn't interned yet!
strings = ["race_string_massive_" + str(j) for j in range(1000000)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is probably too slow, I would prefer to have test which takes less time.


# Give Thread B a copy of the list.
# Thread A still holds 'strings' so local refcount > 0.
shared = list(strings)

def thread_b_func():
# Thread B simply clears its list, which decrements the shared refcount
# of every string. Since Thread B is not the owner, this calls
# _Py_DecRefShared. Since shared refcount drops to 0, they get queued
# to Thread A!
shared.clear()

tb = threading.Thread(target=thread_b_func)
tb.start()

# Thread A enters map() which runs entirely in C!
# While Thread A iterates over the 1,000,000 strings, Thread B is
# concurrently clearing its list and queueing them.
# Any string that Thread B queues BEFORE Thread A reaches it in map()
# will become queued, AND THEN made immortal by sys.intern!
list(map(sys.intern, strings))
tb.join()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix "assert(!_Py_IsImmortal(op))" failure when BRC merge queue races with objects that were recently made immortal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix "assert(!_Py_IsImmortal(op))" failure when BRC merge queue races with objects that were recently made immortal.
Fix non-immortal assertion failure when BRC merge queue races with objects that were made immortal later.

3 changes: 3 additions & 0 deletions Python/brc.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ merge_queued_objects(_PyObjectStack *to_merge)
{
PyObject *ob;
while ((ob = _PyObjectStack_Pop(to_merge)) != NULL) {
if (_Py_IsImmortal(ob)) {
continue;
}
// Subtract one when merging because the queue had a reference.
Py_ssize_t refcount = _Py_ExplicitMergeRefcount(ob, -1);
if (refcount == 0) {
Expand Down
Loading