pyserial-asyncio icon indicating copy to clipboard operation
pyserial-asyncio copied to clipboard

Run cleanup functions synchronously if the event loop is stopping

Open puddly opened this issue 3 years ago • 0 comments

Functions called on connection loss can fail to run in the executor if the event loop is shutting down at the same time:

2022-08-24 19:07:05.408 MacBook.local asyncio ERROR Task exception was never retrieved
future: <Task finished name='Task-9' coro=<SerialTransport._call_connection_lost() done, defined at ~/Projects/pyserial-asyncio/serial_asyncio/__init__.py:398> exception=RuntimeError('cannot schedule new futures after interpreter shutdown')>
Traceback (most recent call last):
  File "~/Projects/pyserial-asyncio/serial_asyncio/__init__.py", line 408, in _call_connection_lost
    await self._loop.run_in_executor(None, self._serial.flush)
  File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 819, in run_in_executor
    executor.submit(func, *args), loop=self)
  File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 169, in submit
    raise RuntimeError('cannot schedule new futures after '
RuntimeError: cannot schedule new futures after interpreter shutdown

I think it's better to call them synchronously and risk potentially stalling the nearly-stopped event loop than to not call them at all.

puddly avatar Aug 24 '22 23:08 puddly