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

Code loosing connection #171

Closed
Schmeling-M opened this issue Apr 17, 2021 · 16 comments
Closed

Code loosing connection #171

Schmeling-M opened this issue Apr 17, 2021 · 16 comments

Comments

@Schmeling-M
Copy link

When I do this in my code:
if Client.status()['state'] == "play":

I get this:
Apr 14 08:34:14 moode systemd[1]: Stopped Music Player Daemon.
Apr 14 08:34:14 moode systemd[1]: Started Music Player Daemon.
Apr 14 08:34:14 moode rc.local[481]: Traceback (most recent call last):
Apr 14 08:34:14 moode rc.local[481]: File "/home/pi/ampcontroller.py", line 196, in
Apr 14 08:34:14 moode rc.local[481]: if Client.status()['state'] == "play": # Playing or Airplay active
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 469, in mpd_command
Apr 14 08:34:14 moode rc.local[481]: return wrapper(self, name, args, callback)
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 532, in _execute
Apr 14 08:34:14 moode rc.local[481]: return retval()
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 454, in command_callback
Apr 14 08:34:14 moode rc.local[481]: res = function(self, self._read_lines())
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 394, in _parse_object
Apr 14 08:34:14 moode rc.local[481]: objs = list(self._parse_objects(lines))
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 240, in _parse_objects
Apr 14 08:34:14 moode rc.local[481]: for key, value in self._parse_pairs(lines):
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 235, in _parse_pairs
Apr 14 08:34:14 moode rc.local[481]: for line in lines:
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 586, in _read_lines
Apr 14 08:34:14 moode rc.local[481]: line = self._read_line()
Apr 14 08:34:14 moode rc.local[481]: File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 571, in _read_line
Apr 14 08:34:14 moode rc.local[481]: raise ConnectionError("Connection lost while reading line")
Apr 14 08:34:14 moode rc.local[481]: mpd.base.ConnectionError: Connection lost while reading line
Apr 14 08:34:14 moode systemd[1]: rc-local.service: Control process exited, code=exited, status=1/FAILURE
Apr 14 08:34:14 moode systemd[1]: Created slice system-bluealsa\x2daplay.slice.
Apr 14 08:34:14 moode systemd[1]: Started BlueAlsa-Aplay.
Apr 14 08:34:14 moode systemd[1]: rc-local.service: Failed with result 'exit-code'.
Apr 14 08:34:14 moode systemd[1]: Failed to start /etc/rc.local Compatibility.

In the beginning of my code, I connect to mpd like so:
Client = mpd.MPDClient() # create client object
Client.timeout = None
Client.idletimeout = None

while True:
try:
Client.connect("localhost", 6600) # connect to localhost:6600
except:
continue
break

It seems like I get a connection error, but how do I solve this, and why am I loosing the connection???

@Mic92
Copy link
Owner

Mic92 commented Apr 17, 2021

Mpd is closing connections after a some timeout if your client is not doing anything. see #90

I cannot see the whole program so it is hard to tell what your pattern in the library is.

@Schmeling-M
Copy link
Author

Schmeling-M commented Apr 17, 2021

I'm starting the code from etc/rc.local, so it starts immediately, and the error likewise, it seems. I have set the timeouts to none, So I wouldn't expect the connection to be closed that fast, but I might be wrong. The entire code is below:

(The formatting is really annoying me, and I cant fix it, I have atatched the file as well, renamed .txt)
ampcontroller.txt

@Mic92
Copy link
Owner

Mic92 commented Apr 18, 2021

Sorry this is not readable without proper indentation. If you could upload a repo or gist, I might have a look...

@Schmeling-M
Copy link
Author

Schmeling-M commented Apr 18, 2021

Yes, terribly sorry about that. I uploaded a gist instead. It seems to be working. Please let me know if it doesn't.

https://gist.github.com/Schmeling-M/3be163d0bf6fb3ac520df6dd9dc8ed54

One thing, that especially pussels me is that the raised exception in base.py, line 571 is raised because of this condition in line 569: if not line.endswith("\n"): Is this because the lack of "\n" means, that the connection to mpd is lost or.....?

I should also mention, that the code is running on a Rpi4B with moode from moodeaudio.org. When the code is running, moode audio spits out the following error 3-5 times at startup: "MPD error JSON.parse: unexpected end of data at line 1 column 1 of the JSON data". I seems it could have a relation to the exception raised in base.py

@Mic92
Copy link
Owner

Mic92 commented Apr 18, 2021

If read returns an empty string, this is detected as connection lost.

I cannot see any obvious bug right now, but maybe check if the following code works for your:

import logging, mpd
logging.basicConfig(level=logging.DEBUG)
client = mpd.MPDClient()
client.connect("localhost", 6600)
print(client.status())

@Schmeling-M
Copy link
Author

I take it, your suggestion doesn't solve the problem. Only debugs it further, correct?

@Mic92
Copy link
Owner

Mic92 commented Apr 18, 2021

Is mpd actually listening on this port?

You can run tcpdump to see what is going on the wire:

$ sudo tcpdump -i any localhost port 6600 -n -A

And than run in another terminal, the same script.

@Mic92
Copy link
Owner

Mic92 commented Apr 18, 2021

Also I would not run this code anywhere where untrusted clients have access to:
It is full of shell injection bugs: https://github.com/moode-player/moode/blob/653bbb46f44851050e74a5c86693603ad9c14ea7/www/command/moode.php#L272

@Schmeling-M
Copy link
Author

Thankyou for all the suggestions and feedback. I'll test the connection in the following days, and see, if that might be the problem.

Fortunately the player is on a private lan. No untrusted clients have access.

@Schmeling-M
Copy link
Author

It seems, I have a connection to mpd. At least I get the correct info, when running your python code:
this
import logging, mpd logging.basicConfig(level=logging.DEBUG) client = mpd.MPDClient() client.connect("localhost", 6600) print(client.status())

gives this
root@moode:/home/pi# python3 mpd_test.py INFO:mpd.base:Calling MPD connect('localhost', 6600, timeout=None) DEBUG:mpd.base:Calling MPD status() {'volume': '0', 'repeat': '0', 'random': '0', 'single': '0', 'consume': '0', 'partition': 'default', 'playlist': '2', 'playlistlength': '3', 'mixrampdb': '0.000000', 'state': 'play', 'song': '1', 'songid': '2', 'time': '979:1550', 'elapsed': '979.219', 'bitrate': '192', 'duration': '1550.294', 'audio': '44100:24:2', 'nextsong': '2', 'nextsongid': '3'}

So I'm guessing, the connection works initially, but with timeout set to none, I cant see why the connection i suddenly lost??

@Schmeling-M
Copy link
Author

my mpd config /etc/mpd.conf looks like so:
`root@moode:/etc# cat mpd.conf
#########################################
This file is automatically generated
by the MPD configuration page.
#########################################

music_directory "/var/lib/mpd/music"
playlist_directory "/var/lib/mpd/playlists"
db_file "/var/lib/mpd/database"
log_file "/var/log/mpd/log"
pid_file "/var/run/mpd/pid"
state_file "/var/lib/mpd/state"
sticker_file "/var/lib/mpd/sticker.sql"
user "mpd"
group "audio"
bind_to_address "any"
port "6600"
log_level "default"
restore_paused "yes"
auto_update "no"
follow_outside_symlinks "yes"
follow_inside_symlinks "yes"
zeroconf_enabled "no"
zeroconf_name "Moode MPD"
filesystem_charset "UTF-8"
metadata_to_use "+comment"
replaygain "off"
replaygain_preamp "0"
volume_normalization "no"
audio_buffer_size "4096"
max_output_buffer_size "131072"
max_playlist_length "16384"
max_connections "128"

decoder {
plugin "ffmpeg"
enabled "yes"
}

input {
plugin "curl"
}

resampler {
plugin "soxr"
quality "high"
threads "1"
}

audio_output {
type "alsa"
name "ALSA Default"
device "hw:0,0"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA Crossfeed"
device "crossfeed"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA Parametric EQ"
device "eqfa12p"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA Graphic EQ"
device "alsaequal"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA Polarity Inv"
device "invpolarity"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA CamillaDSP"
device "camilladsp"
mixer_type "software"
dop "no"
}

audio_output {
type "alsa"
name "ALSA Bluetooth"
device "btstream"
mixer_type "software"
}

audio_output {
type "httpd"
name "HTTP Server"
port "8000"
encoder "lame"
bitrate "320"
tags "yes"
always_on "yes"
}

root@moode:/etc#
`

@Mic92
Copy link
Owner

Mic92 commented Apr 21, 2021

timeout is already None by default... you don't even need to set it.

@Schmeling-M
Copy link
Author

Perhaps, I should try to run you python code in a loop, like my code, and see if that will reproduce the error?

@Schmeling-M
Copy link
Author

I think I found the problem. If I run this code:

https://gist.github.com/Schmeling-M/ae67c0b82f804dfcf65bc3f8a4d0081e

It runs perfectly, until I connect to moode via bluetooth, in which case, it spits out exactly the same error as before

DEBUG:mpd.base:Calling MPD status()
DEBUG:mpd.base:Calling MPD status()
{'volume': '0', 'repeat': '0', 'random': '0', 'single': '0', 'consume': '0', 'partition': 'default', 'playlist': '2', 'playlistlength': '7', 'mixrampdb': '0.000000', 'state': 'play', 'song': '6', 'songid': '7', 'time': '335:335', 'elapsed': '334.685', 'bitrate': '32', 'duration': '334.889', 'audio': '44100:24:2'}
DEBUG:mpd.base:Calling MPD status()
DEBUG:mpd.base:Calling MPD status()
{'volume': '0', 'repeat': '0', 'random': '0', 'single': '0', 'consume': '0', 'partition': 'default', 'playlist': '2', 'playlistlength': '7', 'mixrampdb': '0.000000', 'state': 'play', 'song': '6', 'songid': '7', 'time': '335:335', 'elapsed': '334.788', 'bitrate': '32', 'duration': '334.889', 'audio': '44100:24:2'}
DEBUG:mpd.base:Calling MPD status()
DEBUG:mpd.base:Calling MPD status()
INFO:mpd.base:Calling MPD disconnect()
Traceback (most recent call last):
File "mpd_test2.py", line 15, in
print(client.status())
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 469, in mpd_command
return wrapper(self, name, args, callback)
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 532, in _execute
return retval()
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 454, in command_callback
res = function(self, self._read_lines())
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 394, in _parse_object
objs = list(self._parse_objects(lines))
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 240, in _parse_objects
for key, value in self._parse_pairs(lines):
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 235, in _parse_pairs
for line in lines:
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 586, in _read_lines
line = self._read_line()
File "/usr/local/lib/python3.7/dist-packages/mpd/base.py", line 571, in _read_line
raise ConnectionError("Connection lost while reading line")
mpd.base.ConnectionError: Connection lost while reading line
root@moode:/home/pi#

So it seems that the error is natural, but why it occurs at startup, when I run the script from etc/rc.local I do not know.It could perhaps be because bluetooth forces an mpd disconnect, but I'm not sure.

Do you have any idea, as to why this might be happening?

@Mic92
Copy link
Owner

Mic92 commented Apr 22, 2021

not really no.

@Mic92
Copy link
Owner

Mic92 commented Apr 22, 2021

I don't really see any actionable insights on what is broken here in the library. This seems more a setup issue.

@Mic92 Mic92 closed this as completed Apr 22, 2021
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

No branches or pull requests

2 participants