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

Failing ensure_future still creates a Task #70739

Closed
gordon mannequin opened this issue Mar 13, 2016 · 7 comments
Closed

Failing ensure_future still creates a Task #70739

gordon mannequin opened this issue Mar 13, 2016 · 7 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes easy topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@gordon
Copy link
Mannequin

gordon mannequin commented Mar 13, 2016

BPO 26552
Nosy @gvanrossum, @1st1, @carlosdamazio, @iritkatriel, @kumaraditya303
PRs
  • bpo-26552: Close dangling coroutine or future if loop is closed #30287
  • bpo-26552: Fixed case where failing asyncio.ensure_future did not close the coroutine #30288
  • [3.10] bpo-26552: Fixed case where failing asyncio.ensure_future did not c… …lose the coroutine #31003
  • 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 2022-01-28.22:26:00.502>
    created_at = <Date 2016-03-13.11:46:10.277>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'expert-asyncio']
    title = 'Failing ensure_future still creates a Task'
    updated_at = <Date 2022-01-29.06:57:25.632>
    user = 'https://bugs.python.org/gordon'

    bugs.python.org fields:

    activity = <Date 2022-01-29.06:57:25.632>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-28.22:26:00.502>
    closer = 'gvanrossum'
    components = ['asyncio']
    creation = <Date 2016-03-13.11:46:10.277>
    creator = 'gordon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26552
    keywords = ['patch', 'easy']
    message_count = 7.0
    messages = ['261696', '261701', '407294', '412036', '412038', '412039', '412057']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'yselivanov', 'gordon', 'dmzz', 'iritkatriel', 'kumaraditya']
    pr_nums = ['30287', '30288', '31003']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26552'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @gordon
    Copy link
    Mannequin Author

    gordon mannequin commented Mar 13, 2016

    When calling asyncio.ensure_future() on a coroutine, and if the loop is closed, ensure_future() will raise a RuntimeError.

    However, it still creates a Task, which will generate a RuntimeWarning that we can’t fix since there is no way to cancel the Task.

    Here is the code to reproduce the bug:

    import asyncio
    
    l = asyncio.get_event_loop()
    l.close()
    
    async def foo():
        pass

    try:
    # Since the exception raises here, fut is never set
    # so we can't call fut.cancel()
    fut = asyncio.ensure_future(foo())
    except RuntimeError:
    pass

    # stderr:
    # aio.py:12: RuntimeWarning: coroutine 'foo' was never awaited
    # pass

    @gordon gordon mannequin added topic-asyncio type-bug An unexpected behavior, bug, or error labels Mar 13, 2016
    @gvanrossum
    Copy link
    Member

    Sounds like an easy fix. Could you submit a patch?

    @iritkatriel
    Copy link
    Member

    Reproduced on 3.11.

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Nov 29, 2021
    @gvanrossum
    Copy link
    Member

    So I just realized that the OP's description is slightly misleading. (Their code is spot on though!)

    The code does not create an unwaited-for *task*, assuming that "task" refers to the asyncio.Task class.

    What is created is a *coroutine* object that's never awaited (as the quoted RuntimeWarning message says: "coroutine 'foo' was never awaited").

    So the thing that needs to be closed in the bowels of _ensure_future() is indeed the argument (coro_or_future), in case it is a coroutine object.

    @gvanrossum
    Copy link
    Member

    New changeset 24cc641 by Kumar Aditya in branch 'main':
    bpo-26552: Fixed case where failing asyncio.ensure_future did not close the coroutine (bpo-30288)
    24cc641

    @gvanrossum
    Copy link
    Member

    Thanks Kumar for the fix!

    @gvanrossum
    Copy link
    Member

    New changeset a5451c9 by Kumar Aditya in branch '3.10':
    bpo-26552: Fixed case where failing asyncio.ensure_future did not close the coroutine (bpo-30288) (bpo-31003)
    a5451c9

    @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.9 only security fixes 3.10 only security fixes 3.11 only security fixes easy topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants