From 92a072f152417f607bd4cc5f549972ba76a15cad Mon Sep 17 00:00:00 2001 From: Kaliko Jack Date: Mon, 15 Apr 2013 20:13:57 +0200 Subject: [PATCH] Handles better lost connections --- mpd.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mpd.py b/mpd.py index 4ad4804..373bd16 100644 --- a/mpd.py +++ b/mpd.py @@ -236,6 +236,7 @@ class MPDClient(object): def _read_line(self): line = self._rfile.readline() if not line.endswith("\n"): + self.disconnect() raise ConnectionError("Connection lost while reading line") line = line.rstrip("\n") if line.startswith(ERROR_PREFIX): @@ -414,7 +415,7 @@ class MPDClient(object): if sock is not None: sock.close() if err is not None: - raise err + raise ConnectionError(str(err)) else: raise ConnectionError("getaddrinfo returns an empty list") @@ -434,9 +435,15 @@ class MPDClient(object): raise def disconnect(self): - self._rfile.close() - self._wfile.close() - self._sock.close() + if isinstance(self._rfile, socket._fileobject): + print('closing r socket') + self._rfile.close() + if isinstance(self._wfile, socket._fileobject): + print('closing w socket') + self._wfile.close() + if isinstance(self._sock, socket.socket): + print('closing socket') + self._sock.close() self._reset() def fileno(self): -- 2.39.5