Title: Cancel futures if concurrent.futures.Executor is used as a context manager and there is an exception? · Issue #130975 · python/cpython · GitHub
Open Graph Title: Cancel futures if concurrent.futures.Executor is used as a context manager and there is an exception? · Issue #130975 · python/cpython
X Title: Cancel futures if concurrent.futures.Executor is used as a context manager and there is an exception? · Issue #130975 · python/cpython
Description: I bumped into a bit of a rough edge related to how concurrent.futures.Executor instances behave when they're used as context managers and there is an exception raised from the with statement body. I had some code like this (reduced to th...
Open Graph Description: I bumped into a bit of a rough edge related to how concurrent.futures.Executor instances behave when they're used as context managers and there is an exception raised from the with statement body. ...
X Description: I bumped into a bit of a rough edge related to how concurrent.futures.Executor instances behave when they're used as context managers and there is an exception raised from the with statement bo...
Opengraph URL: https://github.com/python/cpython/issues/130975
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cancel futures if concurrent.futures.Executor is used as a context manager and there is an exception?","articleBody":"I bumped into a bit of a rough edge related to how `concurrent.futures.Executor` instances behave when they're used as context managers and there is an exception raised from the with statement body.\n\nI had some code like this (reduced to the bare minimum):\n\n```\nwith concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:\n futures = {\n executor.submit(check_status, item): item\n for item in to_check\n }\n for index, future in enumerate(concurrent.futures.as_completed(futures)):\n print(f\"{index}/{len(futures)} {future.result()}\")\n```\n\nNow, as it happens `check_status` liked to raise an exception from time to time and this would be fine if the application stopped because of that but it kept going but in a weird way. What I saw was `check_status` kept producing logs but the `print` calls in the snippet above stopped being executed.\n\nThis is because of how `Executor` implements `__exit__` – it waits for all pending futures to complete before propagating the exception:\n\n```\n def __exit__(self, exc_type, exc_val, exc_tb):\n self.shutdown(wait=True)\n return False\n```\n\nNo in my case I started with thousands of futures so it could take hours for the process to finally crash.\n\nSince Python 3.9 the `shutdown` method has the `cancel_futures` parameter (implemented in https://github.com/python/cpython/pull/18057): https://docs.python.org/3.9/library/concurrent.futures.html#concurrent.futures.Executor.shutdown.\n\nI'm proposing changing the behavior of the context manager `__exit__` to cancel futures if there is an exception:\n\n```\n def __exit__(self, exc_type, exc_val, exc_tb):\n self.shutdown(wait=True, cancel_futures=exc_val is not None)\n return False\n```\n\nThis would allow the in-flight futures to finish but any further processing wouldn't happen and the exception would be reported in a (more) timely fashion.\n\ncc @brianquinlan @pitrou as I saw you participate in the `cancel_futures` PR, if there's someone else I should ping here for visibility please let me know.","author":{"url":"https://github.com/jstasiak","@type":"Person","name":"jstasiak"},"datePublished":"2025-03-08T11:29:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/130975/cpython/issues/130975"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:caf04b59-8543-9c0f-8405-a4d90cffa8b5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 840A:C8AE2:7EBF74B:B1CDAEE:6A5041A4 |
| html-safe-nonce | 87a6d1b5020e7e4dffd27c6cca64a708b13aecc28559f6d1e9d585b8de9cc214 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDBBOkM4QUUyOjdFQkY3NEI6QjFDREFFRTo2QTUwNDFBNCIsInZpc2l0b3JfaWQiOiI0NTc5NzEzNjIyMjM0NjQ0OTAwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9880756307f851e5f5fb9bb0dd5da66f2b300d989fc900f5726b67e5024d7464 |
| hovercard-subject-tag | issue:2904733598 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/130975/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b35fcfdea930e3759ec80f1dbb8a07e061887019afbd72e4d3675f74f71d5d88/python/cpython/issues/130975 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b35fcfdea930e3759ec80f1dbb8a07e061887019afbd72e4d3675f74f71d5d88/python/cpython/issues/130975 |
| og:image:alt | I bumped into a bit of a rough edge related to how concurrent.futures.Executor instances behave when they're used as context managers and there is an exception raised from the with statement body. ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jstasiak |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d90666eaf032b7eb8975a378efe38e37f4ebb96c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width