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

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

Closed
achimnol mannequin opened this issue Jan 10, 2018 · 3 comments
Closed

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

achimnol mannequin opened this issue Jan 10, 2018 · 3 comments
Labels
3.7 (EOL) end of life topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@achimnol
Copy link
Mannequin

achimnol mannequin commented Jan 10, 2018

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']

    @achimnol achimnol mannequin added topic-asyncio type-bug An unexpected behavior, bug, or error labels Jan 10, 2018
    @achimnol
    Copy link
    Mannequin Author

    achimnol mannequin commented Jan 10, 2018

    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.

    @njsmith
    Copy link
    Contributor

    njsmith commented Jan 10, 2018

    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

    @njsmith njsmith added the 3.7 (EOL) end of life label Jan 10, 2018
    @1st1
    Copy link
    Member

    1st1 commented Oct 22, 2019

    Was fixed as part of bpo-30773.

    @1st1 1st1 closed this as completed Oct 22, 2019
    @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 topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants