Skip to content

Commit 4c4674b

Browse files
committed
Add a test and a blurb
1 parent 51a8310 commit 4c4674b

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

Lib/multiprocessing/pool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ def _handle_exceptions(callback, args):
788788
try:
789789
return callback(args)
790790
except Exception as e:
791-
args = threading.ExceptHookArgs([type(e), e, e.__traceback__, None])
791+
args = threading.ExceptHookArgs([type(e), e, e.__traceback__,
792+
threading.current_thread()])
792793
threading.excepthook(args)
793794
del args
794795

Lib/test/_test_multiprocessing.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,39 @@ def test_resource_warning(self):
31723172
pool = None
31733173
support.gc_collect()
31743174

3175-
def raising():
3175+
def test_callback_errors(self):
3176+
if self.TYPE == 'manager':
3177+
self.skipTest("cannot intercept excepthook in manager")
3178+
3179+
def _apply(pool, target, **kwargs):
3180+
return pool.apply_async(target, **kwargs)
3181+
3182+
def _map(pool, target, **kwargs):
3183+
return pool.map_async(target, range(1), **kwargs)
3184+
3185+
def record_exceptions(errs):
3186+
def record(args):
3187+
errs.append(args.exc_type)
3188+
return record
3189+
3190+
errs = []
3191+
for func in [_apply, _map]:
3192+
with self.subTest(func=func):
3193+
saved_hook = threading.excepthook
3194+
threading.excepthook = record_exceptions(errs)
3195+
try:
3196+
with self.Pool(1) as pool:
3197+
res = func(pool, noop, callback=raising)
3198+
res.get()
3199+
finally:
3200+
threading.excepthook = saved_hook
3201+
3202+
self.assertEqual(errs, [KeyError, KeyError])
3203+
3204+
def noop(*args):
3205+
pass
3206+
3207+
def raising(*args):
31763208
raise KeyError("key")
31773209

31783210
def unpickleable_result():
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Handle exceptions thrown by callbacks passed to
2+
:class:`multiprocessing.Pool` ``*_async`` methods, preventing them from
3+
breaking the pool.

0 commit comments

Comments
 (0)