Repository navigation
BrokenPipeError (?) when idle for a long time #64
Copy link
Copy link
Closed
Description
Hi and thank you for this project!
When mpd is idle (after calling client.stop()) for a couple of hours, I cannot successfully call client.play() again. I am aware of ConnectionErrors that can occur so I catch them in my script. In this case, however, I get a BrokenPipeError (sometimes I don't get an error at all, but nevertheless mpd won't start to play reproducibly).
I wrote a test script (find it here: https://gist.github.com/speendo/0d9c1a028d045de3f7a6) to track the error down.
I ran this script on my raspberry pi with nohup python3 test_mpd.py &.
Here's a traceback of the Error (found in nohup.out):
Traceback (most recent call last):
File "test_mpd.py", line 62, in <module>
client.play()
File "/usr/local/lib/python3.4/dist-packages/mpd.py", line 600, in decorator
return wrapper(self, name, args, bound_decorator(self, returnValue))
File "/usr/local/lib/python3.4/dist-packages/mpd.py", line 236, in _execute
self._write_command(command, args)
File "/usr/local/lib/python3.4/dist-packages/mpd.py", line 263, in _write_command
self._write_line(" ".join(parts))
File "/usr/local/lib/python3.4/dist-packages/mpd.py", line 243, in _write_line
self._wfile.flush()
File "/usr/lib/python3.4/socket.py", line 391, in write
return self._sock.send(b)
BrokenPipeError: [Errno 32] Broken pipe
And here's the log:
2015-12-15 08:35:26,497 INFO: Log started
2015-12-15 08:35:26,500 INFO: Attempt to (re-)connect to mpd server
2015-12-15 08:35:26,503 INFO: Calling MPD connect('localhost', 6600, timeout=None)
2015-12-15 08:35:26,566 INFO: Finished attempt to (re-)connect to mpd server
2015-12-15 08:35:26,568 INFO: <bound method MPDClient.decorator of <mpd.MPDClient object at 0xb6a38590>>
2015-12-15 08:35:26,570 INFO: Add playlist entry
2015-12-15 08:35:26,573 DEBUG: Calling MPD clear()
2015-12-15 08:35:26,578 DEBUG: Calling MPD add('http://uwstream2.somafm.com:9016',)
2015-12-15 08:35:26,582 INFO: Start playing for the first time
2015-12-15 08:35:26,585 DEBUG: Calling MPD play()
2015-12-15 08:35:36,597 INFO: This obviously works. Switching off now for 4 hours.
2015-12-15 08:35:36,604 DEBUG: Calling MPD stop()
2015-12-15 12:35:36,725 INFO: Trying to switch on again now
2015-12-15 12:35:36,727 DEBUG: Calling MPD play()
Can you help me find the source of this problem (or even solve it)?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
Mic92 commentedon Dec 16, 2015
Duplicate of #31
speendo commentedon Dec 16, 2015
Hello Jörg!
I'm aware of #31 and ConnectionErrors.
Prove me wrong, but I don't see how this issue is related to that.
My script should catch all ConnectionErrors with
Please find the whole script here: https://gist.github.com/speendo/0d9c1a028d045de3f7a6
After a couple of hours of idle time I get a (generic) BrokenPipeError which - as far as I know - is not related to the mpd-specific ConnectionError.
I would be glad if you could clarify why this is a duplicate or reopen the issue.
Thank you!
Mic92 commentedon Dec 16, 2015
ok. take a look at it.
speendo commentedon Dec 16, 2015
Thank you!
Mic92 commentedon Dec 16, 2015
The problem here is, that mpd does not mask any exception thrown at all, which are generated by the layers below. Sorry this was not my invention. The alternative is exception chaining (catching the error and rethrow an MPDError inheriting the original one). Python 2 does not support this. However it is possible to a custom reference in the exeption to its root cause.
speendo commentedon Dec 16, 2015
Okay, thank you. Took me some time but I think I understand the problem now.
I use Python3 anyway but I understand that this is not a solution you would want to offer only for Python3 users...
I would like to avoid catching all Exceptions and do something like
as this is considered as bad style... Is there a way to be more specific - maybe using the custom reference?
speendo commentedon Dec 16, 2015
Wouldn't this be a possible solution?
http://stackoverflow.com/questions/1350671/inner-exception-with-traceback-in-python
[edit]
more to find here:
http://www.ianbicking.org/blog/2007/09/re-raising-exceptions.html
speendo commentedon Dec 24, 2015
Just for testing purposes, I tried to catch every exception and reconnect to the server, if I catch one. Like this:
(find a full example here: https://gist.github.com/speendo/91de6511443e75f18fed)
This also didn't work because the client still thinks it is connected. I get this error:
As far as I understand this, this means I would have to close the (broken) connection first before I could reestablish the client's connection, right?
This would be a bit akward. I am only a hobby programmer and not an expert, but from my point of view I think that python-mpd2 should catch the BrokenPipeError and do something about it - either set the connection closed or reestablish the connection.
If the connection is not reestablished for me it would feel more natural if my script would fire a MPD.ConnectionError instead of a "generic" BrokenPipeError.
However, do you know of a way to get the BrokenPipeError more quickly? Because every test I make takes me 4 hours which is quite a lot for someone who can only code in his free time ;-)
By the way:
Schöne Weihnachten!
dinofizz commentedon Feb 25, 2017
To repro the BrokenPipeError, assuming you have a mpd server running on localhost on port 6600:
As stated above, one cannot call a client.connect(...) to remedy the situation as the client._sock object exists, and mpd thus believes it is still connected.
To handle this case I am catching BrokenPipeError and then calling a client._reset(), which sets the _sock object to None. From here I can attempt to reconnect using client.connect(...)
TomTom0815 commentedon Mar 14, 2017
Hi.
I'm running into this issue as well. After some hours, the connection get dropped and get a socket errors all over the place. It only happens after some hours.
Sad story: I build a device to play music for my little daughter (4 years) - it worked great for many months until I switched to python-mpd2. She goes to bed and listens to music and as soon she wakes up she like to hear her favorite audio book. So, now she wakes me up at 5am and I need to reboot the device. :(
I reworked the connection/re-connection and expectation handling many times (takes one day turn-around for each test) but always ended up with a new exception or fail.
I think the merge request #65 from Speendo solves the issue. Protect the write() and flush() to the sockets, catch socket.error and do a reset() on the socket will solve the issues with the re-connect when I discover that the mpd command didn’t succeeded. Right now, the re-connect fails with “Already connected” or more socket errors.
I’m going to test this merge request locally in the next day. But could you please revive the discussions around this merge request and drive that forward. I would really appreciate a fix for this.
speendo commentedon Mar 14, 2017
Hello TomTom0815
I use this library for my Project FlushFM - a webradio on my toilet that starts playing every time the light goes on.
It's working fine for a more than a year now, so chances are that my pull request will work for you too.
I wish you (and your daughter) the best :)
quantenschaum commentedon Sep 23, 2017
I had the same problem and solved/worked around it by calling
_reset()on BrokenPipeError and then reconnect.I think
disconnect()should be modified to be able to deal with dead, already closed sockets correctly.quantenschaum commentedon Sep 23, 2017
Maybe something like:
So it will still raise its exception, but afterwards the mpd instance is able to reconnect.
quantenschaum commentedon Sep 24, 2017
I find it quite astonishing, that the library does not handle the
BrokenPipeErrorcorrectly, because it is something very common, when dealing with sockets. The defaultconnection_timeoutof mpd is 60 seconds. This means, after 60s mpd closes the connection, when no data was sent. No need to wait for 4 hours. It's 100% reproducible. Connect, wait more than 60s, and send something. You get aBrokenPipeErrorand an unusableMPDClient.The problem is, that
disconnecttries to close_wfile. This forces aflushof its buffers, which fails, because the socket was closed on the other end. The raised exception prevents_sock.close()and_resetto be called and leaves a borkedMPDClient.I worked around this deficiency by wrapping
MPDClientlike thisdisconnectnever raise aConnectionErrorand leavesMPDClientin a clean state.connectto another server without the need todisconnectexplicitly.volumespeendo commentedon Sep 24, 2017
Thank you for looking at it!
Did you also see my merge request? At least for me it work very robust and it tries to keep the logic of the original library.
quantenschaum commentedon Sep 24, 2017
Hi @speendo, I saw it. It might work for your case, but it does not fix the broken
disconnectmethod and the python version handling seems overly complicated.Let's assume the connection is broken and
disconnectis called. Then we end up with a borkedMPDClientas before.In my opinion,
disconnecthas to be changed as I proposed (or in a similar way). Then a broken connection is indicated by an exception and one can recover from this state bydisconnecting andconnecting again. Clean, straight forward and it works in either case. One may discuss that a unification ofmpd.ConnectionErrorandConnectionErrormakes sense, so that there is only one type of exception to be caught, which indicates a problem with the underlying connection.quantenschaum commentedon Oct 20, 2017
Nothing?
speendo commentedon Oct 20, 2017
Well, @quantenschaum, as far as I get it, I think you simply follow a different approach. While my solution keeps the logic of the original library (not questioning if it makes sense or not), you solve the problem by changing its behavior.
When using (my version of) python-mpd2, I do also use a wrapper, because I want my mpd client to stay connected. But this wrapper is implemented in my project not in the library.
Nevertheless, your approach also solves the issue and for that I find it much better than the current state of this library.
... however, I guess you wanted @Mic92 to answer your comment, not me...
quantenschaum commentedon Oct 21, 2017
Yes, of course, the current behavior is leaving a broken MPDClient when the remote end closes the socket. This is a bug and needs to be fixed = changed. One might make the auto reconnect feature optional, but fixing the broken disconnect method is essential.
Mic92 commentedon Feb 20, 2018
merged in #92