| 1 |
import unittest |
| 2 |
|
| 3 |
import threading |
| 4 |
from threading import Thread |
| 5 |
from unittest import TestCase |
| 6 |
import gc |
| 7 |
|
| 8 |
from test.support import threading_helper |
| 9 |
|
| 10 |
|
| 11 |
class MyObj: |
| 12 |
pass |
| 13 |
|
| 14 |
|
| 15 |
@threading_helper.requires_working_threading() |
| 16 |
class TestGC(TestCase): |
| 17 |
def test_get_objects(self): |
| 18 |
event = threading.Event() |
| 19 |
|
| 20 |
def gc_thread(): |
| 21 |
for i in range(100): |
| 22 |
o = gc.get_objects() |
| 23 |
event.set() |
| 24 |
|
| 25 |
def mutator_thread(): |
| 26 |
while not event.is_set(): |
| 27 |
o1 = MyObj() |
| 28 |
o2 = MyObj() |
| 29 |
o3 = MyObj() |
| 30 |
o4 = MyObj() |
| 31 |
|
| 32 |
gcs = [Thread(target=gc_thread)] |
| 33 |
mutators = [Thread(target=mutator_thread) for _ in range(4)] |
| 34 |
with threading_helper.start_threads(gcs + mutators): |
| 35 |
pass |
| 36 |
|
| 37 |
def test_get_referrers(self): |
| 38 |
NUM_GC = 2 |
| 39 |
NUM_MUTATORS = 4 |
| 40 |
|
| 41 |
b = threading.Barrier(NUM_GC + NUM_MUTATORS) |
| 42 |
event = threading.Event() |
| 43 |
|
| 44 |
obj = MyObj() |
| 45 |
|
| 46 |
def gc_thread(): |
| 47 |
b.wait() |
| 48 |
for i in range(100): |
| 49 |
o = gc.get_referrers(obj) |
| 50 |
event.set() |
| 51 |
|
| 52 |
def mutator_thread(): |
| 53 |
b.wait() |
| 54 |
while not event.is_set(): |
| 55 |
d1 = { "key": obj } |
| 56 |
d2 = { "key": obj } |
| 57 |
d3 = { "key": obj } |
| 58 |
d4 = { "key": obj } |
| 59 |
|
| 60 |
gcs = [Thread(target=gc_thread) for _ in range(NUM_GC)] |
| 61 |
mutators = [Thread(target=mutator_thread) for _ in range(NUM_MUTATORS)] |
| 62 |
with threading_helper.start_threads(gcs + mutators): |
| 63 |
pass |
| 64 |
|
| 65 |
def test_freeze_object_in_brc_queue(self): |
| 66 |
# GH-142975: Freezing objects in the BRC queue could result in some |
| 67 |
# objects having a zero refcount without being deallocated. |
| 68 |
|
| 69 |
class Weird: |
| 70 |
# We need a destructor to trigger the check for object resurrection |
| 71 |
def __del__(self): |
| 72 |
pass |
| 73 |
|
| 74 |
# This is owned by the main thread, so the subthread will have to increment |
| 75 |
# this object's reference count. |
| 76 |
weird = Weird() |
| 77 |
|
| 78 |
def evil(): |
| 79 |
gc.freeze() |
| 80 |
|
| 81 |
# Decrement the reference count from this thread, which will trigger the |
| 82 |
# slow path during resurrection and add our weird object to the BRC queue. |
| 83 |
nonlocal weird |
| 84 |
del weird |
| 85 |
|
| 86 |
# Collection will merge the object's reference count and make it zero. |
| 87 |
gc.collect() |
| 88 |
|
| 89 |
# Unfreeze the object, making it visible to the GC. |
| 90 |
gc.unfreeze() |
| 91 |
gc.collect() |
| 92 |
|
| 93 |
thread = Thread(target=evil) |
| 94 |
thread.start() |
| 95 |
thread.join() |
| 96 |
|
| 97 |
def test_set_threshold(self): |
| 98 |
# GH-148613: Setting the GC threshold from another thread could cause a |
| 99 |
# race between the `gc_should_collect` and `gc_set_threshold` functions. |
| 100 |
NUM_THREADS = 8 |
| 101 |
NUM_ITERS = 100_000 |
| 102 |
barrier = threading.Barrier(NUM_THREADS) |
| 103 |
|
| 104 |
class CyclicReference: |
| 105 |
def __init__(self): |
| 106 |
self.r = self |
| 107 |
|
| 108 |
def allocator(): |
| 109 |
barrier.wait() |
| 110 |
for _ in range(NUM_ITERS): |
| 111 |
CyclicReference() |
| 112 |
|
| 113 |
def setter(): |
| 114 |
barrier.wait() |
| 115 |
for i in range(NUM_ITERS): |
| 116 |
gc.set_threshold(100 + (i % 100), 10 + (i % 10), 10 + (i % 10)) |
| 117 |
|
| 118 |
current_threshold = gc.get_threshold() |
| 119 |
try: |
| 120 |
threads = [Thread(target=allocator) for _ in range(NUM_THREADS - 1)] |
| 121 |
threads.append(Thread(target=setter)) |
| 122 |
with threading_helper.start_threads(threads): |
| 123 |
pass |
| 124 |
finally: |
| 125 |
gc.set_threshold(*current_threshold) |
| 126 |
|
| 127 |
|
| 128 |
if __name__ == "__main__": |
| 129 |
unittest.main() |