Skip to content

Retry Connect tunnel after rejected WebSocket upgrades - #1784

Merged
SawyerHood merged 2 commits into
get-bb:mainfrom
hemaaanth:fix/connect-retry-http-rejection
Aug 19, 2026
Merged

SawyerHood merged 2 commits into
get-bb:mainfrom
hemaaanth:fix/connect-retry-http-rejection

Conversation

@hemaaanth

Copy link
Copy Markdown
Contributor

Summary

  • bound Connect tunnel opening handshakes to 15 seconds
  • schedule capped-backoff reconnects directly after non-auth HTTP upgrade rejections
  • drain and terminate rejected responses while preserving their HTTP status for diagnostics
  • make reconnect scheduling idempotent when unexpected-response, error, and close arrive for the same attempt
  • clear an elapsed nextRetryAt when the next dial begins

Why

ConnectTunnel handled unexpected-response by recording the HTTP status, but only scheduled another attempt from the WebSocket close handler. Once an unexpected-response listener handles the response, ws leaves cleanup to the caller and a later close event is not guaranteed. That can leave a paired server indefinitely stuck in reconnecting after 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 passed
  • npx --yes [email protected] exec turbo run typecheck --filter=bb-plugin-connect — passed

AGENT GENERATED: by GPT-5.6 Sol

@bb-slop-cop

bb-slop-cop Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I am SlopCop. I am now reviewing this pull request for security, code quality, performance, architecture, and test coverage.

@SawyerHood
SawyerHood force-pushed the fix/connect-retry-http-rejection branch from 165ee92 to 5e5e63a Compare August 19, 2026 19:28
try {
tunnel = new NodeWebSocket(tunnelUrl, {
headers: { authorization: `Bearer ${credential.credential}` },
handshakeTimeout: TUNNEL_HANDSHAKE_TIMEOUT_MS,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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: handshakeTimeout measures idle time, not total handshake time. Partial response bytes reset it.
  • A gate can keep the socket in CONNECTING forever 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]>
@SawyerHood
SawyerHood merged commit c1a8980 into get-bb:main Aug 19, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connect tunnel stops retrying after an HTTP 500 WebSocket upgrade response

2 participants