Skip to content

Closing async generator while it is running does not raise an exception #76707

Closed

Description

@achimnol
mannequin
BPO 32526
Nosy @njsmith, @asvetlov, @1st1, @achimnol
PRs
  • bpo-32526: Closing async generator while it is running does not raise an exception #5182
  • 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 2019-10-22.22:24:16.987>
    created_at = <Date 2018-01-10.09:31:53.662>
    labels = ['type-bug', '3.7', 'expert-asyncio']
    title = 'Closing async generator while it is running does not raise an exception'
    updated_at = <Date 2019-10-22.22:24:16.986>
    user = 'https://github.com/achimnol'

    bugs.python.org fields:

    activity = <Date 2019-10-22.22:24:16.986>
    actor = 'yselivanov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-22.22:24:16.987>
    closer = 'yselivanov'
    components = ['asyncio']
    creation = <Date 2018-01-10.09:31:53.662>
    creator = 'achimnol'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32526
    keywords = ['patch']
    message_count = 3.0
    messages = ['309760', '309783', '355162']
    nosy_count = 4.0
    nosy_names = ['njs', 'asvetlov', 'yselivanov', 'achimnol']
    pr_nums = ['5182']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32526'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. added
      type-bug
      on Jan 10, 2018
    2. achimnol commented on Jan 10, 2018

      achimnolmannequin
      MannequinAuthor

      Here is the minimal example code:

      https://gist.github.com/achimnol/965a6aecf7b1f96207abf11469b68965

      Just run this code using "python -m pytest -s test.py" to see what happens.
      (My setup uses Python 3.6.4 and pytest 3.3.2 on macOS High Sierra 10.13.2)

      I tried the same logic using synchornous APIs such as threading.Thread / time.sleep instead of loop.create_task / asyncio.sleep, it raised "ValueError: generator already executing" when tried to close the generator.

    3. njsmith commented on Jan 10, 2018

      @njsmith
      Contributor

      It looks like Python's tracking the "running" state of async generators wrong: we should have ag_running set to True when we enter asend/athrow/aclose and False when we exit, but instead it's being toggled back and forth on each *inner* send/throw on the individual coroutines.

      Here's a minimal reproducer (using some random recent checkout of master):

      >>> async def f():
      ...     await asyncio.sleep(1)
      ...     yield
      ... 
      >>> ag = f()
      >>> asend_coro = ag.asend(None)
      >>> fut = asend_coro.send(None)
      # Logically, ag.asend is still running, waiting for that sleep to
      # finish, but we have lost track:
      >>> ag.ag_running
      False
      # We can start another call to asend() going simultaneously
      >>> fut.set_result(None)
      >>> send_coro2 = ag.asend(None)
      >>> send_coro2.send(None)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      StopIteration

      It looks like async_generator did handle this case correctly, but didn't have a test case. I just added one: njsmith/async_generator@339fc63

    4. 1st1 commented on Oct 22, 2019

      @1st1
      Member

      Was fixed as part of bpo-30773.

    5. 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

      3.7 (EOL)topic-asynciotype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions