diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-07-28 10:56:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-28 10:56:21 (GMT) |
commit | 763801aae2eca326e7828fcd274acde6165a4a4b (patch) | |
tree | 4451f803827c95a38cbded15f4ab81ba6308946a /Lib/concurrent | |
parent | 123079241db8c15af6bf206e2f390ed6a9736231 (diff) | |
download | cpython-763801aae2eca326e7828fcd274acde6165a4a4b.zip cpython-763801aae2eca326e7828fcd274acde6165a4a4b.tar.gz cpython-763801aae2eca326e7828fcd274acde6165a4a4b.tar.bz2 |
gh-95166: cancel map waited on future on timeout (GH-95169) (GH-95364)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
(cherry picked from commit e16d4ed59072839b49bda4b447f260201aae7e39)
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Diffstat (limited to 'Lib/concurrent')
-rw-r--r-- | Lib/concurrent/futures/_base.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py index d7e7e41..6742a07 100644 --- a/Lib/concurrent/futures/_base.py +++ b/Lib/concurrent/futures/_base.py @@ -310,6 +310,18 @@ def wait(fs, timeout=None, return_when=ALL_COMPLETED): done.update(waiter.finished_futures) return DoneAndNotDoneFutures(done, fs - done) + +def _result_or_cancel(fut, timeout=None): + try: + try: + return fut.result(timeout) + finally: + fut.cancel() + finally: + # Break a reference cycle with the exception in self._exception + del fut + + class Future(object): """Represents the result of an asynchronous computation.""" @@ -604,9 +616,9 @@ class Executor(object): while fs: # Careful not to keep a reference to the popped future if timeout is None: - yield fs.pop().result() + yield _result_or_cancel(fs.pop()) else: - yield fs.pop().result(end_time - time.monotonic()) + yield _result_or_cancel(fs.pop(), end_time - time.monotonic()) finally: for future in fs: future.cancel() |