René's URL Explorer Experiment


Title: Bad interaction between asyncio task cancellation and trace functions (only 3.11) · Issue #106749 · python/cpython · GitHub

Open Graph Title: Bad interaction between asyncio task cancellation and trace functions (only 3.11) · Issue #106749 · python/cpython

X Title: Bad interaction between asyncio task cancellation and trace functions (only 3.11) · Issue #106749 · python/cpython

Description: Something about cancelling tasks is interfering with tracing, but only in 3.11. Versions 3.10 and 3.12 are unaffected. I'm sorry I don't understand enough to be more specific. Here is traced.py: import linecache, sys import asyncio from ...

Open Graph Description: Something about cancelling tasks is interfering with tracing, but only in 3.11. Versions 3.10 and 3.12 are unaffected. I'm sorry I don't understand enough to be more specific. Here is traced.py: im...

X Description: Something about cancelling tasks is interfering with tracing, but only in 3.11. Versions 3.10 and 3.12 are unaffected. I'm sorry I don't understand enough to be more specific. Here is trace...

Opengraph URL: https://github.com/python/cpython/issues/106749

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bad interaction between asyncio task cancellation and trace functions (only 3.11)","articleBody":"Something about cancelling tasks is interfering with tracing, but only in 3.11. Versions 3.10 and 3.12 are unaffected.  I'm sorry I don't understand enough to be more specific.\r\n\r\nHere is traced.py:\r\n\r\n```py\r\nimport linecache, sys\r\nimport asyncio\r\nfrom datetime import timedelta\r\nimport sys\r\n\r\ndef trace(frame, event, arg):\r\n    # The weird globals here is to avoid a NameError on shutdown...\r\n    if frame.f_code.co_filename == globals().get(\"__file__\"):\r\n        lineno = frame.f_lineno\r\n        line = linecache.getline(__file__, lineno).rstrip()\r\n        print(\"{} {}: {}\".format(event[:4], lineno, line))\r\n    return trace\r\n\r\nprint(sys.version)\r\nsys.settrace(trace)\r\n\r\ndef main():\r\n    async def cancel_task(task: asyncio.Task) -\u003e None:\r\n        task.cancel()\r\n\r\n        try:\r\n            await task\r\n        except asyncio.CancelledError:\r\n            pass\r\n\r\n    class TTLDict:\r\n        def __init__(self) -\u003e None:\r\n            self._data = {}\r\n            self._expiration_tasks = {}\r\n\r\n        def __setitem__(self, key, value) -\u003e None:\r\n            async def _waiter() -\u003e None:\r\n                await asyncio.sleep(1)\r\n\r\n            self._data[key] = value\r\n            self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\n\r\n        async def delete(self, key):\r\n            await cancel_task(self._expiration_tasks.pop(key))\r\n            return self._data.pop(key)\r\n\r\n    async def _main():\r\n        ttl_dict = TTLDict()\r\n        ttl_dict['foo'] = 42\r\n        await ttl_dict.delete('foo')\r\n        print('FINISH')\r\n\r\n    asyncio.run(_main())\r\n\r\n\r\nmain()\r\n```\r\n\r\nWhen run under Python 3.10.9:\r\n```\r\n3.10.9 (main, Dec  7 2022, 07:15:24) [Clang 12.0.0 (clang-1200.0.32.29)]\r\ncall 17: def main():\r\nline 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 26:     class TTLDict:\r\ncall 26:     class TTLDict:\r\nline 26:     class TTLDict:\r\nline 27:         def __init__(self) -\u003e None:\r\nline 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 38:         async def delete(self, key):\r\nretu 38:         async def delete(self, key):\r\nline 42:     async def _main():\r\nline 48:     asyncio.run(_main())\r\ncall 42:     async def _main():\r\nline 43:         ttl_dict = TTLDict()\r\ncall 27:         def __init__(self) -\u003e None:\r\nline 28:             self._data = {}\r\nline 29:             self._expiration_tasks = {}\r\nretu 29:             self._expiration_tasks = {}\r\nline 44:         ttl_dict['foo'] = 42\r\ncall 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 32:             async def _waiter() -\u003e None:\r\nline 35:             self._data[key] = value\r\nline 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nretu 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nline 45:         await ttl_dict.delete('foo')\r\ncall 38:         async def delete(self, key):\r\nline 39:             await cancel_task(self._expiration_tasks.pop(key))\r\ncall 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 19:         task.cancel()\r\nline 21:         try:\r\nline 22:             await task\r\nretu 22:             await task\r\nretu 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nretu 45:         await ttl_dict.delete('foo')\r\ncall 32:             async def _waiter() -\u003e None:\r\nexce 32:             async def _waiter() -\u003e None:\r\nretu 32:             async def _waiter() -\u003e None:\r\ncall 22:             await task\r\nexce 22:             await task\r\nline 23:         except asyncio.CancelledError:\r\nline 24:             pass\r\nretu 24:             pass\r\ncall 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nline 40:             return self._data.pop(key)\r\nretu 40:             return self._data.pop(key)\r\ncall 45:         await ttl_dict.delete('foo')\r\nline 46:         print('FINISH')\r\nFINISH\r\nretu 46:         print('FINISH')\r\nretu 48:     asyncio.run(_main())\r\n```\r\n\r\nUnder 3.11.4:\r\n\r\n```\r\n3.11.4 (main, Jun  7 2023, 08:42:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\ncall 17: def main():\r\nline 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 26:     class TTLDict:\r\ncall 26:     class TTLDict:\r\nline 26:     class TTLDict:\r\nline 27:         def __init__(self) -\u003e None:\r\nline 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 38:         async def delete(self, key):\r\nretu 38:         async def delete(self, key):\r\nline 42:     async def _main():\r\nline 48:     asyncio.run(_main())\r\ncall 42:     async def _main():\r\nline 43:         ttl_dict = TTLDict()\r\ncall 27:         def __init__(self) -\u003e None:\r\nline 28:             self._data = {}\r\nline 29:             self._expiration_tasks = {}\r\nretu 29:             self._expiration_tasks = {}\r\nline 44:         ttl_dict['foo'] = 42\r\ncall 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 32:             async def _waiter() -\u003e None:\r\nline 35:             self._data[key] = value\r\nline 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nretu 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nline 45:         await ttl_dict.delete('foo')\r\ncall 38:         async def delete(self, key):\r\nline 39:             await cancel_task(self._expiration_tasks.pop(key))\r\ncall 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 19:         task.cancel()\r\nline 21:         try:\r\nline 22:             await task\r\nretu 22:             await task\r\nretu 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nretu 45:         await ttl_dict.delete('foo')\r\ncall 32:             async def _waiter() -\u003e None:\r\nexce 32:             async def _waiter() -\u003e None:\r\nretu 32:             async def _waiter() -\u003e None:\r\ncall 22:             await task\r\nexce 22:             await task\r\nline 23:         except asyncio.CancelledError:\r\nline 24:             pass\r\nretu 24:             pass\r\nline 40:             return self._data.pop(key)\r\nretu 40:             return self._data.pop(key)\r\nline 46:         print('FINISH')\r\nFINISH\r\nretu 46:         print('FINISH')\r\nretu 48:     asyncio.run(_main())\r\n```\r\n\r\nUnder 3.12.0b4:\r\n\r\n```\r\n3.12.0b4 (main, Jul 11 2023, 20:38:26) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\ncall 17: def main():\r\nline 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 26:     class TTLDict:\r\ncall 26:     class TTLDict:\r\nline 26:     class TTLDict:\r\nline 27:         def __init__(self) -\u003e None:\r\nline 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 38:         async def delete(self, key):\r\nretu 38:         async def delete(self, key):\r\nline 42:     async def _main():\r\nline 48:     asyncio.run(_main())\r\ncall 42:     async def _main():\r\nline 43:         ttl_dict = TTLDict()\r\ncall 27:         def __init__(self) -\u003e None:\r\nline 28:             self._data = {}\r\nline 29:             self._expiration_tasks = {}\r\nretu 29:             self._expiration_tasks = {}\r\nline 44:         ttl_dict['foo'] = 42\r\ncall 31:         def __setitem__(self, key, value) -\u003e None:\r\nline 32:             async def _waiter() -\u003e None:\r\nline 35:             self._data[key] = value\r\nline 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nretu 36:             self._expiration_tasks[key] = asyncio.create_task(_waiter())\r\nline 45:         await ttl_dict.delete('foo')\r\ncall 38:         async def delete(self, key):\r\nline 39:             await cancel_task(self._expiration_tasks.pop(key))\r\ncall 18:     async def cancel_task(task: asyncio.Task) -\u003e None:\r\nline 19:         task.cancel()\r\nline 21:         try:\r\nline 22:             await task\r\nretu 22:             await task\r\nretu 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nretu 45:         await ttl_dict.delete('foo')\r\ncall 32:             async def _waiter() -\u003e None:\r\nexce 32:             async def _waiter() -\u003e None:\r\nretu 32:             async def _waiter() -\u003e None:\r\ncall 22:             await task\r\nexce 22:             await task\r\nline 23:         except asyncio.CancelledError:\r\nline 24:             pass\r\nretu 24:             pass\r\ncall 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nexce 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nline 39:             await cancel_task(self._expiration_tasks.pop(key))\r\nline 40:             return self._data.pop(key)\r\nretu 40:             return self._data.pop(key)\r\ncall 45:         await ttl_dict.delete('foo')\r\nexce 45:         await ttl_dict.delete('foo')\r\nline 45:         await ttl_dict.delete('foo')\r\nline 46:         print('FINISH')\r\nFINISH\r\nretu 46:         print('FINISH')\r\nretu 48:     asyncio.run(_main())\r\n```\r\n\r\n3.10.9 and 3.12.0b4 produce call events for lines 39 and 45, but 3.11.4 does not.\r\n\r\nFor comparison, here's the diff between the output for 3.10.9 and 3.11.4:\r\n```\r\n1c1\r\n\u003c 3.10.9 (main, Dec  7 2022, 07:15:24) [Clang 12.0.0 (clang-1200.0.32.29)]\r\n---\r\n\u003e 3.11.4 (main, Jun  7 2023, 08:42:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\n43d42\r\n\u003c call 39:             await cancel_task(self._expiration_tasks.pop(key))\r\n46d44\r\n\u003c call 45:         await ttl_dict.delete('foo')\r\n```\r\n\r\nAnd here's the difference between 3.11.4 and 3.12.0b4:\r\n```\r\n1c1\r\n\u003c 3.11.4 (main, Jun  7 2023, 08:42:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\n---\r\n\u003e 3.12.0b4 (main, Jul 11 2023, 20:38:26) [Clang 14.0.3 (clang-1403.0.22.14.1)]\r\n42a43,45\r\n\u003e call 39:             await cancel_task(self._expiration_tasks.pop(key))\r\n\u003e exce 39:             await cancel_task(self._expiration_tasks.pop(key))\r\n\u003e line 39:             await cancel_task(self._expiration_tasks.pop(key))\r\n44a48,50\r\n\u003e call 45:         await ttl_dict.delete('foo')\r\n\u003e exce 45:         await ttl_dict.delete('foo')\r\n\u003e line 45:         await ttl_dict.delete('foo')\r\n```\r\n\r\nThis was originally reported as https://github.com/nedbat/coveragepy/issues/1648","author":{"url":"https://github.com/nedbat","@type":"Person","name":"nedbat"},"datePublished":"2023-07-14T17:38:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/106749/cpython/issues/106749"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:8cb86786-8415-7c65-cb1e-af07cc9d088d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBD80:44FAC:1FAA4F0:2AA6A02:6A598DC6
html-safe-nonce05dc85f50cc263169a374dbae91ad709b9e07c996d812d0da4040eff10332bb7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDgwOjQ0RkFDOjFGQUE0RjA6MkFBNkEwMjo2QTU5OERDNiIsInZpc2l0b3JfaWQiOiI2Mjk5Mzc1ODU3MDUzNTY4NDU0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac44cfbb77137b7c34144c6001da46b01005f8fe35afca817c462247c60eb80fe6
hovercard-subject-tagissue:1805241015
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/106749/issue_layout
twitter:imagehttps://opengraph.githubassets.com/01873a58cea3ff95263d921b0207281fbeb6f7d95642e664c5a9da8ca26cdecd/python/cpython/issues/106749
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/01873a58cea3ff95263d921b0207281fbeb6f7d95642e664c5a9da8ca26cdecd/python/cpython/issues/106749
og:image:altSomething about cancelling tasks is interfering with tracing, but only in 3.11. Versions 3.10 and 3.12 are unaffected. I'm sorry I don't understand enough to be more specific. Here is traced.py: im...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamenedbat
hostnamegithub.com
expected-hostnamegithub.com
Nonea540949572872b935b393b36db38922db390ae71c859537d741b8f3eb7e545b5
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release624bb50a7497aa346bef8cc3743af408a9ea10ca
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/106749#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F106749
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F106749
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/106749
Reloadhttps://github.com/python/cpython/issues/106749
Reloadhttps://github.com/python/cpython/issues/106749
Please reload this pagehttps://github.com/python/cpython/issues/106749
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/106749
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 34.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Bad interaction between asyncio task cancellation and trace functions (only 3.11)https://github.com/python/cpython/issues/106749#top
https://github.com/markshannon
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/nedbat
nedbathttps://github.com/nedbat
on Jul 14, 2023https://github.com/python/cpython/issues/106749#issue-1805241015
coveragepy/coveragepy#1648https://github.com/coveragepy/coveragepy/issues/1648
markshannonhttps://github.com/markshannon
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
asynciohttps://github.com/orgs/python/projects/29
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.