Skip to content

Failing ensure_future still creates a Task #70739

Closed

Description

@gordon
mannequin
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']

    Activity

    1. gordon commented on Mar 13, 2016

      gordonmannequin
      MannequinAuthor

      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

    2. gvanrossum commented on Mar 13, 2016

      @gvanrossum
      Member

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

    3. iritkatriel commented on Nov 29, 2021

      @iritkatriel
      Member

      Reproduced on 3.11.

    4. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      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.

    5. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      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

    6. gvanrossum commented on Jan 28, 2022

      @gvanrossum
      Member

      Thanks Kumar for the fix!

    7. gvanrossum commented on Jan 29, 2022

      @gvanrossum
      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

    8. transferred this issue fromon Apr 10, 2022
    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

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions