From 9c67471b49b7f8533fb831b3291006c37088fe5e Mon Sep 17 00:00:00 2001 From: Moepchi Date: Mon, 30 Nov 2026 23:34:06 +0000 Subject: [PATCH] gateway: idle timeout applies to sockets opened after a change Each socket keeps the IDLE_TIMEOUT_MINUTES it was told in its hello. Raising it from 0 in the admin panel used to drop already connected pages after a few minutes without asking, since they never learned they have to send "active". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- gateway/src/index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 0f1ddb4..4f04c5d 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -626,6 +626,10 @@ interface HeartbeatState { isAlive: boolean; /** Last "active" message (user input in the page) or connect. */ lastActive: number; + /** Idle timeout this socket was told in its hello. A later change in the + * admin panel applies to new sockets only: older pages don't know they + * must send "active" and would be dropped without being asked. */ + idleMs: number; } /** IDLE_TIMEOUT_MINUTES in ms, 0 = off. Read per check, the admin panel changes it live. */ @@ -656,8 +660,7 @@ const heartbeatTimer = setInterval(() => { socket.terminate(); continue; } - const idleMs = idleTimeoutMs(); - if (idleMs > 0 && Date.now() - state.lastActive > idleMs + IDLE_GRACE_MS) { + if (state.idleMs > 0 && Date.now() - state.lastActive > state.idleMs + IDLE_GRACE_MS) { // Close handler disconnects the TS session; the message tells the page // this was deliberate, so it doesn't auto-reconnect. socket.send('{"type":"idleTimeout"}'); @@ -748,16 +751,17 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => { // pulled, so the browser must not assume a feature exists just because it // knows about it. A gateway without "hello" (before v0.15) supports the // v0.14 feature set, streaming included; anything newer gets a name here. + const idleMs = idleTimeoutMs(); socket.send( JSON.stringify({ type: "hello", features: gatewayFeatures(), iceServers: turnServers(), - idleTimeoutMinutes: idleTimeoutMs() / 60_000, + idleTimeoutMinutes: idleMs / 60_000, }), ); - heartbeats.set(socket, { isAlive: true, lastActive: Date.now() }); + heartbeats.set(socket, { isAlive: true, lastActive: Date.now(), idleMs }); socket.on("pong", () => { const state = heartbeats.get(socket); if (state) state.isAlive = true;