[Pkg-mpd-commits] [python-mpd] 146/262: correct cleanup, if socket connection die

Simon McVittie smcv at debian.org
Sun May 22 18:16:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch upstream
in repository python-mpd.

commit c10b65964928c487e00bb1e536177b4d41e71bb2
Author: Jörg Thalheim <joerg at higgsboson.tk>
Date:   Tue Oct 23 07:42:20 2012 +0200

    correct cleanup, if socket connection die
    
    When the connection was closed by server, self._sock wasn't reset to None.
    So a connect attempt afterwards fails, because MPDClient thought it is still connected.
    This fixes #11.
---
 mpd.py  | 11 ++++++++---
 test.py | 13 +++++++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/mpd.py b/mpd.py
index de73609..b61c385 100644
--- a/mpd.py
+++ b/mpd.py
@@ -230,6 +230,7 @@ class MPDClient(object):
         if self.use_unicode:
             line = decode_str(line)
         if not line.endswith("\n"):
+            self.disconnect()
             raise ConnectionError("Connection lost while reading line")
         line = line.rstrip("\n")
         if line.startswith(ERROR_PREFIX):
@@ -365,6 +366,7 @@ class MPDClient(object):
     def _hello(self):
         line = self._rfile.readline()
         if not line.endswith("\n"):
+            self.disconnect()
             raise ConnectionError("Connection lost while reading MPD hello")
         line = line.rstrip("\n")
         if not line.startswith(HELLO_PREFIX):
@@ -431,9 +433,12 @@ class MPDClient(object):
             raise
 
     def disconnect(self):
-        self._rfile.close()
-        self._wfile.close()
-        self._sock.close()
+        if not self._rfile is None:
+            self._rfile.close()
+        if not self._wfile is None:
+            self._wfile.close()
+        if not self._sock is None:
+            self._sock.close()
         self._reset()
 
     def fileno(self):
diff --git a/test.py b/test.py
index c1f0866..06ca0ff 100755
--- a/test.py
+++ b/test.py
@@ -207,5 +207,18 @@ class TestMPDClient(unittest.TestCase):
         self.client.connect(TEST_MPD_HOST, TEST_MPD_PORT, timeout=5)
         self.assertEqual(self.client._sock.gettimeout(), 5)
 
+    def test_connection_lost(self):
+        client = mpd.MPDClient()
+        client.connect(TEST_MPD_HOST, TEST_MPD_PORT)
+        # simulate a disconnect
+        client._sock.send("close\n")
+        client._sock.recv(4096)
+        with self.assertRaises(mpd.ConnectionError):
+            client.status()
+        # consistent behaviour, solves bug #11 (github)
+        with self.assertRaises(mpd.ConnectionError):
+            client.status()
+        self.assertIs(client._sock, None)
+
 if __name__ == '__main__':
     unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list