Error doing job task was destroyed but it is pending

The problem For something like a year I have seen this in the log every single time I start Home Assistant. [homeassistant] Error doing job: Task was destroyed but it is pending! I cannot trace wha...

The problem

For something like a year I have seen this in the log every single time I start Home Assistant.

[homeassistant] Error doing job: Task was destroyed but it is pending!

I cannot trace what causes it. Last year people reported it as related to automations but I tried to remove all automations that run at start and it did not make a difference.

I have enabled debugging in the HA log and pasted the entire log below

What is version of Home Assistant Core has the issue?

2021.5.2

What was the last working version of Home Assistant Core?

At least a year ago

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Harmony

Link to integration documentation on our website

https://www.home-assistant.io/integrations/harmony/

Example YAML snippet

Anything in the logs that might be useful for us?

021-05-11 23:05:17 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(None):8009] Resolved service ServiceInfo(type='mdns', data='Google-Home-Mini-ad732ef4ef84f7a56c4399774142397e._googlecast._tcp.local.') to 192.168.1.76:8009
2021-05-11 23:05:17 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Connecting to 192.168.1.76:8009
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.helpers.translation] Cache miss for en-GB: sensor.deconz, sensor.amcrest, sensor.mobile_app, switch.deconz, binary_sensor.amcrest, light.deconz, binary_sensor.deconz, camera.amcrest, device_tracker.mobile_app
2021-05-11 23:05:17 DEBUG (MainThread) [aioesphomeapi.connection] 192.168.1.93: Got message of type <class 'api_pb2.ConnectResponse'>: 
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "media_player.ultracast_bedroom", "old_state": {"entity_id": "media_player.ultracast_bedroom", "state": "unavailable", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152449}, "last_changed": "2021-05-11T21:05:17.454290+00:00", "last_updated": "2021-05-11T21:05:17.454290+00:00", "context": {"id": "b2e95a68c3274fff9e402b24439fec60", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "media_player.ultracast_bedroom", "state": "off", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152461}, "last_changed": "2021-05-11T21:05:17.980836+00:00", "last_updated": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [(None):8009] connection listener: ffff9a046ca0 (CastStatusListener) ConnectionStatus(status='CONNECTING', address=NetworkAddress(address=None, port=8009))
2021-05-11 23:05:17 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Received cast device connection status: CONNECTING
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.dial] get_info_from_service resolved service ServiceInfo(type='mdns', data='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.') to service_info ServiceInfo(type='_googlecast._tcp.local.', name='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.', addresses=[b'xc0xa8x01G'], port=8009, weight=0, priority=0, server='1951c793-118f-e9b7-c821-907a48a42ed1.local.', properties={b'id': b'1951c793118fe9b7c821907a48a42ed1', b'cd': b'87BE3FA9506216A9626E2C5A8B66F886', b'rm': b'5D0AFBCA4344906C', b've': b'05', b'md': b'Google Nest Hub', b'ic': b'/setup/icon.png', b'fn': b'Bedroom Display', b'ca': b'233477', b'st': b'0', b'bs': b'FA8FCA609DBA', b'nf': b'1', b'rs': b''})
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(None):8009] Resolved service ServiceInfo(type='mdns', data='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.') to 192.168.1.71:8009
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Connecting to 192.168.1.71:8009
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "media_player.ultracast_bedroom", "old_state": {"entity_id": "media_player.ultracast_bedroom", "state": "unavailable", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152449}, "last_changed": "2021-05-11T21:05:17.454290+00:00", "last_updated": "2021-05-11T21:05:17.454290+00:00", "context": {"id": "b2e95a68c3274fff9e402b24439fec60", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "media_player.ultracast_bedroom", "state": "off", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152461}, "last_changed": "2021-05-11T21:05:17.980836+00:00", "last_updated": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sensor.living_room_camera_ptz_preset", "old_state": null, "new_state": {"entity_id": "sensor.living_room_camera_ptz_preset", "state": "4", "attributes": {"friendly_name": "Living room camera PTZ Preset", "icon": "mdi:camera-iris"}, "last_changed": "2021-05-11T21:05:17.986699+00:00", "last_updated": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [homeassistant.components.amcrest.sensor] Updating Kitchen camera PTZ Preset sensor
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> HTTP query 5: http://192.168.1.78:80/cgi-bin/ptz.cgi?action=getPresets&channel=0
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> Running query 5 attempt 1
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [homeassistant.components.amcrest.binary_sensor] Updating Kitchen camera Motion Detected binary sensor
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> HTTP query 6: http://192.168.1.78:80/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> Running query 6 attempt 1
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "binary_sensor.living_room_camera_online", "old_state": null, "new_state": {"entity_id": "binary_sensor.living_room_camera_online", "state": "on", "attributes": {"friendly_name": "Living room camera Online", "device_class": "connectivity"}, "last_changed": "2021-05-11T21:05:17.987521+00:00", "last_updated": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sensor.living_room_camera_ptz_preset", "old_state": null, "new_state": {"entity_id": "sensor.living_room_camera_ptz_preset", "state": "4", "attributes": {"friendly_name": "Living room camera PTZ Preset", "icon": "mdi:camera-iris"}, "last_changed": "2021-05-11T21:05:17.986699+00:00", "last_updated": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] connection listener: ffff9a046ca0 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.71', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "binary_sensor.living_room_camera_online", "old_state": null, "new_state": {"entity_id": "binary_sensor.living_room_camera_online", "state": "on", "attributes": {"friendly_name": "Living room camera Online", "device_class": "connectivity"}, "last_changed": "2021-05-11T21:05:17.987521+00:00", "last_updated": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 INFO (MainThread) [homeassistant.components.esphome] Successfully connected to 192.168.1.93
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] Thread started...
2021-05-11 23:05:18 DEBUG (MainThread) [aioesphomeapi.connection] 192.168.1.93: Sending <class 'api_pb2.DeviceInfoRequest'>: 
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] connection listener: ffff977425e0 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.70', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-15) [homeassistant.components.cast.media_player] [media_player.office_speaker Office Speaker] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-15) [homeassistant.components.cast.media_player] [media_player.office_speaker Office Speaker] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> Query 12 worked. Exit code: <200>
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> HTTP query 13: http://192.168.1.61:80/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> Running query 13 attempt 1
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] Thread started...
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] connection listener: ffff97685280 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.76', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-16) [homeassistant.components.cast.media_player] [media_player.workshop_speaker Workshop Speaker] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-16) [homeassistant.components.cast.media_player] [media_player.workshop_speaker Workshop Speaker] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 ERROR (Thread-16) [homeassistant] Error doing job: Task was destroyed but it is pending!
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Received: Message urn:x-cast:com.google.cast.receiver from receiver-0 to sender-0: {'requestId': 1, 'status': {'userEq': {'high_shelf': {'frequency': 4500.0, 'gain_db': 0.0, 'quality': 0.707}, 'low_shelf': {'frequency': 150.0, 'gain_db': 0.0, 'quality': 0.707}, 'max_peaking_eqs': 0, 'peaking_eqs': []}, 'volume': {'controlType': 'master', 'level': 0.44999998807907104, 'muted': False, 'stepInterval': 0.019999999552965164}}, 'type': 'RECEIVER_STATUS'}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.controllers] Received status: CastStatus(is_active_input=None, is_stand_by=None, volume_level=0.44999998807907104, volume_muted=False, app_id=None, display_name=None, namespaces=[], session_id=None, transport_id=None, status_text='', icon_url=None, volume_control_type='master')
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] Thread started...

Additional information

I cannot see what it is that causes the issue. I have several Google Assistant devices. 7 to be exact

The ERROR is in an area of the log where HA inits connection to a Google Assistant box.

But is it related? I cannot see anything failing. It just spits out the single error in the middle of the log with nothing I can see that fails afterwards

EDIT. See discussion below. Issue seems related to the Harmony integration and not at all Google.

In this post, we will see how to resolve ‘Task was destroyed but it is pending’ when running multiple tasks inside one asyncio loop

Question:

Inside a microservice I am running two separate asyncio tasks; one for incoming grpc calls and one for reading a redis queue. The main looks kinda like this:

Then what happens is that when a specific grpc call comes, the mainRedisLoop() task is somehow destroyed and throws the error:

Could it be some of the return statements in the logic of processing grpc requests from my side are causing the task to suddenly close?

Here is also the mainRedisLoop function:

Besides that:
From my experience you should also consider calling process_message as a task instead of a coroutine depending on how long it takes.
I personally had the problem that a running task was destroyed even when keeping a reference in a set. Inside the task was a long running coroutine. Running this coroutine as a task fixed it.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

Issue

async def run_check(shell_command):
    p = await asyncio.create_subprocess_shell(shell_command,
                    stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    fut = p.communicate()
    try:
        pcap_run = await asyncio.wait_for(fut, timeout=5)
    except asyncio.TimeoutError:
        p.kill()
        await p.communicate()

def get_coros(pcap_list):
    for pcap_loc in pcap_list:
        for pcap_check in get_pcap_executables():
            tmp_coro = (run_check('{args}'
            .format(e=sys.executable, args=args)))
            if tmp_coro != False:
                coros.append(tmp_coro)
     return coros

async def main():
    pcap_list_gen = print_dir_cointent()
    for pcap_list in pcap_list_gen:
        p_coros = get_coros(pcap_list)
        for f in asyncio.as_completed(p_coros):
            res = await f

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

pcap_list_gen is a generator of PCAP lists which contain multile lists.
I get [OS: Error: To many open file] if I pass all pcaps in a single list, so decided to group them into list of smaller size and process one at a time.

Each pcap_list is a list of multiple PCAPS.
I want the next iteration of the loop to only start once the first iteration is finished.

for pcap_list in pcap_list_gen:
    p_coros = get_coros(pcap_list)
        for f in asyncio.as_completed(p_coros):
            res = await f

As per my knowledge: The first loop is getting executed properly asa the iteration goes to next loop it throws error.

Traceback:

Exception ignored in: <generator object BaseSubprocessTransport._connect_pipes at 0x7f7b7c165830>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 188, in _connect_pipes
    waiter.set_exception(exc)
  File "/usr/lib/python3.5/asyncio/futures.py", line 349, in set_exception
    self._schedule_callbacks()
  File "/usr/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
    self._loop.call_soon(callback, self)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
    handle = self._call_soon(callback, args)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
    self._check_closed()
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
[ERROR]  Task was destroyed but it is pending!
task: <Task pending coro=<BaseSubprocessTransport._connect_pipes() done, defined at /usr/lib/python3.5/asyncio/base_subprocess.py:156> wait_for=<Future pending cb=[Task._wakeup()]>>

More logs

RuntimeError: Event loop is closed
Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_UnixSubprocessTransport closed pid=70435 running stdin=<_UnixWritePipeTransport closing fd=12 open>>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 126, in __del__
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 101, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 568, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 560, in write_eof
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
[ERROR]  Task exception was never retrieved
future: <Task finished coro=<ClassificationCheck.run_check() done, defined at ./regression.py:159> exception=RuntimeError('cannot reuse already awaited coroutine',)>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
RuntimeError: cannot reuse already awaited coroutine

O/P of _p_coros_ after for loop iteration in main() as

P_COROS(1st iteration):  [<coroutine object ClassificationCheck.run_check at 0x7f746d984ca8>, <coroutine object ClassificationCheck.run_check at 0x7f746d984db0>, <coroutine object ClassificationCheck.run_check at 0x7f746d984f68>, <coroutine object ClassificationCheck.run_check at 0x7f746d984fc0>]
awating in block 'finally'
awating in block 'finally'
awating in block 'finally'
awating in block 'finally'
P_COROS(2nd iteration):  [<coroutine object ClassificationCheck.run_check at 0x7f746d984ca8>, <coroutine object ClassificationCheck.run_check at 0x7f746d984db0>, <coroutine object ClassificationCheck.run_check at 0x7f746d984f68>, <coroutine object ClassificationCheck.run_check at 0x7f746d984fc0>, <coroutine object ClassificationCheck.run_check at 0x7f746d943048>, <coroutine object ClassificationCheck.run_check at 0x7f746d9430f8>]
Traceback (most recent call last):
  File "./regression.py", line 325, in <module>
    loop.run_until_complete(ClassCheck.main())
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "./regression.py", line 201, in main
    res = await f
  File "/usr/lib/python3.5/asyncio/tasks.py", line 492, in _wait_for_one
    return f.result()  # May raise f.exception().
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
RuntimeError: cannot reuse already awaited coroutine
Exception ignored in: <bound method BaseEventLoop.__del__ of <_UnixSelectorEventLoop running=False closed=True debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_events.py", line 431, in __del__
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 58, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 139, in remove_signal_handler
  File "/usr/lib/python3.5/signal.py", line 47, in signal
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object
Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_UnixSubprocessTransport closed pid=89531 running stdin=<_UnixWritePipeTransport closing fd=8 open>>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 126, in __del__
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 101, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 568, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 560, in write_eof
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed

Solution

Warning says that at the moment you call loop.close() something related to process still running. I guess you should wait() for it’s termination (read also note at the link). Try this:

try:
    pcap_run = await asyncio.wait_for(fut, timeout=5)
except asyncio.TimeoutError:
    p.terminate()
finally:
    await p.wait()

Upd:

Oh, you probably used global coros variable:

def get_coros(pcap_list):
    coros = []  # <--------------- create new list to fill
    for pcap_loc in pcap_list:
        for pcap_check in get_pcap_executables():
            tmp_coro = (run_check('{args}'
            .format(e=sys.executable, args=args)))
            if tmp_coro != False:
                coros.append(tmp_coro)
     return coros

Answered By — Mikhail Gerasimov

For something like a year I have seen this in the log every single time I start Home Assistant.

I cannot trace what causes it. Last year people reported it as related to automations but I tried to remove all automations that run at start and it did not make a difference.

021-05-11 23:05:17 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(None):8009] Resolved service ServiceInfo(type='mdns', data='Google-Home-Mini-ad732ef4ef84f7a56c4399774142397e._googlecast._tcp.local.') to 192.168.1.76:8009
2021-05-11 23:05:17 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Connecting to 192.168.1.76:8009
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.helpers.translation] Cache miss for en-GB: sensor.deconz, sensor.amcrest, sensor.mobile_app, switch.deconz, binary_sensor.amcrest, light.deconz, binary_sensor.deconz, camera.amcrest, device_tracker.mobile_app
2021-05-11 23:05:17 DEBUG (MainThread) [aioesphomeapi.connection] 192.168.1.93: Got message of type <class 'api_pb2.ConnectResponse'>: 
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "media_player.ultracast_bedroom", "old_state": {"entity_id": "media_player.ultracast_bedroom", "state": "unavailable", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152449}, "last_changed": "2021-05-11T21:05:17.454290+00:00", "last_updated": "2021-05-11T21:05:17.454290+00:00", "context": {"id": "b2e95a68c3274fff9e402b24439fec60", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "media_player.ultracast_bedroom", "state": "off", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152461}, "last_changed": "2021-05-11T21:05:17.980836+00:00", "last_updated": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [(None):8009] connection listener: ffff9a046ca0 (CastStatusListener) ConnectionStatus(status='CONNECTING', address=NetworkAddress(address=None, port=8009))
2021-05-11 23:05:17 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Received cast device connection status: CONNECTING
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.dial] get_info_from_service resolved service ServiceInfo(type='mdns', data='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.') to service_info ServiceInfo(type='_googlecast._tcp.local.', name='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.', addresses=[b'xc0xa8x01G'], port=8009, weight=0, priority=0, server='1951c793-118f-e9b7-c821-907a48a42ed1.local.', properties={b'id': b'1951c793118fe9b7c821907a48a42ed1', b'cd': b'87BE3FA9506216A9626E2C5A8B66F886', b'rm': b'5D0AFBCA4344906C', b've': b'05', b'md': b'Google Nest Hub', b'ic': b'/setup/icon.png', b'fn': b'Bedroom Display', b'ca': b'233477', b'st': b'0', b'bs': b'FA8FCA609DBA', b'nf': b'1', b'rs': b''})
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(None):8009] Resolved service ServiceInfo(type='mdns', data='Google-Nest-Hub-1951c793118fe9b7c821907a48a42ed1._googlecast._tcp.local.') to 192.168.1.71:8009
2021-05-11 23:05:17 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Connecting to 192.168.1.71:8009
2021-05-11 23:05:17 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "media_player.ultracast_bedroom", "old_state": {"entity_id": "media_player.ultracast_bedroom", "state": "unavailable", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152449}, "last_changed": "2021-05-11T21:05:17.454290+00:00", "last_updated": "2021-05-11T21:05:17.454290+00:00", "context": {"id": "b2e95a68c3274fff9e402b24439fec60", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "media_player.ultracast_bedroom", "state": "off", "attributes": {"friendly_name": "Ultracast Bedroom", "supported_features": 152461}, "last_changed": "2021-05-11T21:05:17.980836+00:00", "last_updated": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.980836+00:00", "context": {"id": "5d8f151df0712d26ca31308606307e83", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sensor.living_room_camera_ptz_preset", "old_state": null, "new_state": {"entity_id": "sensor.living_room_camera_ptz_preset", "state": "4", "attributes": {"friendly_name": "Living room camera PTZ Preset", "icon": "mdi:camera-iris"}, "last_changed": "2021-05-11T21:05:17.986699+00:00", "last_updated": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [homeassistant.components.amcrest.sensor] Updating Kitchen camera PTZ Preset sensor
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> HTTP query 5: http://192.168.1.78:80/cgi-bin/ptz.cgi?action=getPresets&channel=0
2021-05-11 23:05:18 DEBUG (SyncWorker_1) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> Running query 5 attempt 1
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [homeassistant.components.amcrest.binary_sensor] Updating Kitchen camera Motion Detected binary sensor
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> HTTP query 6: http://192.168.1.78:80/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion
2021-05-11 23:05:18 DEBUG (SyncWorker_13) [amcrest.http] <Amcrest2:AMC00104161DE28N5K> Running query 6 attempt 1
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473529230624] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "binary_sensor.living_room_camera_online", "old_state": null, "new_state": {"entity_id": "binary_sensor.living_room_camera_online", "state": "on", "attributes": {"friendly_name": "Living room camera Online", "device_class": "connectivity"}, "last_changed": "2021-05-11T21:05:17.987521+00:00", "last_updated": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sensor.living_room_camera_ptz_preset", "old_state": null, "new_state": {"entity_id": "sensor.living_room_camera_ptz_preset", "state": "4", "attributes": {"friendly_name": "Living room camera PTZ Preset", "icon": "mdi:camera-iris"}, "last_changed": "2021-05-11T21:05:17.986699+00:00", "last_updated": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.986699+00:00", "context": {"id": "b92419c1db45017dfa7003670cebf94d", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] connection listener: ffff9a046ca0 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.71', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-17) [homeassistant.components.cast.media_player] [media_player.bedroom_display Bedroom Display] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection] [281473384830864] Sending {"id": 2, "type": "event", "event": {"event_type": "state_changed", "data": {"entity_id": "binary_sensor.living_room_camera_online", "old_state": null, "new_state": {"entity_id": "binary_sensor.living_room_camera_online", "state": "on", "attributes": {"friendly_name": "Living room camera Online", "device_class": "connectivity"}, "last_changed": "2021-05-11T21:05:17.987521+00:00", "last_updated": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2021-05-11T21:05:17.987521+00:00", "context": {"id": "07c21e71b81a025b48198c95a9b0710e", "parent_id": null, "user_id": null}}}
2021-05-11 23:05:18 INFO (MainThread) [homeassistant.components.esphome] Successfully connected to 192.168.1.93
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] [Bedroom Display(192.168.1.71):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-17) [pychromecast.socket_client] Thread started...
2021-05-11 23:05:18 DEBUG (MainThread) [aioesphomeapi.connection] 192.168.1.93: Sending <class 'api_pb2.DeviceInfoRequest'>: 
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] connection listener: ffff977425e0 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.70', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-15) [homeassistant.components.cast.media_player] [media_player.office_speaker Office Speaker] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-15) [homeassistant.components.cast.media_player] [media_player.office_speaker Office Speaker] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> Query 12 worked. Exit code: <200>
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> HTTP query 13: http://192.168.1.61:80/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect
2021-05-11 23:05:18 DEBUG (SyncWorker_9) [amcrest.http] <AMC000EW_KT531F:AMC000EWK7SWKT531F> Running query 13 attempt 1
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] Thread started...
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] connection listener: ffff97685280 (CastStatusListener) ConnectionStatus(status='CONNECTED', address=NetworkAddress(address='192.168.1.76', port=8009))
2021-05-11 23:05:18 DEBUG (Thread-16) [homeassistant.components.cast.media_player] [media_player.workshop_speaker Workshop Speaker] Received cast device connection status: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-16) [homeassistant.components.cast.media_player] [media_player.workshop_speaker Workshop Speaker] Cast device availability changed: CONNECTED
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.controllers] Receiver:Updating status
2021-05-11 23:05:18 ERROR (Thread-16) [homeassistant] Error doing job: Task was destroyed but it is pending!
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Sending: Message urn:x-cast:com.google.cast.tp.connection from sender-0 to receiver-0: {'type': 'CONNECT', 'origin': {}, 'userAgent': 'PyChromecast', 'senderInfo': {'sdkType': 2, 'version': '15.605.1.3', 'browserVersion': '44.0.2403.30', 'platform': 4, 'systemVersion': 'Macintosh; Intel Mac OS X10_10_3', 'connectionType': 1}}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.socket_client] [Office Speaker(192.168.1.70):8009] Received: Message urn:x-cast:com.google.cast.receiver from receiver-0 to sender-0: {'requestId': 1, 'status': {'userEq': {'high_shelf': {'frequency': 4500.0, 'gain_db': 0.0, 'quality': 0.707}, 'low_shelf': {'frequency': 150.0, 'gain_db': 0.0, 'quality': 0.707}, 'max_peaking_eqs': 0, 'peaking_eqs': []}, 'volume': {'controlType': 'master', 'level': 0.44999998807907104, 'muted': False, 'stepInterval': 0.019999999552965164}}, 'type': 'RECEIVER_STATUS'}
2021-05-11 23:05:18 DEBUG (Thread-15) [pychromecast.controllers] Received status: CastStatus(is_active_input=None, is_stand_by=None, volume_level=0.44999998807907104, volume_muted=False, app_id=None, display_name=None, namespaces=[], session_id=None, transport_id=None, status_text='', icon_url=None, volume_control_type='master')
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Sending: Message urn:x-cast:com.google.cast.receiver from sender-0 to receiver-0: {'type': 'GET_STATUS', 'requestId': 1}
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] [Workshop Speaker(192.168.1.76):8009] Connected!
2021-05-11 23:05:18 DEBUG (Thread-16) [pychromecast.socket_client] Thread started...

I cannot see what it is that causes the issue. I have several Google Assistant devices. 7 to be exact

The ERROR is in an area of the log where HA inits connection to a Google Assistant box.

But is it related? I cannot see anything failing. It just spits out the single error in the middle of the log with nothing I can see that fails afterwards

EDIT. See discussion below. Issue seems related to the Harmony integration and not at all Google.

The problem comes from closing the loop immediately after cancelling the tasks. As the cancel() docs state

«This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop.»

Take this snippet of code:

import asyncio
import signal


async def pending_doom():
    await asyncio.sleep(2)
    print(">> Cancelling tasks now")
    for task in asyncio.Task.all_tasks():
        task.cancel()

    print(">> Done cancelling tasks")
    asyncio.get_event_loop().stop()


def ask_exit():
    for task in asyncio.Task.all_tasks():
        task.cancel()


async def looping_coro():
    print("Executing coroutine")
    while True:
        try:
            await asyncio.sleep(0.25)
        except asyncio.CancelledError:
            print("Got CancelledError")
            break

        print("Done waiting")

    print("Done executing coroutine")
    asyncio.get_event_loop().stop()


def main():
    asyncio.async(pending_doom())
    asyncio.async(looping_coro())

    loop = asyncio.get_event_loop()
    for sig in (signal.SIGINT, signal.SIGTERM):
        loop.add_signal_handler(sig, ask_exit)

    loop.run_forever()

    # I had to manually remove the handlers to
    # avoid an exception on BaseEventLoop.__del__
    for sig in (signal.SIGINT, signal.SIGTERM):
        loop.remove_signal_handler(sig)


if __name__ == '__main__':
    main()

Notice ask_exit cancels the tasks but does not stop the loop, on the next cycle looping_coro() stops it. The output if you cancel it is:

Executing coroutine
Done waiting
Done waiting
Done waiting
Done waiting
^CGot CancelledError
Done executing coroutine

Notice how pending_doom cancels and stops the loop immediately after. If you let it run until the pending_doom coroutines awakes from the sleep you can see the same warning you’re getting:

Executing coroutine
Done waiting
Done waiting
Done waiting
Done waiting
Done waiting
Done waiting
Done waiting
>> Cancelling tasks now
>> Done cancelling tasks
Task was destroyed but it is pending!
task: <Task pending coro=<looping_coro() running at canceling_coroutines.py:24> wait_for=<Future cancelled>>

The meaning of the issue is that a loop doesn’t have time to finish all the tasks.

This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop.

There is no chance to do a «next cycle» of the loop in your approach. To make it properly you should move a stop operation to a separate non-cyclic coroutine to give your loop a chance to finish.

Second significant thing is CancelledError raising.

Unlike Future.cancel(), this does not guarantee that the task will be cancelled: the exception might be caught and acted upon, delaying cancellation of the task or preventing cancellation completely. The task may also return a value or raise a different exception.

Immediately after this method is called, cancelled() will not return True (unless the task was already cancelled). A task will be marked as cancelled when the wrapped coroutine terminates with a CancelledError exception (even if cancel() was not called).

So after cleanup your coroutine must raise CancelledError to be marked as cancelled.

Using an extra coroutine to stop the loop is not an issue because it is not cyclic and be done immediately after execution.

def main():                                              
    loop = asyncio.get_event_loop()                      
    asyncio.ensure_future(listen_to_ipc_channel_layer()) 
                                                     
    for sig in (signal.SIGINT, signal.SIGTERM):          
        loop.add_signal_handler(sig, ask_exit)           
    loop.run_forever()                                   
    print("Close")                                       
    loop.close()                                         
                                                     
                                                     
@asyncio.coroutine                                       
def listen_to_ipc_channel_layer():                       
    while True:                                          
        try:                                             
            print("Running")                                 
            yield from asyncio.sleep(0.1)                
        except asyncio.CancelledError as e:              
            print("Break it out")                        
            raise e # Raise a proper error
                                                     
                                          
# Stop the loop concurrently           
@asyncio.coroutine                                       
def exit():                                              
    loop = asyncio.get_event_loop()                      
    print("Stop")                                        
    loop.stop()                                          


def ask_exit():                          
    for task in asyncio.Task.all_tasks():
        task.cancel()                    
    asyncio.ensure_future(exit())        
                                     
                                     
if __name__ == "__main__":               
    main()                               

The reasons this happens is as explained by @Yeray Diaz Diaz
In my case, I wanted to cancel all the tasks that were not done after the first finished, so I ended up cancelling the extra jobs, then using loop._run_once() to run the loop a bit more and allow them to stop:

    loop = asyncio.get_event_loop()
    job = asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
    tasks_finished,tasks_pending, = loop.run_until_complete(job)
    tasks_done = [t for t in tasks_finished if t.exception() is None]
    if tasks_done == 0:
        raise Exception("Failed for all tasks.")
    assert len(tasks_done) == 1
    data = tasks_done[0].result()
    for t in tasks_pending:
        t.cancel()
        t.cancel()
    while not all([t.done() for t in tasks_pending]):
        loop._run_once()

Понравилась статья? Поделить с друзьями:
  • Error document is not defined no undef
  • Error document follows
  • Error docker exporter does not currently support exporting manifest lists
  • Error do not access object prototype method hasownproperty from target object no prototype builtins
  • Error dmg image is corrupted