From 56bffee76714f1b46a67efb515d7f54a524a1234 Mon Sep 17 00:00:00 2001 From: Devin Collins <3997333+ImDevinC@users.noreply.github.com> Date: Tue, 30 Dec 2025 22:16:38 -0800 Subject: [PATCH 1/2] fix(sockets): improves how we read from sockets --- discordrpc/asyncdiscord.py | 4 +++- discordrpc/constants.py | 3 ++- discordrpc/sockets.py | 14 ++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/discordrpc/asyncdiscord.py b/discordrpc/asyncdiscord.py index 919201a..e21a9a7 100644 --- a/discordrpc/asyncdiscord.py +++ b/discordrpc/asyncdiscord.py @@ -6,6 +6,7 @@ from loguru import logger as log from .sockets import UnixPipe, SOCKET_BAD_BUFFER_SIZE, SOCKET_DISCONNECTED +from socket import timeout from .commands import * from .exceptions import * from .constants import MAX_SOCKET_RETRY_ATTEMPTS @@ -82,10 +83,11 @@ def poll_callback(self, callback: callable): while self.polling: try: val = self.rpc.receive() + except timeout: + continue except Exception as ex: log.error(f"error receiving data from socket. {ex}") self.disconnect() - return if val[0] == SOCKET_BAD_BUFFER_SIZE: log.debug("bad buffer size when receiving data from socket") if val[0] == SOCKET_DISCONNECTED: diff --git a/discordrpc/constants.py b/discordrpc/constants.py index 34dbfda..b5e1a1b 100644 --- a/discordrpc/constants.py +++ b/discordrpc/constants.py @@ -6,4 +6,5 @@ 10 # Number of IPC sockets to try (discord-ipc-0 through discord-ipc-9) ) SOCKET_SELECT_TIMEOUT = 0.1 # Socket select timeout in seconds (reduced from 1.0s for 90% latency improvement) -SOCKET_BUFFER_SIZE = 1024 # Socket receive buffer size in bytes +#SOCKET_BUFFER_SIZE = 1024 # Socket receive buffer size in bytes +SOCKET_BUFFER_SIZE = 8 diff --git a/discordrpc/sockets.py b/discordrpc/sockets.py index 856b521..5c949f4 100644 --- a/discordrpc/sockets.py +++ b/discordrpc/sockets.py @@ -14,7 +14,7 @@ SOCKET_BAD_BUFFER_SIZE: int = -2 SOCKET_SEND_TIMEOUT: int = 5 SOCKET_CONNECT_TIMEOUT: int = 2 -SOCKET_RECEIVE_TIMEOUT: int = 5 +SOCKET_RECEIVE_TIMEOUT: int = 10 class UnixPipe: def __init__(self): @@ -64,7 +64,6 @@ def disconnect(self): self.socket = None # Reset so connect() creates a fresh socket def send(self, payload, op): - log.debug(f"Sending payload: {payload} with op: {op}") payload_bytes = json.dumps(payload).encode("UTF-8") header = struct.pack(" (int, str): - self.socket.settimeout(SOCKET_RECEIVE_TIMEOUT) data = self.socket.recv(SOCKET_BUFFER_SIZE) if len(data) == 0: return SOCKET_DISCONNECTED, {} header = data[:8] code = int.from_bytes(header[:4], "little") length = int.from_bytes(header[4:], "little") - all_data = data[8:] - buffer_size = length - len(all_data) - if buffer_size < 0: + all_data = b"" + if length < 0: return SOCKET_BAD_BUFFER_SIZE, {} - data = self.socket.recv(length - len(all_data)) - all_data += data + if length > 0: + data = self.socket.recv(length) + all_data += data return code, all_data.decode("UTF-8") From 3ae94c22d8d588bdf7dbdba8115ce88a68e8ff73 Mon Sep 17 00:00:00 2001 From: Devin Collins <3997333+ImDevinC@users.noreply.github.com> Date: Tue, 30 Dec 2025 22:17:07 -0800 Subject: [PATCH 2/2] chore(manifest): Update manifest --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 727d436..d5bafa7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "version": "1.9.1", + "version": "1.9.2", "thumbnail": "store/thumbnail.png", "id": "com_imdevinc_StreamControllerDiscordPlugin", "name": "Discord - Debug",