Skip to content

Commit 5af4f4b

Browse files
committed
Issue #3757: thread-local objects now support cyclic garbage collection.
Thread-local objects involved in reference cycles will be deallocated timely by the cyclic GC, even if the underlying thread is still running.
1 parent 6e451df commit 5af4f4b

3 files changed

Lines changed: 244 additions & 47 deletions

File tree

‎Lib/test/test_threading_local.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def _local_refs(self, n):
3838
gc.collect()
3939
self.assertEqual(len(weaklist), n)
4040

41-
# XXX threading.local keeps the local of the last stopped thread alive.
41+
# XXX _threading_local keeps the local of the last stopped thread alive.
4242
deadlist = [weak for weak in weaklist if weak() is None]
43-
self.assertEqual(len(deadlist), n-1)
43+
self.assertIn(len(deadlist), (n-1, n))
4444

4545
# Assignment to the same thread local frees it sometimes (!)
4646
local.someothervar = None
@@ -127,6 +127,20 @@ def __init__(self, *args, **kwargs):
127127
class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
128128
_local = _thread._local
129129

130+
# Fails for the pure Python implementation
131+
def test_cycle_collection(self):
132+
class X:
133+
pass
134+
135+
x = X()
136+
x.local = self._local()
137+
x.local.x = x
138+
wr = weakref.ref(x)
139+
del x
140+
gc.collect()
141+
self.assertIs(wr(), None)
142+
143+
130144
class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):
131145
_local = _threading_local.local
132146

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Extensions
6767
Library
6868
-------
6969

70+
- Issue #3757: thread-local objects now support cyclic garbage collection.
71+
Thread-local objects involved in reference cycles will be deallocated
72+
timely by the cyclic GC, even if the underlying thread is still running.
73+
7074
- Issue #9452: Add read_file, read_string, and read_dict to the configparser
7175
API; new source attribute to exceptions.
7276

0 commit comments

Comments
 (0)