]> granicus.if.org Git - python/commitdiff
Issue #21362: concurrent.futures does not validate that max_workers is proper
authorBrian Quinlan <brian@sweetapp.com>
Sat, 17 May 2014 20:51:10 +0000 (13:51 -0700)
committerBrian Quinlan <brian@sweetapp.com>
Sat, 17 May 2014 20:51:10 +0000 (13:51 -0700)
Doc/library/concurrent.futures.rst
Lib/concurrent/futures/process.py
Lib/concurrent/futures/thread.py
Lib/test/test_concurrent_futures.py

index 0495737e8b5098931b7b0ea145a858494f94dd74..08c926a6d4c99417f5c2d2d0685731b660d585ad 100644 (file)
@@ -175,6 +175,8 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
    An :class:`Executor` subclass that executes calls asynchronously using a pool
    of at most *max_workers* processes.  If *max_workers* is ``None`` or not
    given, it will default to the number of processors on the machine.
+   If *max_workers* is lower or equal to ``0``, then a :exc:`ValueError`
+   will be raised.
 
    .. versionchanged:: 3.3
       When one of the worker processes terminates abruptly, a
index 07b5225d1dab8082c1c0dff922a4434696dca5d6..12993901e36c88c824aa7a6c2488c2da442d3d5e 100644 (file)
@@ -334,6 +334,9 @@ class ProcessPoolExecutor(_base.Executor):
         if max_workers is None:
             self._max_workers = os.cpu_count() or 1
         else:
+            if max_workers <= 0:
+                raise ValueError("max_workers must be greater than 0")
+
             self._max_workers = max_workers
 
         # Make the call queue slightly larger than the number of processes to
index f9beb0f7f7ed3439b1dbf1c0dff861125e27e871..8d6081cf15a56b9ad05d3ae97ef3c1e4f9afd1ba 100644 (file)
@@ -87,6 +87,9 @@ class ThreadPoolExecutor(_base.Executor):
             max_workers: The maximum number of threads that can be used to
                 execute the given calls.
         """
+        if max_workers <= 0:
+            raise ValueError("max_workers must be greater than 0")
+
         self._max_workers = max_workers
         self._work_queue = queue.Queue()
         self._threads = set()
index c74b2ca6ed0825667cc38aa57cf2ad52218c0d3a..55254b5c5e8c01f4044ea7ac199b001361ed5b49 100644 (file)
@@ -425,6 +425,13 @@ class ExecutorTest:
         self.assertTrue(collected,
                         "Stale reference not collected within timeout.")
 
+    def test_max_workers_negative(self):
+        for number in (0, -1):
+            with self.assertRaisesRegexp(ValueError,
+                                         "max_workers must be greater "
+                                         "than 0"):
+                self.executor_type(max_workers=number)
+
 
 class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase):
     def test_map_submits_without_iteration(self):