Repository navigation
Stop concurrent browse tunnel starts from leaking an ngrok session (GST-2117) - #4210
Merged
Merged
Conversation
…ST-2117) Two /tunnel/start calls that both saw the tunnel off each opened an ngrok session; the daemon kept only the second, so the first stayed public and closeTunnel could not reach it. ctx.tunnel.start is now single-flight, so a concurrent caller shares the in-flight start and its URL. A start that failed after ngrok.forward() reset only tunnelListener, leaving tunnelActive, tunnelUrl and tunnelServer set, so the daemon never idled out. The catch now clears all four. Tests found and first written by GEVA-449's pstack verification. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
|
😎 This pull request was merged. |
3 tasks done
capy-ai Bot
added a commit
that referenced
this pull request
Nov 9, 2026
) * Make the BROWSE_TUNNEL=1 auto-start share the browse tunnel start lock (GST-2117) #4210 made ctx.tunnel.start single-flight, but the BROWSE_TUNNEL=1 auto-start in start() called startTunnel() directly, so a /tunnel/start that overlapped it still opened a second ngrok session closeTunnel could not reach. The lock now lives in startTunnel itself, which both callers use. The unreleased changelog fragment names both paths. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Split the browse tunnel changelog sentence at its colon (GST-2117) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Restore the landed tunnel changelog fragment and add this fix's own (GST-2117) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.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.
Starting the browse tunnel from two places at once no longer leaves a public ngrok tunnel that the daemon can't close, and a failed start no longer keeps an idle daemon running forever. This serves gstack's goal of a browser that agents can share remotely without exposing more than you asked for.
What changed
The new tunnel test fails on main (two ngrok sessions; idle daemon never exits) and passes with this fix.
Before/after as text: The new tunnel test fails on main (two ngrok sessions; idle daemon never exits) and passes with this fix
ctx.tunnel.startinbrowse/src/server.tsis now single-flight. APOST /tunnel/startthat arrives while another start is running waits for the samestartTunnel()promise and returns the same URL, instead of opening a second ngrok session.startTunnel()fails afterngrok.forward()(for example, the state-file write throws), the catch now clearstunnelServer,tunnelUrlandtunnelActivealong withtunnelListener, soidleCheckTickcan shut an idle daemon down again.Why
Two quick
/tunnel/startcalls opened two ngrok sessions. The daemon kept the second, so the first stayed public on the user's ngrok account until the process exited, and closing the tunnel never reached it. A start that failed after ngrok opened also left the daemon convinced a tunnel was live, so a headless daemon and its Chromium never stopped on idle. Both were found by GEVA-449's pstack verification.Tests
browse/test/tunnel-start-state.test.tsdrives the real/tunnel/startroute throughbuildFetchHandler().fetchLocalwith@ngrok/ngrokmocked. Each case runs in a childbun test(same pattern aspty-inject-scan.test.ts) so the module mock and the module-level tunnel state can't leak into other shard files. Both cases fail on origin/main (c25580e:openedwas["session-1","session-2"]; idle tick never exited) and pass with the fix.dual-listener.test.ts's source guard marker now matches the single-flight line; its assertion is unchanged.Blast radius
The product change is 4 lines in
browse/src/server.ts: the factory'stunnel.startcallback andstartTunnel's catch block. TheBROWSE_TUNNEL=1auto-start instart()still callsstartTunnel()directly, so it shares the catch fix but not the single-flight slot (see Review notes; pre-existing on main, not widened here, read at server.ts:2128). No network, secrets, permissions, workflow, install-script or dependency changes.Summary
A pair-agent caller that hit
/tunnel/starttwice could leak a public tunnel, and a failed start could pin the daemon awake. Route-level starts now coalesce into one in-flight start, and the failure path resets all of the tunnel state, so the idle timer works again.Test Coverage
All new code paths have test coverage.
browse/test/tunnel-start-state.test.tsdrives the real/tunnel/startroute throughbuildFetchHandler().fetchLocalwith@ngrok/ngrokmocked, each case in a childbun testso the module mock and module-level tunnel state can't leak into other shard files. Both cases fail on origin/main c25580e (openedwas["session-1","session-2"]; the idle tick never exited) and pass at ab195de.dual-listener.test.ts's source-guard marker now matches the single-flight line; its assertion is unchanged.Tests ran: test 1653 (files, carried from CI run 42607749960/1 at tree 1d192cd69b8a)
Pre-Landing Review
Pre-Landing Review: 2 issues — 0 auto-fixed, 1 asked (0 fixed, 1 skipped), 0 deferred, 1 noted. Adversarial (in-host, Claude): 5 findings, all below the blocking floor and noted. Outside voice: skipped by the review plan (code class, no numeric claims).
browse/test/tunnel-start-state.test.ts:50(probe env inherits GSTACK_STATE_ROOT). The repo-roottest-setup.tspreload strips inheritedGSTACK_STATE_ROOT/GSTACK_STATE_DIRbefore the probe children inherit the environment; QA capture 001 showed 0 egress rows under an outerGSTACK_STATE_ROOT.TIMING_ASSERTION: browse/test/tunnel-start-state.test.ts:80 sleep-then-assert(the 50 ms sleep only lets both starts reach the gated forward; a slow tick makes the test fail, never pass falsely) and one MEDIUMinternal.url_privatehit on the test's synthetic loopback fixture URL at line 38.REVIEW_LOOP: passes=1 blocking_found=0 deferred=0 noted=1 fixed=0 scope=frozen ledgers_read=2 dispatches=1
Gate edits: none detected (2 listed, 1 read; patterns: RH-1,RH-3,RH-4,RH-12,RH-13,RH-14,RH-15,RH-16; unlisted idioms: none)
Review notes
Below the blocking floor (advisory, informational, test-gap or wording items); recorded here and not fixed in this PR.
browse/src/server.ts:2128The single-flight guard (tunnelStarting) only wraps the factory's ctx.tunnel.start. The BROWSE_TUNNEL=1 auto-start still calls startTunnel() directly, and it runs after the daemon is already serving and has written its state file. So a /tunnel/start that arrives while the auto-start's ngrok.forward is pending sees tunnelActive=false and opens a second session. Example: a CLI pair-agent launched from cold with BROWSE_TUNNEL=1 inherited. Whichever forward resolves last overwrites tunnelListener and tunnelServer, so the other ngrok session and its Bun listener are never closed. This is the same leak the commit title names, left open on the second caller.browse/src/server.ts:2128BROWSE_TUNNEL=1 auto-start calls startTunnel() outside tunnelStarting; a /tunnel/start overlapping it (documented restart-then-pair-agent flow) opens a second ngrok session that closeTunnel cannot reachbrowse/src/server.ts:468startTunnel's catch closes the module-level tunnelListener (possibly another session's) and the new tunnelServer = null drops the other session's Bun listener; reachable only through finding 1's overlapbrowse/src/routes/tunnel.ts:26Two concurrent restarts of a dead tunnel: probe/close run before the coalesced start, so B's close can tear down A's new session and A gets a closed URLbrowse/src/routes/pairing.ts:133A close during an in-flight start is lost; the pending start then installs a live tunnel (pre-existing)browse/src/server.ts:1791A coalesced caller's authtoken is ignored in favour of the first caller'sExploratory QA
Functional smoke (bounded 300 s / 12 probes), report-only, isolated temp state roots. One probe, capture 001: the HEAD copy of
tunnel-start-state.test.tsrun with a fresh outerGSTACK_STATE_ROOT. Expected and observed: 2 pass, 0 fail, 0 egress rows in the outer root. Outcome: pass (concurrency/order, partial-failure recovery and state isolation). Not run (blocked): theBROWSE_TUNNEL=1auto-start racing a/tunnel/start, becausestart()launches Chromium and this machine has no browser.Design Review
No frontend files changed — design review skipped.
Eval Results
No prompt-related files changed — evals skipped.
Review bot comments
No review bot comments.
Plan Completion
Plan completion audit: not run (no plan is bound to this branch and no docs/designs/ file matches). Bug fix, plan review exempt; the failing-first test is the repro.
TODOS
No TODO items completed in this PR.
Documentation
Status: current. No documentation changes were needed.
Scope: base c25580e to head ab195de on branch fix-browse-tunnel-start-leaks-and-stuck-state. Four committed files (browse/src/server.ts, two browse tests, one changelog.d fragment), with nothing staged, unstaged or untracked. The change makes concurrent
/tunnel/startcalls share one in-flight start, so only one ngrok session ever opens. It also resets the tunnel state when a start fails after ngrok has opened.Documentation health:
/tunnel/start, torn down on/tunnel/stop) is still accurate.Coverage debt: none new, because no public surface was added or changed. The tunnel's concurrency and failure-reset behavior is an internal guarantee and is covered only by the changelog fragment and tests/tunnel-start-state.test.ts.
Diagram drift: none. The tunnel diagrams in ARCHITECTURE.md and docs/REMOTE_BROWSER_ACCESS.md show listeners and ports, and those didn't change.
Test plan
bun run test: carried from CI free-tests run 42607749960/1 at tree 1d192cd69b8a (36829 tests, 0 failed)bun test browse/test/tunnel-start-state.test.ts: fails 0/2 on c25580e, passes 2/2 at ab195de🤖 Generated with Claude Code
Agent notes
Refs GST-2117. Lane GSTA-443 for GStack Minder. Source patches: org drive
research/viral-claims/pstack-formal-verification/fixes/gstack/browse-tunnel-*, read as text and rewritten for current main. Out of scope: GEVA's autoplan resume rebind bug. Localbrowse/test/watchdog.test.ts"default headless mode" fails identically on unmodified main on this machine (no browser to boot), not related.Approvals
No approvals recorded for branch fix-browse-tunnel-start-leaks-and-stuck-state.
Plan: exempt: bug fix
Plan check: exempt bug fix
REVIEW_LOOP: passes=1 blocking_found=0 deferred=0 noted=1 fixed=0 scope=frozen ledgers_read=2 dispatches=1
Gate edits: none detected (2 listed, 1 read; patterns: RH-1,RH-3,RH-4,RH-12,RH-13,RH-14,RH-15,RH-16; unlisted idioms: none)