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 exception handler called with incorrect context · Issue #96704 · python/cpython

asyncio exception handler called with incorrect context #96704

New issue
Copy link
New issue
Copy link
Closed
Closed
asyncio exception handler called with incorrect context#96704
Copy link
Assignees
gvanrossumkumaraditya303
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.12only security fixesonly security fixestopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@kumaraditya303
kumaraditya303
opened on Sep 9, 2022
Issue body actions

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()
    loop.set_exception_handler(exc_handler)
    asyncio.create_task(task())

asyncio.run(main())

On main:

Unhandled error in exception handler
context: {'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 the AssertionError. 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.

Activity

  1. kumaraditya303
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    topic-asyncio
    3.11only security fixesonly security fixes
    3.10 (EOL)end of lifeend of life
    3.12only security fixesonly security fixes
    on Sep 9, 2022
  2. kumaraditya303
    added this to asyncioon Sep 9, 2022
  3. kumaraditya303
    moved this to Todo in asyncioon Sep 9, 2022
  4. gvanrossum commented on Sep 10, 2022

    @gvanrossum
    gvanrossum
    on Sep 10, 2022
    Member
    More actions

    Maybe you can retrieve the task from the dict arg to the handler and use its ‘_context’ attribute? Or maybe we need to add a ‘get_context()’ method to Task? That’s a complex API change b/c custom task implementations though. Otherwise the implementation seems far from easy, given how many places call this?

  5. kumaraditya303 commented on Sep 11, 2022

    @kumaraditya303
    kumaraditya303
    on Sep 11, 2022
    ContributorAuthor
    More actions

    I only care about correct context in the task finalizer so that exception handler is called with correct context. I don't want to rely on any private attribute and it can change.

  6. gvanrossum
    added a commit that references this issue on Sep 11, 2022

    pythonGH-96704: Add task.get_context(), use it in call_exception_hand…

    36a7adb
  7. gvanrossum commented on Sep 11, 2022

    @gvanrossum
    gvanrossum
    on Sep 11, 2022
    Member
    More actions

    Your wish is my command. :-) (Sorry if you had a PR already, if you did we can compare notes, if you didn't you can review mine. :-)

    Reacted by Kumar Aditya
  8. kumaraditya303
    assigned
    gvanrossum
    and
    kumaraditya303
    on Sep 24, 2022
  9. gvanrossum
    added 2 commits that reference this issue on Oct 4, 2022

    pythonGH-96704: Add task.get_context(), use it in call_exception_hand…

    1d2d081

    GH-96704: Add {Task,Handle}.get_context(), use it in call_exception_h…

    8079bef
  10. gvanrossum
    closed this as completedon Oct 5, 2022
  11. Repository owner moved this from Todo to Done in asyncioon Oct 5, 2022
  12. carljm
    added a commit that references this issue on Oct 6, 2022

    Merge branch 'main' into dictwatch

    4bb5d9d
  13. mpage
    added a commit that references this issue on Oct 11, 2022

    pythonGH-96704: Add {Task,Handle}.get_context(), use it in call_excep…

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

Metadata

Metadata

Assignees

  • @gvanrossum
    gvanrossum
  • @kumaraditya303
    kumaraditya303

Labels

3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.12only security fixesonly security fixestopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

  • asyncio
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402520 — 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.