]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 17 Jun 2022 22:32:15 +0000 (15:32 -0700)
committerGitHub <noreply@github.com>
Fri, 17 Jun 2022 22:32:15 +0000 (23:32 +0100)
Closes GH-83658.
(cherry picked from commit e37a158725dec561f234b81864363d55f05c7b4e)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/multiprocessing/pool.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst [new file with mode: 0644]

index bbe05a550c349cdaaf32f2d849a5b56aaed9a583..961d7e5991847af1ea8034a5a37d9159cf4614ae 100644 (file)
@@ -203,6 +203,9 @@ class Pool(object):
             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')
index 1b55cb31c30304e876f13ab148795d9ac93c509a..ba488b17920ef7e4012c40860fb1666357d10f46 100644 (file)
@@ -2805,6 +2805,11 @@ class _TestPoolWorkerLifetime(BaseTestCase):
         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:
diff --git a/Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst b/Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst
new file mode 100644 (file)
index 0000000..a187309
--- /dev/null
@@ -0,0 +1 @@
+Make :class:`multiprocessing.Pool` raise an exception if ``maxtasksperchild`` is not ``None`` or a positive int.