]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-125864: Propagate `pickle.loads()` failures in `InterpreterPoolExecutor` (gh-125898)
authorPeter Bierma <zintensitydev@gmail.com>
Thu, 24 Oct 2024 16:51:45 +0000 (12:51 -0400)
committerGitHub <noreply@github.com>
Thu, 24 Oct 2024 16:51:45 +0000 (10:51 -0600)
Authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/concurrent/futures/interpreter.py
Lib/test/test_concurrent_futures/test_interpreter_pool.py

index fd7941adb766bbf9e6e71ad965d71fad9bad76e3..d17688dc9d7346269caf9245caa2ca608747be3d 100644 (file)
@@ -107,7 +107,8 @@ class WorkerContext(_thread.WorkerContext):
 
     @classmethod
     def _call_pickled(cls, pickled, resultsid):
-        fn, args, kwargs = pickle.loads(pickled)
+        with cls._capture_exc(resultsid):
+            fn, args, kwargs = pickle.loads(pickled)
         cls._call(fn, args, kwargs, resultsid)
 
     def __init__(self, initdata, shared=None):
index 5264b1bb6e9c75e9a7e204e758f677f68db13283..ea1512fc830d0c911f83ce93eca04f8f1e211517 100644 (file)
@@ -56,6 +56,16 @@ class InterpretersMixin(InterpreterPoolMixin):
         return r, w
 
 
+class PickleShenanigans:
+    """Succeeds with pickle.dumps(), but fails with pickle.loads()"""
+    def __init__(self, value):
+        if value == 1:
+            raise RuntimeError("gotcha")
+
+    def __reduce__(self):
+        return (self.__class__, (1,))
+
+
 class InterpreterPoolExecutorTest(
             InterpretersMixin, ExecutorTest, BaseTestCase):
 
@@ -279,6 +289,14 @@ class InterpreterPoolExecutorTest(
         self.assertEqual(len(executor._threads), 1)
         executor.shutdown(wait=True)
 
+    def test_pickle_errors_propagate(self):
+        # GH-125864: Pickle errors happen before the script tries to execute, so the
+        # queue used to wait infinitely.
+
+        fut = self.executor.submit(PickleShenanigans(0))
+        with self.assertRaisesRegex(RuntimeError, "gotcha"):
+            fut.result()
+
 
 class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase):