[MPD] Fixed loosing pending commands on broken idle connection #16299
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm using MPD on an old tablet. I wrote a rule to play music to alert when windows are open to long. It works in general.
Unfortunately, when MPD is idle for a long time, the connection get lost. Then (when my window rule send the play command) the mpd binding automatically reconnects after 60s, but the pending commands are lost. Therefore my alert is not working most of the time.
Expected Behavior
The mpd binding should handle reconnect on idle connections without loosing commands. (This is similar to JDBC connection pools like HikariCP handle broken idle JDBC connections.)
Current Behavior
The mpd binding looses pending commands when reconnecting after detecting a broken connection.
Here is a trace log of such a case (formatted with extra empty lines):
The
received line 'null'
shows the broken connection.I looked into the code and can explain the behavior:
Is this on purpose, especially for broken idle connections?
Possible Solution
I think broken idle connections should be handled by a reconnect right after instead of sleeping for 60s and clearing the pending commands.
This pull request does this. It introduces a new variable wakingUpFromIdle which keeps track of whether the last command sent was a "noidle" which tries to wakeup the idle mode. If the connection reset occurs right after that, the connection is rebuilt right away.
Steps to Reproduce (for Bugs)
I can reproduce the bug with my android tablet, if I wait long enough, but I cannot reproduce the error otherwise. If I just kill the MPD instance, the connection ist terminated properly and the MPD binding detects the closed connection. It probalby is possible using a firewall in between which silently terminates the connection, but I didn't try this.
Context
See first paragraph
Your Environment
I'm using openhab 4.1.0 as a docker container openhab/openhab:4.1.0. on debian bookworm. Besides the 4.1.0 binding I also used the own built MPD binding from the trunk.