GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — asyncio: default exception handler does not run in the task's context · Issue #159303 · python/cpython

asyncio: default exception handler does not run in the task's context #159303

New issue
Copy link
New issue
Copy link
Open
#159307
Open
asyncio: default exception handler does not run in the task's context#159303
#159307
Copy link
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

Description

@Reskov
Reskov
opened on Oct 13, 2026
Issue body actions

Bug report

gh-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

if self._exception_handler is None:
try:
self.default_exception_handler(context)
except (SystemExit, KeyboardInterrupt):
raise
except BaseException:
# Second protection layer for unexpected errors
# in the default implementation, as well as for subclassed
# event loops with overloaded "default_exception_handler".
logger.error('Exception in default exception handler',
exc_info=True)

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):

default handler (expected req-1):
  logged with request_id=req-2
custom handler calling default_exception_handler (expected req-1):
  logged with request_id=req-1

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 PRs

  • gh-159303: Run asyncio's default exception handler in the task context #159307

Activity

  1. Reskov
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 13, 2026
  2. drakeo338
    added a commit that references this issue on Oct 13, 2026

    pythongh-159303: Document the exception handler context change

    a6373a7
  3. aisk
    added
    topic-asyncio
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Oct 13, 2026
  4. github-project-automation
    added this to asyncioon Oct 13, 2026
  5. github-project-automation
    moved this to Todo in asyncioon Oct 13, 2026
  6. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  7. github-project-automation
    moved this to Todo in Project_VKon Oct 18, 2026
  8. github-project-automation
    moved this from Todo to In progress in Project_VKon Oct 18, 2026
  9. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  10. drakeo338
    added a commit that references this issue on Oct 29, 2026

    pythongh-159303: Note that a handler's ContextVar writes are not visi…

    340c4e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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

    Projects

    • asyncio
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue 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.