Title: AssertionError on await of Future returned by asyncio.wrap_future
Type: Stage: resolved
Components: asyncio, Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, jhaydaman, yselivanov
Priority: normal Keywords: patch

Created on 2018-04-06 18:02 by jhaydaman, last changed 2018-05-30 17:53 by asvetlov. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7056 merged jhaydaman, 2018-05-22 14:57
Messages (4)
msg315030 - (view) Author: Jason Haydaman (jhaydaman) * Date: 2018-04-06 18:02
When the concurrent Future wrapped by asyncio.wrap_future has set_result or set_exception called multiple times, I get the following traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/", line 145, in _run
  File "/usr/local/lib/python3.6/asyncio/", line 399, in _set_state
    _copy_future_state(other, future)
  File "/usr/local/lib/python3.6/asyncio/", line 369, in _copy_future_state
    assert not dest.done()

Minimal reproducible example:

import asyncio
import concurrent.futures

f = concurrent.futures.Future()
async_f = asyncio.wrap_future(f)

loop = asyncio.get_event_loop()

The documentation says that set_result and set_exception are only meant for Executors, so, arguably using them outside of that context would fall into undocumented behavior rather than a bug. But it still seems like it should be the second set_result that raises something like InvalidStateError. Alternatively, this can be avoided if _copy_future_state checks for done in addition to cancellation. What should happen here?
msg315031 - (view) Author: Jason Haydaman (jhaydaman) * Date: 2018-04-06 18:15
May also be worth pointing out that even in the case of only calling set_result once, _done_callbacks still has _chain_future in it:

import asyncio
import concurrent.futures

f = concurrent.futures.Future()
async_f = asyncio.wrap_future(f)

loop = asyncio.get_event_loop()

>>> 1
>>> [<function _chain_future.<locals>._call_set_state at 0x7f1687f3c620>]

Should that be cleared by that point?
msg317224 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-05-21 09:34
Raising `InvalidStateError` sounds perfect.

Would you make a pull request?
msg318157 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-05-30 07:15
New changeset 0a28c0d12ee7201de039ced4d815f57f1f8fd48c by Andrew Svetlov (jhaydaman) in branch 'master':
bpo-33238: Add InvalidStateError to concurrent.futures. (GH-7056)
Date User Action Args
2018-05-30 17:53:49asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-30 07:15:15asvetlovsetmessages: + msg318157
2018-05-22 14:57:43jhaydamansetkeywords: + patch
stage: patch review
pull_requests: + pull_request6690
2018-05-21 09:35:14asvetlovsetversions: + Python 3.8, - Python 3.6
2018-05-21 09:34:42asvetlovsetmessages: + msg317224
2018-04-06 18:15:52jhaydamansetmessages: + msg315031
2018-04-06 18:02:45jhaydamancreate