Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20efceb

Browse files
committed
Issue #21362: concurrent.futures does not validate that max_workers is proper
1 parent 120e8ed commit 20efceb

4 files changed

Lines changed: 15 additions & 0 deletions

File tree

Doc/library/concurrent.futures.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
175175
An :class:`Executor` subclass that executes calls asynchronously using a pool
176176
of at most *max_workers* processes. If *max_workers* is ``None`` or not
177177
given, it will default to the number of processors on the machine.
178+
If *max_workers* is lower or equal to ``0``, then a :exc:`ValueError`
179+
will be raised.
178180

179181
.. versionchanged:: 3.3
180182
When one of the worker processes terminates abruptly, a

Lib/concurrent/futures/process.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ def __init__(self, max_workers=None):
334334
if max_workers is None:
335335
self._max_workers = os.cpu_count() or 1
336336
else:
337+
if max_workers <= 0:
338+
raise ValueError("max_workers must be greater than 0")
339+
337340
self._max_workers = max_workers
338341

339342
# Make the call queue slightly larger than the number of processes to

Lib/concurrent/futures/thread.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def __init__(self, max_workers):
8787
max_workers: The maximum number of threads that can be used to
8888
execute the given calls.
8989
"""
90+
if max_workers <= 0:
91+
raise ValueError("max_workers must be greater than 0")
92+
9093
self._max_workers = max_workers
9194
self._work_queue = queue.Queue()
9295
self._threads = set()

Lib/test/test_concurrent_futures.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ def test_no_stale_references(self):
425425
self.assertTrue(collected,
426426
"Stale reference not collected within timeout.")
427427

428+
def test_max_workers_negative(self):
429+
for number in (0, -1):
430+
with self.assertRaisesRegexp(ValueError,
431+
"max_workers must be greater "
432+
"than 0"):
433+
self.executor_type(max_workers=number)
434+
428435

429436
class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase):
430437
def test_map_submits_without_iteration(self):

0 commit comments

Comments
 (0)