Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Script freezes on await coroutine cancelled by wait_for if this coroutine contains any await inside it's CancelledError handling #288

Closed

Description

@germn
import asyncio


async def some_func():
    try:
        await asyncio.sleep(2)
    except asyncio.CancelledError:
        # await asyncio.sleep(1)
        print('cancelled')

async def main():
    coro = some_func()
    try:
        await asyncio.wait_for(coro, timeout=1)
    except asyncio.TimeoutError:
        await coro
        print('timeout')

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Output as expected:

cancelled
timeout

But if you uncomment line inside some_func script will freeze (without printing). It can be avoided if we will use task instead of coroutine. But behavior with coroutine seems to be unclear: why everything works if line is commented and just freezes (without any error) if line is uncommented? If coroutine can't be awaited in this case shouldn't we get exception if we try to awaite it?

Windows 10, Python 3.5.0

Activity

  1. gvanrossum commented on Nov 17, 2015

    @gvanrossum
    Member

    I'm looking into this. I suspect it's something deep inside Task._step()... Not sure how easy it'll be to track it down.

  2. gvanrossum commented on Nov 17, 2015

    @gvanrossum
    Member

    So here's the problem (as you already indicated). The call await_for(coro, timeout=1) constructs a Task to wrap coro. But two lines below, when the wait_for() has timed out, the code uses await coro. I still have to dig deeper, but it appears that somehow the Task created by await_for() gets woken up when some_func() awaits the second sleep() call (the one commented out in the working version). IOW some_func() does catch the CancelledError but then await instantly gets cancelled again.

    At this point my brain hurts and I still haven't quite figured out the complete sequence of events, nor an idea of how to fix it (if we can even fix it). I've got the feeling it's similar to the scenario warned against in the docstring for run_until_complete() -- two tasks wrapping the same coroutine.

    Anyway, congrats on discovering this!

    PS. It's not unique to Python 3.5 or Windows -- translating to @asyncio.coroutine and yield from I can repro this with Python 3.4 or even 3.3, all on OS X.

  3. 1st1 commented on Nov 17, 2015

    @1st1
    Member

    I think I know what's going on here:

    import asyncio
    
    async def some_func():
        try:
            await asyncio.sleep(2)
        except asyncio.CancelledError:
            await asyncio.sleep(1)
            print('cancelled')
    
    async def main():
        coro = some_func()
        try:
            await asyncio.wait_for(coro, timeout=1)
        except asyncio.TimeoutError:
            await coro
            print('timeout')
    
    if __name__ == "__main__":
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())
    1. wait_for, creates a Task (T1) for coro.
    2. coro, creates asyncio.sleep(2) coroutine, which creates a Future (F1) and waits on it (in F1.__iter__'s yield self)
    3. wait_for, after 1 second, timeouts and cancels T1.
    4. T1, in its cancel method, cancels F1, which schedules T1._wakeup with a CancelledError.
    5. wait_for raises TimeoutError, which is caught by main().
    6. main() awaits on coro, which causes Python eval loop to call coro.__iter__().__next__()
    7. None is pushed to coro, which really pushes None to F1!
    8. F1.__iter__ generator, calls F1.result(), which raises CancelledError (important: keep in mind step # 4!!!)
    9. CancelledError propagates in coro (instance of some_func), and advances the coroutine to execute await asyncio.sleep(1) (F2).
    10. asyncio event loop runs again, and T1._wakeup scheduled in step # 4 actually happens. It throws another CancelledError in coro, right where it's awaiting await asyncio.sleep(1). And cancels the wrong future.
    11. T1 consumes the CancelledError in its _step.
    12. coro is now in an inconsistent state -- it will never be waken up by F2 because it was cancelled. main(), in turn, awaits on coro, which isn't going to receive any exceptions or values anymore.

    Now, what can we do? One option, would be to enhance CoroWrapper (wraps all coroutines in debug mode) to prohibit awaiting on a coroutine more than one time. Or, specifically, awaiting on a coroutine, that it being awaited. Thoughts?

  4. 1st1 commented on Nov 18, 2015

    @1st1
    Member

    I've updated my previous comment with a bit more accurate description of what's going on.

  5. gvanrossum commented on Nov 18, 2015

    @gvanrossum
    Member

    Thanks! Async code never ceases to amaze. I think your analysis matches my own observations. A crucial step seems to be (7), which is fundamental to how yield from (and now await) works, but also hard to grok.

    Regarding your proposed solution, I don't disagree, but how can you tell that a coroutine object is being awaited? I guess you're thinking of just instrumenting the calls to CoroWrapper? Given that we don't want to add overhead to the CPython interpreter that's the only thing we can do.

    Let's not try to fix this for 3.5.1 though. It's pretty uncommon and the workaround is easy (just wrap the offending coroutine in a Task, or don't use this pattern -- I'm guessing it occurs most likely in code written by new asyncio users who are still trying to figure out how things fit together).

  6. 1st1 commented on Nov 18, 2015

    @1st1
    Member

    Regarding your proposed solution, I don't disagree, but how can you tell that a coroutine object is being awaited? I guess you're thinking of just instrumenting the calls to CoroWrapper?

    Yes, we can use the newly added coroutine.cr_await for this purpose. See PR #293.

    Given that we don't want to add overhead to the CPython interpreter that's the only thing we can do.

    Actually, we can add this check to the CPython directly. gen_yf is a fairly fast call, and YIELD_FROM opcode is easy to modify to check if the coroutine is being awaited already (we can target specifically coroutines, and leave generators as is).

    Let's not try to fix this for 3.5.1 though.

    Up to you. The PR is fairly small, and shouldn't affect anything.

  7. gvanrossum commented on Nov 18, 2015

    @gvanrossum
    Member

    That wouldn't do anything for Python 3.3 or 3.4. But maybe that's okay.
    Let's see that PR...

    On Wed, Nov 18, 2015 at 9:19 AM, Yury Selivanov notifications@github.com
    wrote:

    Regarding your proposed solution, I don't disagree, but how can you tell
    that a coroutine object is being awaited? I guess you're thinking of just
    instrumenting the calls to CoroWrapper?

    Yes, we can use the newly added coroutine.cr_await for this purpose. See
    PR #293 #293.

    Given that we don't want to add overhead to the CPython interpreter that's
    the only thing we can do.

    Actually, we can add this check to the CPython directly. gen_yf is a
    fairly fast call, and YIELD_FROM opcode is easy to modify to check if the
    coroutine is being awaited already (we can target specifically coroutines,
    and leave generators as is).

    Let's not try to fix this for 3.5.1 though.

    Up to you. The PR is fairly small, and shouldn't affect anything.

    —
    Reply to this email directly or view it on GitHub
    #288 (comment).

    --Guido van Rossum (python.org/~guido)

  8. reopened this on Nov 18, 2015
  9. 1st1 commented on Nov 18, 2015

    @1st1
    Member

    That wouldn't do anything for Python 3.3 or 3.4. But maybe that's okay.

    Yes. We can probably hack something in CoroWrapper for 3.3 and 3.4, but even if that's possible, it would be fragile.

  10. 1st1 commented on Nov 18, 2015

    @1st1
    Member

    @germn PR #293 has been merged. Starting with 3.5.1, in debug mode, your code would crash instead of freezing.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions