File: test_gc.py

package info (click to toggle)
python3.14 3.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 130,180 kB
  • sloc: python: 756,125; ansic: 717,520; xml: 31,250; sh: 5,990; cpp: 4,063; makefile: 1,999; objc: 787; lisp: 502; javascript: 136; asm: 75; csh: 12
file content (99 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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):
        NUM_GC = 2
        NUM_MUTATORS = 4

        b = threading.Barrier(NUM_GC + NUM_MUTATORS)
        event = threading.Event()

        obj = MyObj()

        def gc_thread():
            b.wait()
            for i in range(100):
                o = gc.get_referrers(obj)
            event.set()

        def mutator_thread():
            b.wait()
            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(NUM_GC)]
        mutators = [Thread(target=mutator_thread) for _ in range(NUM_MUTATORS)]
        with threading_helper.start_threads(gcs + mutators):
            pass

    def test_freeze_object_in_brc_queue(self):
        # GH-142975: Freezing objects in the BRC queue could result in some
        # objects having a zero refcount without being deallocated.

        class Weird:
            # We need a destructor to trigger the check for object resurrection
            def __del__(self):
                pass

        # This is owned by the main thread, so the subthread will have to increment
        # this object's reference count.
        weird = Weird()

        def evil():
            gc.freeze()

            # Decrement the reference count from this thread, which will trigger the
            # slow path during resurrection and add our weird object to the BRC queue.
            nonlocal weird
            del weird

            # Collection will merge the object's reference count and make it zero.
            gc.collect()

            # Unfreeze the object, making it visible to the GC.
            gc.unfreeze()
            gc.collect()

        thread = Thread(target=evil)
        thread.start()
        thread.join()


if __name__ == "__main__":
    unittest.main()