-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
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.
- Loading branch information
Showing
5 changed files
with
160 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,61 @@ | ||
import unittest | ||
|
||
import threading | ||
from threading import Thread | ||
from unittest import TestCase | ||
import gc | ||
|
||
from test.support import threading_helper | ||
|
||
|
||
class MyObj: | ||
pass | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestGC(TestCase): | ||
def test_get_objects(self): | ||
event = threading.Event() | ||
|
||
def gc_thread(): | ||
for i in range(100): | ||
o = gc.get_objects() | ||
event.set() | ||
|
||
def mutator_thread(): | ||
while not event.is_set(): | ||
o1 = MyObj() | ||
o2 = MyObj() | ||
o3 = MyObj() | ||
o4 = MyObj() | ||
|
||
gcs = [Thread(target=gc_thread)] | ||
mutators = [Thread(target=mutator_thread) for _ in range(4)] | ||
with threading_helper.start_threads(gcs + mutators): | ||
pass | ||
|
||
def test_get_referrers(self): | ||
event = threading.Event() | ||
|
||
obj = MyObj() | ||
|
||
def gc_thread(): | ||
for i in range(100): | ||
o = gc.get_referrers(obj) | ||
event.set() | ||
|
||
def mutator_thread(): | ||
while not event.is_set(): | ||
d1 = { "key": obj } | ||
d2 = { "key": obj } | ||
d3 = { "key": obj } | ||
d4 = { "key": obj } | ||
|
||
gcs = [Thread(target=gc_thread) for _ in range(2)] | ||
mutators = [Thread(target=mutator_thread) for _ in range(4)] | ||
with threading_helper.start_threads(gcs + mutators): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2024-10-23-14-42-27.gh-issue-125859.m3EF9E.rst
This file contains 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 crash in the free threading build when :func:`gc.get_objects` or | ||
:func:`gc.get_referrers` is called during an in-progress garbage collection. |
This file contains 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