Skip to content

Free a preview port slot when a forwarded connection errors - #384

Closed
minutechreview wants to merge 1 commit into
useagenthq:mainfrom
minutechreview:fix/forwarder-error-slot-304
Closed

minutechreview wants to merge 1 commit into
useagenthq:mainfrom
minutechreview:fix/forwarder-error-slot-304

Conversation

@minutechreview

@minutechreview minutechreview commented Oct 25, 2026 •

Copy link
Copy Markdown

In the loopback port forwarder, error() set socket.data.closed = true without lowering the listener's connection count. When close() ran next, it saw closed and returned early, so the count stayed at 1 and sweep() never closed that listener. Enough errored connections and a machine runs out of its 64 preview port slots.

  • backend/src/runners/loopback.ts: one releaseConnection helper marks the socket closed, lowers the count and bumps lastActivity, and only does this once per socket. close() and error() both call it. close() still ends the link stream, error() still resets it.
  • backend/src/runners/loopback.test.ts: new case that drives a socket through open, error, then close (the Bun.listen handlers are captured with spyOn), moves the clock past FORWARDER_IDLE_MS, and expects sweep() to close the listener.

Testing:

  • bun test src/runners/loopback.test.ts: 4 pass. With loopback.ts from main, the new case fails (sweep() returns 0, expected 1).
  • bun test src/runners src/runs/terminal.test.ts: 58 pass.
  • backend tsc --noEmit (TypeScript 5.9.3) is clean.

Fixes #304


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

Signed-off-by: minutechreview <141821420+minutechreview@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Free a preview port slot when a forwarded connection errors

2 participants