Title: `exc_info` can get lost when `throw`ing into an `await` or `yield from` · Issue #108668 · python/cpython · GitHub
Open Graph Title: `exc_info` can get lost when `throw`ing into an `await` or `yield from` · Issue #108668 · python/cpython
X Title: `exc_info` can get lost when `throw`ing into an `await` or `yield from` · Issue #108668 · python/cpython
Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CPython versions tested on: 3.8, 3.9, 3.10...
Open Graph Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP...
X Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP...
Opengraph URL: https://github.com/python/cpython/issues/108668
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`exc_info` can get lost when `throw`ing into an `await` or `yield from`","articleBody":"# Bug report\r\n\r\n### Checklist\r\n\r\n- [X] I am confident this is a bug in CPython, not a bug in a third-party project\r\n- [X] I have searched the [CPython issue tracker](https://github.com/python/cpython/issues?q=is%3Aissue+sort%3Acreated-desc),\r\nand am confident this bug has not been reported before\r\n\r\n\r\n### CPython versions tested on:\r\n\r\n3.8, 3.9, 3.10, 3.11, 3.12, CPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\r\n\r\n### Output from running 'python -VV' on the command line:\r\n\r\nPython 3.11.5 (main, Aug 28 2023, 16:29:45) [GCC 13.2.1 20230801]\r\n\r\n### A clear and concise description of the bug:\r\n\r\nif an exception is active inside a coroutine/generator (`coro1`/`agen1`) and another exception is `.throw`n into its `await coro2`/`yield from agen2`, then `coro2`/`agen2` loses the original `exc_info`.\r\n\r\n* a generator example:\r\n\r\n ```python\r\n import sys\r\n\r\n\r\n def outer():\r\n try:\r\n raise RuntimeError(\"boom\")\r\n except RuntimeError: # or `__exit__`/`finally`\r\n print(sys.exc_info())\r\n yield from inner()\r\n # note: if you inline `inner()` here, things work fine.\r\n print(sys.exc_info())\r\n\r\n\r\n def inner():\r\n print(sys.exc_info())\r\n try:\r\n yield\r\n except ValueError:\r\n pass\r\n print(sys.exc_info())\r\n\r\n\r\n gen = outer()\r\n gen.send(None)\r\n try:\r\n gen.throw(ValueError)\r\n except StopIteration:\r\n pass\r\n ```\r\n\r\n output:\r\n\r\n ```\r\n (\u003cclass 'RuntimeError'>, RuntimeError('boom'), \u003ctraceback object at 0x7fdb6b313840>)\r\n (\u003cclass 'RuntimeError'>, RuntimeError('boom'), \u003ctraceback object at 0x7fdb6b313840>)\r\n (None, None, None)\r\n (\u003cclass 'RuntimeError'>, RuntimeError('boom'), \u003ctraceback object at 0x7fdb6b313840>)\r\n ```\r\n\r\n expected output:\r\n\r\n same `exc_info` all four times.\r\n\r\n* two coroutine examples (using asyncio in particular, with asyncio calling `throw`):\r\n\r\n ```python\r\n import asyncio\r\n import sys\r\n from asyncio import CancelledError\r\n from math import inf\r\n\r\n\r\n async def outer():\r\n try:\r\n raise RuntimeError(\"boom\")\r\n except RuntimeError: # or `__aexit__`/`finally`\r\n print(sys.exc_info())\r\n await clean_up_and_log()\r\n # note: if you inline `clean_up_and_log()` here, things work fine.\r\n print(sys.exc_info())\r\n\r\n\r\n async def clean_up_and_log():\r\n print(sys.exc_info())\r\n\r\n # attempt to do some clean up that we are okay skipping part of if we get cancelled\r\n # during it or if we already have a cancellation request.\r\n #\r\n # (to reproduce, suppose that we get a cancellation request before the optional\r\n # clean up step.)\r\n asyncio.current_task().cancel()\r\n try:\r\n await asyncio.sleep(0)\r\n except CancelledError:\r\n if sys.version_info >= (3, 11):\r\n asyncio.current_task().uncancel()\r\n\r\n # after the optional async clean up, log the exception.\r\n print(sys.exc_info())\r\n\r\n\r\n async def clean_up_and_log(): # this example is >= 3.11 only.\r\n print(sys.exc_info())\r\n\r\n # attempt to do some clean up that we want to skip if it takes too long (e.g. due to\r\n # a non-responsive peer) or if we get cancelled during it or if we already have a\r\n # cancellation request.\r\n try:\r\n async with asyncio.timeout(0.1):\r\n await asyncio.sleep(inf)\r\n except TimeoutError:\r\n pass\r\n except CancelledError:\r\n asyncio.current_task().uncancel()\r\n\r\n # after the optional async clean up, log the exception.\r\n print(sys.exc_info())\r\n\r\n\r\n asyncio.run(outer())\r\n ```\r\n\r\n output and expected output: same as the generator example.\r\n\r\nthis problem is a special case of GH-73773 that was not discussed or fixed as part of that issue[^1].\r\n\r\nfor comparison, PyPy behaves the way I expect here.\r\n\r\n[^1]: I am not sure whether it would be preferred that I necrobump the old closed issue or file a new report here.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157235\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gschaffner","@type":"Person","name":"gschaffner"},"datePublished":"2023-08-30T06:11:24Z","url":"https://github.com/python/cpython/issues/108668"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:e7190f9e-a0a3-7b63-ecd3-b8a3b597cc5d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1D6:39DC4B:59C7B8:7E3848:6B0078D3 |
| html-safe-nonce | ecee1c4ba4fe71416528012deaa5ee9a16dfc89f118c6ed3a0cfad08d5bf717e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUQ2OjM5REM0Qjo1OUM3Qjg6N0UzODQ4OjZCMDA3OEQzIiwidmlzaXRvcl9pZCI6IjQ0MDI0NjY1ODgyOTc1NTgyMjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c2dbe8bdbbf730d3d2d3466bb449513c6872ed88cc21b441812b1107a7bc8d00 |
| hovercard-subject-tag | issue:1872978213 |
| 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/108668/issue_layout |
| twitter:image | https://opengraph.githubassets.com/761af498aab948e8998c5ef38e2ae892b6588cb7c7b7979c11a33b51c3ca9947/python/cpython/issues/108668 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/761af498aab948e8998c5ef38e2ae892b6588cb7c7b7979c11a33b51c3ca9947/python/cpython/issues/108668 |
| og:image:alt | Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gschaffner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 75c2b65dd205f26961d0c29afed1a8d0bb7cca9f05780571dfd1ef93eb539d16 |
| 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 | 84e563eb4b55f8fa102f325f117533cee9e3df4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width