diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2012-11-03 13:36:01 (GMT) |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2012-11-03 13:36:01 (GMT) |
commit | 6b973747f3c5e31afd89f6a7d79bc9f3f1628710 (patch) | |
tree | 10127b702286a312c859d4bf9922716f8ad402bf /Lib/test/test_concurrent_futures.py | |
parent | 0f77bf27ca372655104884bf1e1ecaf014e8a61a (diff) | |
download | cpython-6b973747f3c5e31afd89f6a7d79bc9f3f1628710.zip cpython-6b973747f3c5e31afd89f6a7d79bc9f3f1628710.tar.gz cpython-6b973747f3c5e31afd89f6a7d79bc9f3f1628710.tar.bz2 |
Issue #16284: Prevent keeping unnecessary references to worker functions in concurrent.futures ThreadPoolExecutor.
Diffstat (limited to 'Lib/test/test_concurrent_futures.py')
-rw-r--r-- | Lib/test/test_concurrent_futures.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index 6ae450d..4ad3309 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -15,6 +15,7 @@ import sys import threading import time import unittest +import weakref from concurrent import futures from concurrent.futures._base import ( @@ -52,6 +53,11 @@ def sleep_and_print(t, msg): sys.stdout.flush() +class MyObject(object): + def my_method(self): + pass + + class ExecutorMixin: worker_count = 5 @@ -396,6 +402,22 @@ class ExecutorTest(unittest.TestCase): self.executor.map(str, [2] * (self.worker_count + 1)) self.executor.shutdown() + @test.support.cpython_only + def test_no_stale_references(self): + # Issue #16284: check that the executors don't unnecessarily hang onto + # references. + my_object = MyObject() + my_object_collected = threading.Event() + my_object_callback = weakref.ref( + my_object, lambda obj: my_object_collected.set()) + # Deliberately discarding the future. + self.executor.submit(my_object.my_method) + del my_object + + collected = my_object_collected.wait(timeout=5.0) + self.assertTrue(collected, + "Stale reference not collected within timeout.") + class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest): def test_map_submits_without_iteration(self): |