From b555ccce00de86231c5825e6b98133640c9d526a Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Fri, 12 May 2017 08:22:36 +0200 Subject: [PATCH] Companion: ensure sockets get closed - Should fix #293 --- resources/lib/PlexCompanion.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index a233e103..9b961c5f 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -144,7 +144,24 @@ class PlexCompanion(Thread): offset=data.get('offset')) def run(self): - httpd = False + # Ensure that sockets will be closed no matter what + try: + self.__run() + finally: + try: + self.httpd.socket.shutdown(SHUT_RDWR) + except AttributeError: + pass + finally: + try: + self.httpd.socket.close() + except AttributeError: + pass + log.info("----===## Plex Companion stopped ##===----") + + def __run(self): + self.httpd = False + httpd = self.httpd # Cache for quicker while loops client = self.client threadStopped = self.threadStopped @@ -245,11 +262,3 @@ class PlexCompanion(Thread): sleep(50) client.stop_all() - if httpd: - try: - httpd.socket.shutdown(SHUT_RDWR) - except: - pass - finally: - httpd.socket.close() - log.info("----===## Plex Companion stopped ##===----")