Repository navigation
Retry Connect tunnel after rejected WebSocket upgrades - #1784
Conversation
|
🚨 SLOP COP 🚨 · I am SlopCop. I am now reviewing this pull request for security, code quality, performance, architecture, and test coverage. |
165ee92 to
5e5e63a
Compare
| try { | ||
| tunnel = new NodeWebSocket(tunnelUrl, { | ||
| headers: { authorization: `Bearer ${credential.credential}` }, | ||
| handshakeTimeout: TUNNEL_HANDSHAKE_TIMEOUT_MS, |
There was a problem hiding this comment.
🚨 slopcop/review — The timeout is not a fixed 15-second deadline
ws maps handshakeTimeout to a Node socket idle timeout. Partial response bytes reset that timeout. A peer can keep this attempt in CONNECTING forever, so the tunnel never starts another retry.
I reproduced this with trusted ws 8.21.0 code. A 50 ms timeout stayed in CONNECTING after 304 ms when a loopback server sent one valid header byte every 20 ms.
Please add an absolute timer for each dial. Clear it after open, rejection, error, close, stop, or socket replacement. Add a loopback test that sends partial headers and verifies a later retry.
There was a problem hiding this comment.
Addressed in 56d4ad6: added an absolute per-dial deadline (TUNNEL_HANDSHAKE_TIMEOUT_MS) alongside ws's idle handshakeTimeout. It schedules the capped-backoff retry and terminates the attempt; it is cleared on open and on any scheduled reconnect. Verified with a loopback gate that drips one header byte every 20ms: the tunnel now retries at ~17s (15s deadline + 2s backoff). Added a fake-timer regression test in tunnel-lifecycle.test.ts.
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
Plain-English summary
This PR makes remote Connect access retry when the gate rejects a WebSocket upgrade. It also adds a handshake timeout and drains rejected responses.
Finding
- Medium:
handshakeTimeoutmeasures idle time, not total handshake time. Partial response bytes reset it. - A gate can keep the socket in
CONNECTINGforever by sending slow header bytes. The tunnel then cannot retry. - Add an absolute timer for each dial. Clear it after every terminal event. Add a real loopback test for slow partial headers.
I confirmed this behavior with trusted ws 8.21.0 code. A 50 ms timeout remained active after 304 ms of slow header data.
Other review results
- The security review found no issue.
- The retry guard, cleanup, capped backoff, and normal performance look sound.
- GitHub CI passed all 81 Connect tests for the reviewed SHA. Build, typecheck, lint, and the relevant wider test jobs also passed.
- I did not execute code from the fork. A browser test cannot reach this pre-upgrade failure safely without live Connect credentials.
Architecture
The plugin and host daemon repeat much of the socket retry lifecycle. A shared tunnel controller could prevent drift, but that refactor need not block this fix.
The author rebased the PR during this review. The new head keeps the same two-file patch, while this review marker identifies the requested SHA.
ws's handshakeTimeout is a socket idle timeout, so a peer that drips header bytes can hold a dial in CONNECTING forever. Add an absolute deadline per dial that schedules the capped-backoff retry and terminates the attempt; clear it on open and on any scheduled reconnect. Co-Authored-By: Claude <[email protected]>
Summary
unexpected-response,error, andclosearrive for the same attemptnextRetryAtwhen the next dial beginsWhy
ConnectTunnelhandledunexpected-responseby recording the HTTP status, but only scheduled another attempt from the WebSocketclosehandler. Once anunexpected-responselistener handles the response,wsleaves cleanup to the caller and a latercloseevent is not guaranteed. That can leave a paired server indefinitely stuck inreconnectingafter a transient HTTP 500.The handshake deadline also prevents a replacement dial from remaining pending forever when the gate accepts the connection but never completes the upgrade.
Fixes #1782.
Verification
npx --yes [email protected] exec turbo run test --filter=bb-plugin-connect --force— 81 tests passednpx --yes [email protected] exec turbo run typecheck --filter=bb-plugin-connect— passed