Skip to content

Commit 36e33d8

Browse files
committed
fix: handle WebSocket ping errors and DCC peer logging with None
send_ping could leak unretrieved exceptions if the websocket closed between the closed check and the ping call; catch and exit cleanly. DCCProtocol.connection_made used %d for peerport, which crashed when the peer info was not available. Use %s to log safely.
1 parent b609412 commit 36e33d8

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎dccbot/aiodcc.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def connection_made(self, transport: asyncio.BaseTransport) -> None:
2828
else:
2929
self.connection.peeraddress = None
3030
self.connection.peerport = None
31-
log.debug("DCC connection from %s:%d", self.connection.peeraddress, self.connection.peerport)
31+
log.debug("DCC connection from %s:%s", self.connection.peeraddress, self.connection.peerport)
3232
self.connection.reactor._handle_event(self.connection, irc.client.Event("dcc_connect", self.connection.peeraddress, None, None))
3333
if hasattr(self.connection, "server") and self.connection.server:
3434
self.connection.server.close()

‎dccbot/app.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,16 @@ async def websocket_handler(self, request: web.Request) -> web.WebSocketResponse
527527
# Send periodic ping frames to keep the connection alive
528528
async def send_ping() -> None:
529529
while True:
530-
await asyncio.sleep(10) # Send a ping every 10 seconds
531-
if ws.closed:
530+
try:
531+
await asyncio.sleep(10) # Send a ping every 10 seconds
532+
if ws.closed:
533+
break
534+
await ws.ping()
535+
except ConnectionResetError:
536+
break
537+
except Exception:
538+
logger.exception("WebSocket ping failed")
532539
break
533-
await ws.ping()
534540

535541
# Start the ping task
536542
ping_task = asyncio.create_task(send_ping())

0 commit comments

Comments
 (0)