Fix modbus blocking threads#50619
Fix modbus blocking threads#50619frenck merged 7 commits intohome-assistant:devfrom janiversen:modbus_sync_2
Conversation
|
Hey there @adamchengtkc, @vzahradnik, mind taking a look at this pull request as its been labeled with an integration ( |
|
I really like the expression GitHub uses "degraded performance for GitHub Actions." see: In future PR´s I will use "degraded performance" instead of problems or bugs. In other words, I will try again later. |
Make single sync function to do pymodbus calls. Add async version of pymodbus calls. Convert platform to async. Update/simplify test_delay. Change Lock() from threading. to asyncio. Add thread block test.
|
We also may want to set the https://developers.home-assistant.io/docs/integration_fetching_data/#request-parallelism If a user would set up many different hubs with many entities for a platform, trying to run service calls in parallel would consume many thread pool threads. |
|
That's fix my issue #50610 |
|
Tested this PR, works as expected for both Serial Modbus RTU and TCP Modbus over IP. Maybe a Placebo but it feels like the GUI is more responsive, awesome work @janiversen adding asynchrony to this component. |
|
Added PARALLEL_UPDATES = 1, that should be the last of the requested changes. |
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Breaking change
Proposed change
Since long before 2021.3 (as many think is working perfectly) the modbus integration have had a problem that caused threads to be blocked when a modbus device had problems. Pymodbus 2.30 was very "relaxed" and ignored quite a lot, so the problem was mostly ignored. I heard about it a couple of times, which caused a try to run pymodbus async. last year, with little success due to big flaws in pymodbus 2.30.
Pymodbus 2.5.1 (introduced in 2021.5) detected a lot more, and thus HA experienced that a lot more threads got blocked. A big thanks to @MartinHjelmare for pointing at the threading Lock() as a potential problem.
This PR changes the whole modbus integration to be async, however pymodbus is still used synchronously and thus there a switch from async to sync.
There is added a new test "test_thread_lock", that proves the problem:
This PR do not stand alone, if there are modbus communication problems they should be identified and solved,
to make that possible 2 new configuration variables are available:
"delay" is used solely to postpone sending requests after connect.
"timeout" asks pymodbus to wait for a response, if a response comes earlier waiting is stopped
"close_comm_on_error" ask pymodbus to keep connections open even in case of errors.
Type of change
Additional information
Please add this PR (after review) to 2021.6
This PR fixes or closes issue: fixes #
fixes Something is blocking Home Assistant from wrapping up the start up phase #48894
fixes Something is blocking Home Assistant from wrapping up the start up phase #49159
fixes Modbus error with 2021.5.0 #50139
fixes Something is blocking Home Assistant from wrapping up the start up #50155
fixes Modbus sends first request too soon for slow devices. #50011
This PR is related to issue:
Link to documentation pull request:
There are no new documentation.
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: