New submission from STINNER Victor:

I noticed the following warning in test_asyncio (see above). It understand that 
when the asyncio Future ("destination" in _chain_future) is cancelled, 
_call_check_cancel() calls source_loop.call_soon_threadsafe(source.cancel) 
where source is a concurrent.futures.Future object.

Then futures.Future.cancel() calls self._invoke_callbacks() which calls 
_call_set_state() of _chain_future(). _call_set_state() calls 
dest_loop.call_soon_threadsafe(_set_state, destination, source) but at this 
point, the event loop is closed. Morever, destination (the asyncio future) is 
already cancelled, but it doesn't make sense to try to copy the state: source 
and destination are already cancelled.

I suggest to modify _call_set_state() to do nothing if destination is already 

    def _call_set_state(source):
        if destination.cancelled():

*But* I see that futures.Future has a set_running_or_notify_cancel() method 
which seems to be important to call. Maybe we should call it in this case?

    def _call_set_state(source):
        if destination.cancelled():


The warning:

(test.test_asyncio.test_selector_events.BaseSelectorEventLoopTests) ... 
exception calling callback for <Future at 0xec840d1e28 state=finished returned 
Traceback (most recent call last):
  File "D:\buildarea\3.6.bolen-windows8\build\lib\concurrent\futures\", 
line 297, in _invoke_callbacks
  File "D:\buildarea\3.6.bolen-windows8\build\lib\asyncio\", line 
462, in _call_set_state
    dest_loop.call_soon_threadsafe(_set_state, destination, source)
  File "D:\buildarea\3.6.bolen-windows8\build\lib\asyncio\", line 
603, in call_soon_threadsafe
    handle = self._call_soon(callback, args)
  File "D:\buildarea\3.6.bolen-windows8\build\lib\asyncio\", line 
577, in _call_soon
  File "D:\buildarea\3.6.bolen-windows8\build\lib\asyncio\", line 
356, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

components: asyncio
messages: 277107
nosy: gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: asyncio: wrap_future() doesn't handle cancellation correctly
versions: Python 3.5, Python 3.6, Python 3.7

Python tracker <>
Python-bugs-list mailing list

Reply via email to