Enable TCP keepalive on client socket - #63
Open
TooAngel wants to merge 1 commit into
Open
Conversation
Chromecasts silently drop idle connections. Without SO_KEEPALIVE the OS never probes, and the next write hangs until the caller's own timeout fires. setKeepAlive(true, 10000) lets the OS detect dead peers after ~10s of TCP-level idle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Chromecasts silently drop idle TCP connections — no FIN, no RST. The client's
tls.connectsocket doesn't haveSO_KEEPALIVEenabled, so the OS never probes the peer, and a dead socket looks alive.Symptom: after some idle period, the next
send()writes into a socket that will never be read. Node reports success (the kernel buffer accepts the bytes), but nothing gets through. The application-layer heartbeat innode-castv2-clientis supposed to detect this via missing PONGs, but does not fire reliably — see the long-standing thibauts/node-castv2-client#44. Callers end up hanging on their own timeouts.Fix
One line: enable
SO_KEEPALIVEon the TLS socket with a 10 s initial delay. When the socket is genuinely idle for that long, the OS starts sending keepalive probes; if the peer is gone, the OS closes the socket and Node emitsclose— which the existingonclosehandler already handles.Why it's safe
Verified against
Real Chromecast (Blaupunkt PMR100-style receiver). Before the change, first
player.loadafter >5 min of idle hung for 8 s until the caller's timeout fired, then retried. After the change (patched at the consumer level as a preview), no hang and no retry needed.