fix: add receive timeout to WebSocket to prevent silent hangs #98

Closed
opened 2026-08-27 22:45:23 +02:00 by SirStone · 1 comment
Owner

Problem

The hand-rolled WebSocket client (ws_client.nim) uses blocking socket.recv() with no timeout. If the server disappears without sending a close frame (network flap, server crash), the main thread hangs indefinitely. No logs, no error, no recovery. The bot becomes a silent corpse.

Solution

Set SO_RCVTIMEO on the socket via socket.setSockOpt(OptRcvTimeo, timeout) and add error handling when receive times out — detect disconnection, log it, and clean up gracefully.

Files

  • src/robocode_tankroyale_botapi/ws_client.nim
  • src/robocode_tankroyale_botapi/bot.nim (receive loop error handling)
## Problem The hand-rolled WebSocket client (`ws_client.nim`) uses blocking `socket.recv()` with no timeout. If the server disappears without sending a close frame (network flap, server crash), the main thread hangs indefinitely. No logs, no error, no recovery. The bot becomes a silent corpse. ## Solution Set `SO_RCVTIMEO` on the socket via `socket.setSockOpt(OptRcvTimeo, timeout)` and add error handling when receive times out — detect disconnection, log it, and clean up gracefully. ## Files - `src/robocode_tankroyale_botapi/ws_client.nim` - `src/robocode_tankroyale_botapi/bot.nim` (receive loop error handling)
Author
Owner

Fixed in v1.0.2. Added 30s SO_RCVTIMEO on WebSocket socket via stdlib timeout overload. 5 consecutive timeouts trigger disconnect. Validated with 3-round battle test — no false positives during normal gameplay.

Fixed in v1.0.2. Added 30s SO_RCVTIMEO on WebSocket socket via stdlib timeout overload. 5 consecutive timeouts trigger disconnect. Validated with 3-round battle test — no false positives during normal gameplay.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SirStone/SirRoboGarage#98