mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Issue #16284: Prevent keeping unnecessary references to worker functions in concurrent.futures ThreadPoolExecutor.
This commit is contained in:
parent
0f77bf27ca
commit
6b973747f3
6 changed files with 36 additions and 0 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue