Ok, I found a way to reproduce the error InvalidStateError in asyncio. I'm not sure that it's the same the error in #21447.
Output of attached bug.py in debug mode:
---
Exception in callback Future.set_result(None)
handle: <TimerHandle when=79580.878306285 Future.set_result(None)>
source_traceback: Object created at (most recent call last):
File "/home/haypo/bug.py", line 11, in <module>
loop.run_until_complete(task2)
File "/home/haypo/prog/python/default/Lib/asyncio/base_events.py", line 239, in run_until_complete
self.run_forever()
File "/home/haypo/prog/python/default/Lib/asyncio/base_events.py", line 212, in run_forever
self._run_once()
File "/home/haypo/prog/python/default/Lib/asyncio/base_events.py", line 912, in _run_once
handle._run()
File "/home/haypo/prog/python/default/Lib/asyncio/events.py", line 96, in _run
self._callback(*self._args)
File "/home/haypo/prog/python/default/Lib/asyncio/tasks.py", line 241, in _step
result = next(coro)
File "/home/haypo/prog/python/default/Lib/asyncio/coroutines.py", line 72, in __next__
return next(self.gen)
File "/home/haypo/prog/python/default/Lib/asyncio/tasks.py", line 487, in sleep
h = future._loop.call_later(delay, future.set_result, result)
Traceback (most recent call last):
File "/home/haypo/prog/python/default/Lib/asyncio/events.py", line 96, in _run
self._callback(*self._args)
File "/home/haypo/prog/python/default/Lib/asyncio/futures.py", line 326, in set_result
raise InvalidStateError('{}: {!r}'.format(self._state, self))
asyncio.futures.InvalidStateError: CANCELLED: <Future cancelled>
---
The fix is to replace the following line of sleep():
---
h = future._loop.call_later(delay, future.set_result, result)
---
with:
---
def maybe_set_result(future, result):
if not future.cancelled():
future.set_result(result)
h = future._loop.call_later(delay, maybe_set_result, future, result)
---
This generic issue was already discussed there:
https://groups.google.com/forum/?fromgroups#!searchin/python-tulip/set_result$20InvalidStateError/python-tulip/T1sxLqjuoVY/YghF-YsgosgJ
A patch was also proposed:
https://codereview.appspot.com/69870048/
|