Skip to content

Make the browse tunnel auto-start share the start lock (GST-2117) - #4212

Merged
capy-ai[bot] merged 3 commits into
mainfrom
fix-browse-tunnel-auto-start-shares-start-lock
Nov 9, 2026
Merged

capy-ai[bot] merged 3 commits into
mainfrom
fix-browse-tunnel-auto-start-shares-start-lock

Conversation

@garrytan

@garrytan garrytan commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

A browse daemon started with BROWSE_TUNNEL=1 no longer leaks a second public ngrok tunnel when a /tunnel/start call arrives while its own auto-start is still opening. This serves gstack's goal of a browser that agents can share remotely without exposing more than you asked for.

What changed

The new auto-start overlap test opens two ngrok sessions on main and one with this fix

The new auto-start overlap test opens two ngrok sessions on main and one with this fix.

Before/after as text: The new auto-start overlap test opens two ngrok sessions on main and one with this fix
--- Before · before.txt · captured output
+++ After · after.txt · captured output
+$ bun test browse/test/tunnel-start-state.test.ts   # this PR c473d8b
+(pass) two concurrent tunnel starts share one ngrok session and URL [434.70ms]
+(pass) a route start that overlaps the BROWSE_TUNNEL=1 auto-start shares its ngrok session [722.13ms]
+(pass) a tunnel start that fails after ngrok opens leaves the daemon idle-stoppable [384.38ms]
+ 3 pass
+ 0 fail
-$ bun test browse/test/tunnel-start-state.test.ts   # origin/main ba2289e
-(pass) two concurrent tunnel starts share one ngrok session and URL [401.57ms]
-[browse] Tunnel listener bound on 127.0.0.1:<port>, ngrok → https://session-1.invalid
-[browse] Tunnel listener bound on 127.0.0.1:<port>, ngrok → https://session-2.invalid
-(fail) a route start that overlaps the BROWSE_TUNNEL=1 auto-start shares its ngrok session [697.01ms]
-(pass) a tunnel start that fails after ngrok opens leaves the daemon idle-stoppable [316.57ms]
- 2 pass
- 1 fail
 
  • One start lock for both callers. In browse/src/server.ts the single-start lock now lives in startTunnel, which wraps openTunnel (the old body). The /tunnel/start route and the BROWSE_TUNNEL=1 auto-start in start() both go through it, so an overlapping start waits for the one in flight and gets the same URL.
  • Changelog wording. The unreleased fragment from Stop concurrent browse tunnel starts from leaking an ngrok session (GST-2117) #4210 now names both overlap paths.

Tests

browse/test/tunnel-start-state.test.ts gains a case that runs the real start() (headless Chromium, mocked @ngrok/ngrok, BROWSE_TUNNEL=1) in the same child bun test probe, holds the auto-start inside ngrok.forward(), and sends POST /tunnel/start to the daemon's real port. It fails on main ba2289e (opened is ["session-1","session-2"]) and passes here. The dual-listener.test.ts source-guard marker returns to start: (authtoken) => startTunnel({.

Blast radius

The product change is 8 lines in browse/src/server.ts: startTunnel becomes a wrapper that holds the lock, and the factory's tunnel.start callback calls it instead of holding its own. Every throw in openTunnel sits inside its try and .finally always clears the lock, so a failed start can't leave it stuck (read at server.ts:404-411, confirmed by the adversarial reader). No network, secrets, permissions, workflow, install-script or dependency changes.

Why

The #4210 review noted that the BROWSE_TUNNEL=1 auto-start "still calls startTunnel() directly", outside the lock. A /tunnel/start that arrived during that start opened a second ngrok session that closing the tunnel never reached. Now there is one session, whichever path starts first.

Summary

The auto-start and the route share one in-flight tunnel start, a new test drives the real start() to prove it, and the changelog fragment says so.

Test Coverage

All new code paths have test coverage. The new case in browse/test/tunnel-start-state.test.ts runs the real start() with BROWSE_TUNNEL=1 and @ngrok/ngrok mocked, holds the auto-start's forward open, sends a /tunnel/start and checks one session and one URL. It fails on origin/main ba2289e (opened was ["session-1","session-2"]) and passes at c473d8b. dual-listener.test.ts's source-guard marker is back to the original call line; its assertion is unchanged.

Pre-Landing Review

Pre-Landing Review: 1 issue — 0 auto-fixed, 1 asked (0 fixed, 1 skipped), 0 deferred, 0 noted. Adversarial (in-host, Claude): no findings in the range, one older note outside it. Outside voice (GPT-6.1 Sol): clean.

Review notes

Below the blocking floor; recorded here and not fixed in this PR.

  • INFORMATIONAL browse/test/tunnel-start-state.test.ts:113 The overlap probe relies on a fixed 100 ms sleep for the routed /tunnel/start to join the in-flight auto-start before release(); a deterministic join signal would remove the timing dependency.
  • Low (pre-existing, outside the range) closeTunnel() does not wait for an in-flight openTunnel, so a close during a start is a no-op and the start then marks the tunnel active. Same at ba2289e; it does not leak an ngrok session.

Exploratory QA

Not run: no required probe for this change. The failing-first test is the functional check, and it drives the real start() path.

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). Bug fix, plan review exempt; the failing-first test is the repro.

TODOS

No TODO items completed in this PR.

Issues

Refs #4210

Documentation

Status: current. No documentation changes were needed.

Scope: branch fix-browse-tunnel-auto-start-shares-start-lock, base ba2289e to head d881bce: 2 commits and 4 files (browse/src/server.ts, two browse tests and one changelog.d fragment), with no staged, unstaged or new files. I grepped the tunnel docs in README.md, BROWSER.md, ARCHITECTURE.md, docs/REMOTE_BROWSER_ACCESS.md and docs/BROWSER_INTERNALS.md for start-lock and concurrency wording.

Documentation health:

  • README.md: current (the /pair-agent row's tunnel auto-start note is still accurate)
  • BROWSER.md: current (the BROWSE_TUNNEL variable table is unchanged)
  • ARCHITECTURE.md: current (the dual-listener lifecycle is unchanged)
  • docs/REMOTE_BROWSER_ACCESS.md: current (the BROWSE_TUNNEL=1 $B restart setup is still valid)
  • docs/BROWSER_INTERNALS.md: current

Coverage debt: none new. The change is an internal fix to the shared in-flight start lock in startTunnel and adds no public surface. The changelog.d fragment covers the user-visible effect.

Diagram drift: none. No diagram names startTunnel or the start lock.

Test plan

  • bun run test: carried from CI free-tests run 42613170296/1 at tree b96d3ed890d1 (36831 tests, 0 failed)
  • bun test browse/test/tunnel-start-state.test.ts: 2 pass, 1 fail on ba2289e; 3 pass at c473d8b
  • macOS and Windows lanes: free-tests run 42613170296 (cso-macos-launcher, macos-named-regressions) and windows-smoke run 42613170297, both green at d881bce

🤖 Generated with Claude Code

Agent notes

Refs GST-2117. Lane GSTA-443 for GStack Minder. Follows #4210's review note on the BROWSE_TUNNEL=1 auto-start. The second commit only splits a changelog sentence at its colon (prose lint); the reviewed product tree is unchanged.

Approvals

No approvals recorded for branch fix-browse-tunnel-auto-start-shares-start-lock.
Plan: exempt: bug fix
Plan check: exempt bug fix

REVIEW_LOOP: passes=1 blocking_found=0 deferred=0 noted=0 fixed=0 scope=frozen ledgers_read=2 dispatches=1
Gate edits: none detected (6 listed, 4 read; patterns: RH-1,RH-3,RH-4,RH-12,RH-13,RH-14,RH-15,RH-16; unlisted idioms: none)

{
  "schema_version": 1,
  "pr": 4212,
  "head": "ec9238a104cbbcb50af70161081a011a894936d8",
  "version": "1.98.0.0",
  "base": "main",
  "gate": {
    "test": {
      "pass": 1653,
      "fail": 0,
      "skip": 0
    }
  },
  "ci_carried": [
    {
      "lane": "test",
      "run": 42613170296,
      "attempt": 1,
      "tree": "b96d3ed890d1cf6b084473caad3316e46a6c9bda"
    }
  ],
  "session_kind": "unattended",
  "head_tree": "548fc4439f2b2e0079d379924f198ef1b880b77d",
  "product_patch_id": "01d51b517bc5c4d7f2e621523c5cbeec432e2c0c",
  "gstack_version": "1.98.0.0",
  "review_depth": {
    "depth": "light",
    "class": "code",
    "runtime_lines": 12,
    "rules": "66e5174161eecc2c",
    "base": "ba2289e1cd9f2387d069b3b6c51b404d56c4a1e9",
    "registry": "ba2289e1cd9f2387d069b3b6c51b404d56c4a1e9",
    "gate_scan": {
      "tagged": 0,
      "head": "ec9238a104cbbcb50af70161081a011a894936d8"
    },
    "reasons": [
      "runtime=12"
    ],
    "risk": [],
    "mode": "panel"
  },
  "pregate": "requires-remote cleared @548fc4439f2b (8 checks, current for ec9238a104cb)",
  "requires_remote": [
    "free-tests.yml/cso-macos-launcher: https://github.com/garrytan/gstack/actions/runs/42614976852/attempts/1",
    "free-tests.yml/macos-named-regressions: https://github.com/garrytan/gstack/actions/runs/42614976852/attempts/1",
    "windows-smoke.yml/windows-smoke: https://github.com/garrytan/gstack/actions/runs/42614976834/attempts/1"
  ],
  "issues": {
    "fixes": [],
    "refs": [
      4210
    ],
    "flakes": []
  },
  "docs": {
    "status": "current",
    "scope": "docs/, README.md, ARCHITECTURE.md, BROWSER.md (docs/REMOTE_BROWSER_ACCESS.md, docs/BROWSER_INTERNALS.md reviewed)"
  },
  "review_loop": {
    "passes": 1,
    "blocking_found": 0,
    "deferred": 0,
    "noted": 0,
    "fixed": 0,
    "scope": "frozen",
    "rounds": 1,
    "ledgers_read": 2,
    "dispatches": {
      "specialist": 1
    }
  },
  "carry": {
    "from": "d881bce57715c5809548eeec4e42995b5bade7d3",
    "reason": "release metadata",
    "product_patch_id": "01d51b517bc5c4d7f2e621523c5cbeec432e2c0c",
    "changed_paths": [
      "metadata:changelog.d/fix-browse-tunnel-auto-start-shares-start-lock.md",
      "metadata:changelog.d/fix-browse-tunnel-start-leaks-and-stuck-state.md"
    ],
    "merge_commits": 0,
    "range_diff": "1:  c473d8ba5 = 1:  c473d8ba5 Make the BROWSE_TUNNEL=1 auto-start share the browse tunnel start lock (GST-2117)\n2:  d881bce57 = 2:  d881bce57 Split the browse tunnel changelog sentence at its colon (GST-2117)\n-:  --------- > 3:  ec9238a10 Restore the landed tunnel changelog fragment and add this fix's own (GST-2117)",
    "covers": [
      "pre-landing review",
      "adversarial review",
      "gate integrity",
      "documentation audit"
    ],
    "reruns": [
      "CI at head (gstack-ship-queue ready requires it); test and helper code changed since the reviewed head runs there unreviewed"
    ],
    "ancestry": "linear",
    "trees": [
      "b96d3ed890d1cf6b084473caad3316e46a6c9bda"
    ]
  }
}
{"head":"d881bce57715c5809548eeec4e42995b5bade7d3","version":"1.98.0.0","gstack_version":"1.98.0.0","review_loop":{"passes":1,"blocking_found":0,"deferred":0,"noted":0,"fixed":0,"scope":"frozen","rounds":1,"ledgers_read":2,"dispatches":{"specialist":1}},"depth":"light"}

Open in Capy

…k (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>
@trunk-io

trunk-io Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

😎 This pull request was merged.

garrytan and others added 2 commits November 9, 2026 11:55
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…GST-2117)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
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.

1 participant