Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.ensure_future() breaks implicit exception chaining #84646

Closed
cjerdonek opened this issue May 1, 2020 · 3 comments
Closed

asyncio.ensure_future() breaks implicit exception chaining #84646

cjerdonek opened this issue May 1, 2020 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@cjerdonek
Copy link
Member

BPO 40466
Nosy @asvetlov, @cjerdonek, @1st1
PRs
  • bpo-29587: Enable exception chaining for gen.throw() with "yield from" #19858
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-06.10:36:11.392>
    created_at = <Date 2020-05-01.09:57:11.661>
    labels = ['3.8', 'type-bug', '3.7', '3.9', 'expert-asyncio']
    title = 'asyncio.ensure_future() breaks implicit exception chaining'
    updated_at = <Date 2020-05-06.10:36:11.391>
    user = 'https://github.com/cjerdonek'

    bugs.python.org fields:

    activity = <Date 2020-05-06.10:36:11.391>
    actor = 'chris.jerdonek'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-06.10:36:11.392>
    closer = 'chris.jerdonek'
    components = ['asyncio']
    creation = <Date 2020-05-01.09:57:11.661>
    creator = 'chris.jerdonek'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40466
    keywords = ['patch']
    message_count = 3.0
    messages = ['367832', '367925', '368232']
    nosy_count = 3.0
    nosy_names = ['asvetlov', 'chris.jerdonek', 'yselivanov']
    pr_nums = ['19858']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40466'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @cjerdonek
    Copy link
    Member Author

    This issue is about how if a coroutine is wrapped in a Task with asyncio.ensure_future(), then portions of the exception chain can be lost.

    Specifically, if you run the following code, then ValueError will be raised with exc.__context__ equal to the KeyError:

        import asyncio
    
        async def raise_error():
            raise ValueError
    
        async def main():
            try:
                raise KeyError
            except Exception as exc:
                future = raise_error()
                # Uncommenting the next line makes exc.__context__ None below.
                # future = asyncio.ensure_future(future)
            try:
                await future
            except Exception as exc:
                print(f'error: {exc!r}, context: {exc.__context__!r}')
                raise
    
    asyncio.get_event_loop().run_until_complete(main())
    

    However, if you uncomment the asyncio.ensure_future() line, then the ValueError will be raised with no context.

    I originally raised this issue a couple years ago here:
    https://mail.python.org/pipermail/async-sig/2017-November/000403.html

    There it was suggested that this was a special case of this issue:
    https://bugs.python.org/issue29587

    However, after writing code to fix that, this issue still exists.

    @cjerdonek cjerdonek added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels May 1, 2020
    @cjerdonek
    Copy link
    Member Author

    My PR is ready for review: #19858

    @cjerdonek
    Copy link
    Member Author

    Closing this as a duplicate of https://bugs.python.org/issue29587

    It turns out that, as Nathaniel first suggested, this is really just another special case of that issue (the "yield from" case as opposed to the "yield" case). It's just that the particular examples provided in that issue were for "yield" and not "yield from", so it wasn't immediately evident.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant