Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/concurrent/futures/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ def _result_or_cancel(fut, timeout=None):
try:
try:
return (fut.result(timeout), None)
except TimeoutError:
except TimeoutError as exc:
if fut.done():
# The future already finished, so this is the callable's own
# TimeoutError, not the map() timeout waiting for the future.
return (None, exc)
raise
except BaseException as exc:
return (None, exc)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_concurrent_futures/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def raiser(exception, msg='std'):
raise exception(msg)


# Used in test_map_timeout_from_callable
def timeout_on_one(x):
if x == 1:
raise TimeoutError
return x


class FalseyBoolException(Exception):
def __bool__(self):
return False
Expand Down Expand Up @@ -87,6 +94,16 @@ def test_map_exception(self):
self.assertRaises(StopIteration, next, i)
self.assertRaises(StopIteration, next, i)

@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
def test_map_timeout_from_callable(self):
# A TimeoutError from the callable is not the map() timeout.
i = self.executor.map(timeout_on_one, [0, 1, 2, 3])
self.assertEqual(next(i), 0)
self.assertRaises(TimeoutError, next, i)
self.assertEqual(next(i), 2)
self.assertEqual(next(i), 3)
self.assertRaises(StopIteration, next, i)

@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
@support.requires_resource('walltime')
def test_map_timeout(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`concurrent.futures.Executor.map` cancelling the remaining calls when
the mapped callable raises :exc:`TimeoutError`; such an exception is now
propagated like any other, without stopping the iteration.
Loading