Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

asyncio exception handler called with incorrect context #96704

Closed
Assignees
gvanrossumkumaraditya303
Labels
3.10only security fixes3.11only security fixes3.12only security fixestopic-asynciotype-bugAn unexpected behavior, bug, or error
@kumaraditya303

Description

@kumaraditya303

Reproducer:

importasyncioimportcontextvarsname=contextvars.ContextVar('name',default='foo')defexc_handler(*args):assertname.get()=='bar'asyncdeftask():name.set('bar')1/0asyncdefmain():loop=asyncio.get_running_loop()loop.set_exception_handler(exc_handler)asyncio.create_task(task())asyncio.run(main())

On main:

Unhandled error in exception handlercontext: {'message': 'Task exception was never retrieved', 'exception': ZeroDivisionError('division by zero'), 'future': <Task finished name='Task-2' coro=<task() done, defined at /workspaces/cpython/main.py:11> exception=ZeroDivisionError('division by zero')>}Traceback (most recent call last):  File "/workspaces/cpython/Lib/asyncio/base_events.py", line 1797, in call_exception_handler    self._exception_handler(self, context)  File "/workspaces/cpython/main.py", line 8, in exc_handler    assert name.get() == 'bar'AssertionError

The exception handler is called with incorrect context hence theAssertionError. It should called with the same context as the task.
My 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.

Metadata

Metadata

Labels

3.10only security fixes3.11only security fixes3.12only security fixestopic-asynciotype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp