screentinker/server/lib/tcp-keepalive.js
ScreenTinker bcfe3eaf8b fix(#148) Items 2-4: mark-offline closes the socket + tighten ping + TCP keepalive
Item 2: when the heartbeat checker marks a device offline it now also disconnects any socket
it still holds for it, so DB-offline can't diverge from socket-state into a silent half-open
(defensive — the live-socket guard already defers genuinely-live sockets).

Item 3: tighten half-open detection WITHOUT reintroducing the TV-WebKit decode-load risk the
30s pong-timeout was chosen for — lower only pingInterval 30s->15s (probe more often), KEEP
pingTimeout at 30s. Detection = interval+timeout = 45s (was 60s), and the client inherits
these via the handshake so BOTH ends detect a dead peer ~25% sooner. (Deliberately did NOT
drop pingTimeout to ~20s: MAXHUB is a video-playing TV-class device and the code comment
warns tighter timeouts cause spurious drops under decode load.)

Item 4: SO_KEEPALIVE on every accepted connection (lib/tcp-keepalive.js) so a half-open TCP
can't persist indefinitely at the OS layer, independent of the app ping.

Tests: server closes a non-ponging peer within ~pingInterval+pingTimeout while a ponging peer
survives; a device whose transport dies ends offline with its connection torn down; keepalive
applied to each accepted connection (and never breaks setup on error). Suite 328/328.
2026-07-02 14:59:25 -05:00

15 lines
681 B
JavaScript

'use strict';
// #148 Item 4 — enable TCP SO_KEEPALIVE on every accepted connection so a half-open TCP
// (e.g. a connection severed silently by an edge firewall/NAT reap, leaving no FIN) can't
// persist indefinitely at the OS layer, independent of the Engine.IO application ping. The
// http/https server's 'connection' event fires with the raw net.Socket (before TLS), which
// is where setKeepAlive belongs. Best-effort — never let it break connection setup.
function applyTcpKeepAlive(server, idleMs) {
server.on('connection', (socket) => {
try { socket.setKeepAlive(true, idleMs); } catch (_) { /* best-effort */ }
});
}
module.exports = { applyTcpKeepAlive };