OpenCyphal / pycyphal

Python implementation of the Cyphal protocol stack.
https://pycyphal.readthedocs.io/
MIT License
117 stars 106 forks source link

Background workers should terminate if the event loop is closed #224

Open pavel-kirienko opened 2 years ago

pavel-kirienko commented 2 years ago

Resource mismanagement may result in the following messages logged endlessly:

ERROR    pycyphal.transport.udp._socket_reader:_socket_reader.py:257 SocketReader(id=0x23dddada8f0, original_fd=2276, socket=<socket.socket fd=2276, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=17, laddr=('0.0.0.0', 16383)>, remote_node_ids=[None]): Worker thread error: Event loop is closed; will continue after a short nap
Traceback (most recent call last):
  File "C:\projects\yakut\.nox\test-3-10\lib\site-packages\pycyphal\transport\udp\_socket_reader.py", line 249, in _thread_entry_point
    loop.call_soon_threadsafe(self._dispatch_frame, ts, source_ip, frame)
  File "C:\Python310-x64\lib\asyncio\base_events.py", line 795, in call_soon_threadsafe
    self._check_closed()
  File "C:\Python310-x64\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Source:

https://github.com/OpenCyphal/pycyphal/blob/4e09c9067208df3e00cd89a4465e7b6b6753b19a/pycyphal/transport/udp/_socket_reader.py#L256-L258

Other locations can be found in the CAN media drivers etc.

Perhaps there should be a counter to terminate the task after X successive errors of the same type.

pavel-kirienko commented 1 year ago

Actually, a RuntimeError should just bring down the worker unconditionally.