fix(host-service): guard websocket upgrade sockets against peer resets
Problem
HOST-SERVICE-5: fatal Error: read ECONNRESET with a single frame at TCP.onStreamRead — a raw TCP socket emitting 'error' with no listener attached.
Root cause: Node's http server detaches its own socket error handling before emitting 'upgrade', and @hono/node-ws's injectWebSocket upgrade handler then awaits app.request() before ws.handleUpgrade adopts the socket (which is when ws attaches its own error handler). A peer resetting the connection during that async window — or after the non-ws rejection path's socket.end() — emits an unhandled 'error' on the bare socket, which surfaces as an uncaught exception in the host-service process.
Fix
Attach an 'error' listener to the socket on every 'upgrade' event, registered before injectWebSocket. It logs the error code, request path, and remote address. The listener stays attached for the socket's lifetime, so it also covers any later gaps; once ws adopts the socket its own handler destroys it as before.
Deliberately no Sentry.captureException here — peer resets are routine network noise, and the point is to stop them from being reported as fatals. Also deliberately not a process.on('uncaughtException') swallow — safety.ts already provides that as a last resort; this handles the error at the socket where the context lives.
Audit of other socket sites (no changes needed)
- http server request sockets: Node attaches its own per-connection error handler and routes errors to
'clientError', which has safe default handling (destroy + 400). No crash path. - DaemonClient (unix socket):
'error'handler attached on connect; connect-phase errors reject the open promise. Also would show asPipe.onStreamRead, notTCP. - TunnelClient (relay + local channel WebSockets): all sockets have
onerror/onclosehandlers; undici WebSocket errors never throw uncaught. - pty-daemon server: per-connection
'error'handler present; daemon has no Sentry init, so it can't be the source of this issue anyway. - Server-side
wsconnections:@hono/node-wsattachesws.on('error')inupgradeWebSocket, andwshandles underlying socket errors by destroying the socket without emitting.
Refs HOST-SERVICE-5.
https://claude.ai/code/session_01KLJknVKnpN3ExAL8aCpQKG
Summary by cubic
Prevent host-service crashes caused by unhandled ECONNRESET during WebSocket upgrades. Attach a per-upgrade socket error listener so peer resets are logged (path only), not fatal.
- Bug Fixes
- Add server 'upgrade' handler that registers
socket.on('error')beforeinjectWebSocketin@hono/node-ws, covering the async window beforewsadopts the socket. - Log error code, remote address, and request path only (query string stripped to avoid leaking the
token/HOST_SERVICE_SECRET); no Sentry capture to avoid noise. Refs HOST-SERVICE-5.
- Add server 'upgrade' handler that registers
Summary by CodeRabbit
- Bug Fixes
- Improved WebSocket connection error handling.
- Prevented socket errors during connection upgrades from unexpectedly terminating the service.
- Added clearer diagnostic logging for connection resets and other WebSocket errors.