]> granicus.if.org Git - python/commitdiff
bpo-33110: Catch errors raised when running add_done_callback on already completed...
authorSam Martin <ABitMoreDepth@users.noreply.github.com>
Wed, 22 May 2019 21:29:02 +0000 (22:29 +0100)
committerAntoine Pitrou <antoine@python.org>
Wed, 22 May 2019 21:29:02 +0000 (23:29 +0200)
Wrap the callback call within the `add_done_callback` function within concurrent.futures, in order to behave in an identical manner to callbacks added to a running future are triggered once it has completed.

Lib/concurrent/futures/_base.py
Lib/test/test_concurrent_futures.py
Misc/NEWS.d/next/Library/2019-05-06-22-34-47.bpo-33110.rSJSCh.rst [new file with mode: 0644]

index 8f155f0ea82bdcfbc56a6368c96fd8977a7f9626..6001e3bdb81bb3855caad825172d88895db824f4 100644 (file)
@@ -404,7 +404,10 @@ class Future(object):
             if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]:
                 self._done_callbacks.append(fn)
                 return
-        fn(self)
+        try:
+            fn(self)
+        except Exception:
+            LOGGER.exception('exception calling callback for %r', self)
 
     def result(self, timeout=None):
         """Return the result of the call that the future represents.
index 3c963dff1db2c97a6158189f5a9655e984b9005f..212ccd8d5320a7bd30b43e38a3939c532fb546e9 100644 (file)
@@ -1087,6 +1087,22 @@ class FutureTests(BaseTestCase):
         f.add_done_callback(fn)
         self.assertTrue(was_cancelled)
 
+    def test_done_callback_raises_already_succeeded(self):
+        with test.support.captured_stderr() as stderr:
+            def raising_fn(callback_future):
+                raise Exception('doh!')
+
+            f = Future()
+
+            # Set the result first to simulate a future that runs instantly,
+            # effectively allowing the callback to be run immediately.
+            f.set_result(5)
+            f.add_done_callback(raising_fn)
+
+            self.assertIn('exception calling callback for', stderr.getvalue())
+            self.assertIn('doh!', stderr.getvalue())
+
+
     def test_repr(self):
         self.assertRegex(repr(PENDING_FUTURE),
                          '<Future at 0x[0-9a-f]+ state=pending>')
diff --git a/Misc/NEWS.d/next/Library/2019-05-06-22-34-47.bpo-33110.rSJSCh.rst b/Misc/NEWS.d/next/Library/2019-05-06-22-34-47.bpo-33110.rSJSCh.rst
new file mode 100644 (file)
index 0000000..f1e2460
--- /dev/null
@@ -0,0 +1 @@
+Handle exceptions raised by functions added by concurrent.futures add_done_callback correctly when the Future has already completed.