Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure we run open/close in executor #82

Merged
merged 1 commit into from
Apr 19, 2022
Merged

Make sure we run open/close in executor #82

merged 1 commit into from
Apr 19, 2022

Conversation

elupus
Copy link
Contributor

@elupus elupus commented Apr 1, 2022

Run open/close calls on pyserial in executor since these calls can execute time.sleep() and other blocking operations. These break larger pure async applications like HomeAssistant since they stall the main event loop.

Fixed: #81
Related to: home-assistant/core#67430
Related to: home-assistant/core#67419

@rob-smallshire rob-smallshire merged commit 6e1baf2 into pyserial:master Apr 19, 2022
@marcopi
Copy link

marcopi commented May 9, 2022

After applying this patch to pyserial-asyncio, my HA app gives 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'

I tried using another syntax:

    callback = partial(serial.serial_for_url, *args, **kwargs)
    serial_instance = await loop.run_in_executor(None, callback)

but this solve only one part of the problem but the issue in HA (home-assistant/core#67419) still persist somewhere else.

@elupus
Copy link
Contributor Author

elupus commented May 9, 2022

@marcopi can you provide a pull request for your change? Seems run_in_executor fail for keyword arguments. i did not know that.

Also the issue you say persist. You are sue it's using the changed version? Do you have a a new stack trace for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Blocking call scheduled on loop when closing connection
3 participants