GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing github.com — asyncio: default exception handler does not run in the task's context · Issue #159303 · python/cpython Open Labels stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug reportgh-96704 (PR #96756, 3.12) changed BaseEventLoop.call_exception_handler() to run the handler inside task.get_context() / handle.get_context(). The change was applied only to the branch where a custom handler is set with loop.set_exception_handler(). When no handler is set, default_exception_handler() is called directly, in whatever context is current at that moment. cpython/Lib/asyncio/base_events.py Lines 1942 to 1952 in 85bca2d
import asyncio import contextvars import logging import sys request_id = contextvars.ContextVar("request_id", default=None) class RequestIdFilter(logging.Filter): def filter(self, record): record.request_id = request_id.get() return True class NoTracebackFormatter(logging.Formatter): def formatException(self, exc_info): return "" handler = logging.StreamHandler(sys.stdout) handler.addFilter(RequestIdFilter()) handler.setFormatter(NoTracebackFormatter(" logged with request_id=%(request_id)s")) logging.getLogger("asyncio").addHandler(handler) logging.getLogger("asyncio").propagate = False async def fail(): request_id.set("req-1") raise ValueError async def other_request(): request_id.set("req-2") task = asyncio.ensure_future(fail()) await asyncio.sleep(0.01) del task # "Task exception was never retrieved" is reported here async def main(custom_handler): if custom_handler: asyncio.get_running_loop().set_exception_handler( lambda loop, context: loop.default_exception_handler(context) ) await asyncio.create_task(other_request()) print(sys.version) print("default handler (expected req-1):") asyncio.run(main(custom_handler=False)) print("custom handler calling default_exception_handler (expected req-1):") asyncio.run(main(custom_handler=True)) Output (identical on 3.12.7, 3.13.16, 3.14.8, 3.15.0): Expected: the default handler runs in the failed task's (or handle's) context, as custom handlers have since gh-96704. Workaround: loop.set_exception_handler(lambda loop, context: loop.default_exception_handler(context)) CPython versions tested on:3.15 Operating systems tested on:macOS Linked PRsActivity
MetadataMetadataAssigneesLabelsstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error Projects
MilestoneRelationshipsDevelopmentIssue actions | ||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |