From 28106b9fb0590785500d835e003d8b12b203eb75 Mon Sep 17 00:00:00 2001 From: nirnx Date: Fri, 19 Dec 2025 13:24:56 -0800 Subject: [PATCH] fix(socket): Properly cleanup Discord IPC socket on disconnect - Add close() method to backend to disconnect Discord client on shutdown - Fix sockets.py disconnect() to reset socket to None after closing - Add try/except around socket shutdown/close for robustness This fixes the error that occurred when restarting StreamController: "failed to connect to socket /run/user/1000/discord-ipc-0, trying next socket. [Errno 106] Transport endpoint is already connected" --- backend.py | 11 +++++++++++ discordrpc/sockets.py | 11 +++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend.py b/backend.py index 39c15d2..e985360 100644 --- a/backend.py +++ b/backend.py @@ -151,5 +151,16 @@ def _get_current_voice_channel(self): self.setup_client() self.discord_client.get_selected_voice_channel() + def close(self): + """Cleanup method called when backend is shutting down""" + log.debug("Closing Discord backend connection") + if self.discord_client: + try: + self.discord_client.disconnect() + except Exception as ex: + log.error(f"Error disconnecting Discord client: {ex}") + self.discord_client = None + self._is_authed = False + backend = Backend() diff --git a/discordrpc/sockets.py b/discordrpc/sockets.py index e9b591c..a0f786b 100644 --- a/discordrpc/sockets.py +++ b/discordrpc/sockets.py @@ -42,8 +42,15 @@ def connect(self): def disconnect(self): if self.socket is None: return - self.socket.shutdown(socket.SHUT_RDWR) - self.socket.close() + try: + self.socket.shutdown(socket.SHUT_RDWR) + except Exception: + pass # Socket might already be disconnected + try: + self.socket.close() + except Exception: + pass + self.socket = None # Reset so connect() creates a fresh socket def send(self, payload, op): payload = json.dumps(payload).encode('UTF-8')