Title: asyncio exception handler called with incorrect context · Issue #96704 · python/cpython · GitHub
Open Graph Title: asyncio exception handler called with incorrect context · Issue #96704 · python/cpython
X Title: asyncio exception handler called with incorrect context · Issue #96704 · python/cpython
Description: Reproducer: import asyncio import contextvars name = contextvars.ContextVar('name', default='foo') def exc_handler(*args): assert name.get() == 'bar' async def task(): name.set('bar') 1/0 async def main(): loop = asyncio.get_running_loop...
Open Graph Description: Reproducer: import asyncio import contextvars name = contextvars.ContextVar('name', default='foo') def exc_handler(*args): assert name.get() == 'bar' async def task(): name.set('bar') 1/0 async def...
X Description: Reproducer: import asyncio import contextvars name = contextvars.ContextVar('name', default='foo') def exc_handler(*args): assert name.get() == 'bar' async def task(): name....
Opengraph URL: https://github.com/python/cpython/issues/96704
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio exception handler called with incorrect context","articleBody":"Reproducer:\r\n```py\r\nimport asyncio\r\nimport contextvars\r\n\r\nname = contextvars.ContextVar('name', default='foo')\r\n\r\n\r\ndef exc_handler(*args):\r\n assert name.get() == 'bar'\r\n\r\n\r\nasync def task():\r\n name.set('bar')\r\n 1/0\r\n\r\n\r\nasync def main():\r\n loop = asyncio.get_running_loop()\r\n loop.set_exception_handler(exc_handler)\r\n asyncio.create_task(task())\r\n\r\nasyncio.run(main())\r\n```\r\n\r\nOn main:\r\n```console\r\nUnhandled error in exception handler\r\ncontext: {'message': 'Task exception was never retrieved', 'exception': ZeroDivisionError('division by zero'), 'future': \u003cTask finished name='Task-2' coro=\u003ctask() done, defined at /workspaces/cpython/main.py:11\u003e exception=ZeroDivisionError('division by zero')\u003e}\r\nTraceback (most recent call last):\r\n File \"/workspaces/cpython/Lib/asyncio/base_events.py\", line 1797, in call_exception_handler\r\n self._exception_handler(self, context)\r\n File \"/workspaces/cpython/main.py\", line 8, in exc_handler\r\n assert name.get() == 'bar'\r\nAssertionError\r\n```\r\n\r\nThe exception handler is called with incorrect context hence the `AssertionError`. It should called with the same context as the task. \r\nMy use case is retrieving the current request id from the context var if there are any unhandled error but because of this bug, I always get the default value.","author":{"url":"https://github.com/kumaraditya303","@type":"Person","name":"kumaraditya303"},"datePublished":"2022-09-09T11:44:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/96704/cpython/issues/96704"}
| 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:8e43850a-a99f-4853-2ba7-cb5296e6f94a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8552:1D9151:7C2E1A:A55563:696978B7 |
| html-safe-nonce | c6a822be252b17fcc6f8e3c75aa4684f4671b3686209aa8897ccd6fcaaab88be |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTUyOjFEOTE1MTo3QzJFMUE6QTU1NTYzOjY5Njk3OEI3IiwidmlzaXRvcl9pZCI6IjY4NDAzOTUzNTA5NjM0ODQ4NTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0e18e95c00e425d3f8d726045b79e8bac43ce070b79a1d218f47c0abee4ea22f |
| hovercard-subject-tag | issue:1367713302 |
| 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/96704/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e671c7c451516dddab82a62565b85b00c5de1b1f9391dd5b23b05e1d8c7326ec/python/cpython/issues/96704 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e671c7c451516dddab82a62565b85b00c5de1b1f9391dd5b23b05e1d8c7326ec/python/cpython/issues/96704 |
| og:image:alt | Reproducer: import asyncio import contextvars name = contextvars.ContextVar('name', default='foo') def exc_handler(*args): assert name.get() == 'bar' async def task(): name.set('bar') 1/0 async def... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kumaraditya303 |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6f193beb8ff08443adc07685d75302ab8aaf0a135f6e251c3ff3112c8deb881 |
| 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 | 212e3e3d3298bf5b313830edfd2399e869f7ea76 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width