Add WebSocket heartbeat with pong-timeout dead-connection detection - #7
Merged
Conversation
The previous ping was fire-and-forget: it sent frames but never verified a response, so a silently dead connection (half-open TCP, dropped network, LB reaping an idle connection) went undetected until the OS TCP stack gave up minutes later. During that window isConnected()/healthcheck reported healthy and compilations silently timed out one by one. The heartbeat now sends a ping every interval and arms a pong-timeout; if no pong or other inbound activity arrives before the deadline, the socket is treated as dead and terminate()d, which triggers the existing reconnect path. Any inbound message also counts as liveness, so a busy connection is never killed for a missed pong. The events server implements keepalive at the application level (it replies to a "ping" text message with a "pong" text message), because API Gateway WebSocket APIs answer protocol-level ping/pong at the AWS edge -- which proves only that the front door is up, not that the Lambda backend is reachable. A configurable PingMode (--ping-mode / WEBSOCKET_PING_MODE, default "application") selects text ping/pong (true end-to-end liveness) or protocol ping/pong. Healthcheck now also reports lastActivityTime / secondsSinceLastActivity. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
The WebSocket ping was fire-and-forget — it sent frames every 30s but never verified a response. A silently dead connection (half-open TCP, dropped network, a load balancer reaping an idle connection) went undetected until the OS TCP stack gave up, which can take minutes. During that window
isConnected()and the healthcheck reported healthy while compilation subscriptions silently hung until their per-request timeout.Change
terminate()d, which triggers the existing reconnect path. Dead connections are now caught in ~pingInterval + pongTimeout(≈40s) instead of minutes.PingModesetting (--ping-mode/WEBSOCKET_PING_MODE, defaultapplication):application— sends a"ping"text message; the events server replies with a"pong"text message. This round-trips through the Lambda backend = true end-to-end liveness.control— protocol-levelws.ping()/'pong'. Behind API Gateway these are answered at the AWS edge, so they only prove the front door is up, not that the Lambda is reachable.lastActivityTimeandsecondsSinceLastActivity.Why app-level is the default
API Gateway WebSocket APIs don't surface protocol ping/pong control frames to the Lambda backend. The matching server-side handler (
events-lambda,message === 'ping'→'pong') has been deployed to prod/staging/beta and verified: text"pong"round-trips on all three stages (prod ~267ms, staging ~177ms, beta ~894ms cold) — comfortably within the 10spongTimeout.Tests
New unit coverage for: ping cadence, termination on silence, staying alive under text
"pong"replies, staying alive under application messages, reconnect-after-timeout, heartbeat stop on close, andcontrolmode (protocol ping + protocol pong). 100 tests pass, typecheck and lint clean.🤖 Generated with Claude Code