This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2014-06-30.14:05:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1404137118.99.0.781732644161.issue21886@psf.upfronthosting.co.za>
In-reply-to
Content
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/
History
Date User Action Args
2014-06-30 14:05:19vstinnersetrecipients: + vstinner
2014-06-30 14:05:18vstinnersetmessageid: <1404137118.99.0.781732644161.issue21886@psf.upfronthosting.co.za>
2014-06-30 14:05:18vstinnerlinkissue21886 messages
2014-06-30 14:05:18vstinnercreate