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

Executor fix: use a callback in run_in_executor

Open marcopi opened this issue 3 years ago • 3 comments

As stated in: https://github.com/pyserial/pyserial-asyncio/pull/82 last commit threw error with the following stack trace:

Logger: homeassistant.config_entries
Source: components/zha/core/gateway.py:182
First occurred: 16:12:26 (1 occurrences)
Last logged: 16:12:26

Error setting up entry socket://[192.168.1.12:2001](http://192.168.1.12:2001/) for zha
Traceback (most recent call last):
 File "/usr/src/homeassistant/homeassistant/config_entries.py", line 335, in async_setup
   result = await component.async_setup_entry(hass, self)
 File "/usr/src/homeassistant/homeassistant/components/zha/__init__.py", line 102, in async_setup_entry
   await zha_gateway.async_initialize()
 File "/usr/src/homeassistant/homeassistant/components/zha/core/gateway.py", line 182, in async_initialize
   self.application_controller = await app_controller_cls.new(
 File "/usr/local/lib/python3.9/site-packages/zigpy/application.py", line 69, in new
   await app.startup(auto_form)
 File "/usr/local/lib/python3.9/site-packages/bellows/zigbee/application.py", line 132, in startup
   self._ezsp = await bellows.ezsp.EZSP.initialize(self.config)
 File "/usr/local/lib/python3.9/site-packages/bellows/ezsp/__init__.py", line 82, in initialize
   await ezsp.connect()
 File "/usr/local/lib/python3.9/site-packages/bellows/ezsp/__init__.py", line 92, in connect
   self._gw = await bellows.uart.connect(self._config, self)
 File "/usr/local/lib/python3.9/site-packages/bellows/uart.py", line 363, in connect
   protocol, connection_done = await thread.run_coroutine_threadsafe(
 File "/usr/local/lib/python3.9/site-packages/bellows/uart.py", line 340, in _connect
   transport, protocol = await serial_asyncio.create_serial_connection(
 File "/usr/local/lib/python3.9/site-packages/serial_asyncio/__init__.py", line 449, in create_serial_connection
   serial_instance = await loop.run_in_executor(None, serial.serial_for_url, *args, **kwargs)
TypeError: run_in_executor() got an unexpected keyword argument 'url'

The fix will use the correct syntax and hopefully solve the problem in my Home Assistant setup (sometimes HA looses connection with ZHA on eszp protocol via socket).

marcopi avatar May 22 '22 20:05 marcopi

Could we get a release of pyserial-asyncio incorporating this change and #82?

puddly avatar Aug 04 '22 06:08 puddly

@puddly Yes, when I'm back from vacation.

rob-smallshire avatar Aug 05 '22 09:08 rob-smallshire

On second thought, it looks like running these sync functions in a thread pool executor has some issues when closing the connection and immediately shutting down the loop. I'll have to investigate this.

puddly avatar Aug 07 '22 17:08 puddly

An equivalent fix was merged as part of #88.

rob-smallshire avatar Oct 30 '22 08:10 rob-smallshire