Skip to content

Commit

Permalink
pythonGH-83658: make multiprocessing.Pool raise an exception if maxta…
Browse files Browse the repository at this point in the history
…sksperchild is not None or a positive int (pythonGH-93364)

Closes pythonGH-83658.
(cherry picked from commit e37a158)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
  • Loading branch information
iritkatriel authored and miss-islington committed Jun 17, 2022
1 parent 5163a25 commit a49809d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lib/multiprocessing/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def __init__(self, processes=None, initializer=None, initargs=(),
processes = os.cpu_count() or 1
if processes < 1:
raise ValueError("Number of processes must be at least 1")
if maxtasksperchild is not None:
if not isinstance(maxtasksperchild, int) or maxtasksperchild <= 0:
raise ValueError("maxtasksperchild must be a positive int or None")

if initializer is not None and not callable(initializer):
raise TypeError('initializer must be a callable')
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,11 @@ def test_pool_worker_lifetime_early_close(self):
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))

def test_pool_maxtasksperchild_invalid(self):
for value in [0, -1, 0.5, "12"]:
with self.assertRaises(ValueError):
multiprocessing.Pool(3, maxtasksperchild=value)

def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
# tests cases against bpo-38744 and bpo-39360
cmd = '''if 1:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :class:`multiprocessing.Pool` raise an exception if ``maxtasksperchild`` is not ``None`` or a positive int.

0 comments on commit a49809d

Please sign in to comment.