You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
Repository navigation
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
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?
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.
coro, creates asyncio.sleep(2) coroutine, which creates a Future (F1) and waits on it (in F1.__iter__'s yield self)
wait_for, after 1 second, timeouts and cancels T1.
T1, in its cancel method, cancels F1, which schedulesT1._wakeup with a CancelledError.
wait_for raises TimeoutError, which is caught by main().
main() awaits on coro, which causes Python eval loop to call coro.__iter__().__next__()
None is pushed to coro, which really pushes None to F1!
F1.__iter__ generator, calls F1.result(), which raises CancelledError (important: keep in mind step # 4!!!)
CancelledError propagates in coro (instance of some_func), and advances the coroutine to execute await asyncio.sleep(1) (F2).
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.
T1 consumes the CancelledError in its _step.
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?
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).
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.
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).
Activity
gvanrossum commentedon Nov 17, 2015
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.
gvanrossum commentedon Nov 17, 2015
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 thewait_for()has timed out, the code usesawait 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 thenawaitinstantly 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.coroutineandyield fromI can repro this with Python 3.4 or even 3.3, all on OS X.1st1 commentedon Nov 17, 2015
I think I know what's going on here:
wait_for, creates aTask(T1) forcoro.coro, createsasyncio.sleep(2)coroutine, which creates aFuture(F1) and waits on it (inF1.__iter__'syield self)wait_for, after 1 second, timeouts and cancels T1.T1, in itscancelmethod, cancels F1, which schedulesT1._wakeupwith aCancelledError.wait_forraisesTimeoutError, which is caught bymain().main()awaits oncoro, which causes Python eval loop to callcoro.__iter__().__next__()Noneis pushed tocoro, which really pushesNoneto F1!F1.__iter__generator, callsF1.result(), which raisesCancelledError(important: keep in mind step # 4!!!)CancelledErrorpropagates incoro(instance ofsome_func), and advances the coroutine to executeawait asyncio.sleep(1)(F2).T1._wakeupscheduled in step # 4 actually happens. It throws anotherCancelledErrorincoro, right where it's awaitingawait asyncio.sleep(1). And cancels the wrong future.CancelledErrorin its_step.corois now in an inconsistent state -- it will never be waken up by F2 because it was cancelled.main(), in turn, awaits oncoro, 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?1st1 commentedon Nov 18, 2015
I've updated my previous comment with a bit more accurate description of what's going on.
gvanrossum commentedon Nov 18, 2015
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 nowawait) 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).
1st1 commentedon Nov 18, 2015
Yes, we can use the newly added
coroutine.cr_awaitfor this purpose. See PR #293.Actually, we can add this check to the CPython directly.
gen_yfis a fairly fast call, andYIELD_FROMopcode is easy to modify to check if the coroutine is being awaited already (we can target specifically coroutines, and leave generators as is).Up to you. The PR is fairly small, and shouldn't affect anything.
gvanrossum commentedon Nov 18, 2015
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:
--Guido van Rossum (python.org/~guido)
1st1 commentedon Nov 18, 2015
Yes. We can probably hack something in
CoroWrapperfor 3.3 and 3.4, but even if that's possible, it would be fragile.1st1 commentedon Nov 18, 2015
@germn PR #293 has been merged. Starting with 3.5.1, in debug mode, your code would crash instead of freezing.