Skip to content

Web: one held connection per tab, and warn a tab starved by the browser's connection limit - #3108

Merged
cliffhall merged 4 commits into
v2/mainfrom
v2/fix/3107-tab-connection-limit
Dec 1, 2026
Merged

cliffhall merged 4 commits into
v2/mainfrom
v2/fix/3107-tab-connection-limit

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #3107

Problem

Chrome allows six HTTP/1.1 connections per host, and every tab of the origin shares them. Each Inspector tab held up to three SSE responses open: /api/servers/events, /api/events/webhook/deliveries, and /api/mcp/events for each connected session. Two connected tabs therefore used all six connections. The second tab's initialize then queued in the browser until the client gave up after 30s.

Approach

As discussed on the issue: one combined stream per tab, plus a clear warning when a tab is still starved. We chose not to move the streams to WebSockets, to avoid a second transport and a wider attack surface.

One stream per tab. GET /api/tab/stream opens with a hello { tabId } frame. It then carries the servers change signal, webhook deliveries, and mcp { sessionId, event } for every session attached to it with POST /api/tab/attach. When a session's events end, the tab gets a session_end { sessionId } frame instead of a closed stream. The wire format is in core/mcp/remote/tabStream.ts. Attaching keeps the existing /api/mcp/events behaviour:

The per-purpose routes remain for callers outside the browser.

Browser side. TabEventChannel (core/mcp/remote/tabEventChannel.ts, browser-safe) is created once per page by src/lib/tabEventChannel.ts. It reopens the stream after a drop, and a drop ends every attached session the way a dropped /api/mcp/events stream did. Three things now use it:

  • RemoteClientTransport, through a new eventChannel option. With the option it attaches its session to the channel; without it, it opens its own stream as before. The event handling moved into handleRemoteEvent / handleEventStreamEnd, which both paths share.
  • useServers, through a new subscribeServerChanges option.
  • useWebhookDeliveries, which now only routes frames. The stream, its retries and its error toast moved to the channel. listenForWebhookDeliveries and WEBHOOK_STREAM_ERROR_NOTIFICATION_ID are removed.

Starvation warning. A browser queues requests it has no connection for; it doesn't fail them, so the channel measures the delay instead. If the stream's hello, or a GET /api/tab/ping sent after it, takes more than 5s against the local backend, the tab is starved. It then opens Active Sessions with the warning. The warning clears when the slow request finally completes, because that is exactly when a connection frees up. A tab also re-probes whenever it comes back into view, since tabs opened while it was hidden can starve it.

The wording says "until another Inspector tab is closed" rather than "another session". Each tab now holds one connection however many sessions it has, so ending a session frees nothing.

Behaviour change to note

Every open tab now listens for webhook deliveries. Before, only a tab with a session listened. So if a delivery arrives while some other Inspector tab is open, the receiver acknowledges it even when that tab has no matching subscription and drops it. Before, it would have answered 503 and the server would retry. This only matters when the subscribing tab is gone while another tab is open.

Verification

  • Real browser (Chromium via Playwright, one profile, dev server):
    • Tab 1 connects to everything-server-default, and tab 2 then connects to filesystem-server-default in about 1s. That is the original hang.
    • Tabs 3–5 load normally.
    • Tab 6 opens Active Sessions with the warning (screenshot below).
    • Closing another tab clears it.
  • Tests: a new integration suite (tab-stream.test.ts) runs the routes end to end against real stdio children. There are also unit suites for the channel, the transport's channel path, useTabStarved, the page-level channel, the useServers option and the modal alert.
  • npm run format and npm run local:gate both pass.

Screenshot

The starved tab. It was captured over CDP because the tab is starved and can't load its fonts, so the font is a fallback.

Starved tab opens Active Sessions with the connection-limit warning

🤖 Generated with Claude Code

…er limit (#3107)

A browser shares six HTTP/1.1 connections per host across every tab of
the origin, and each tab held up to three SSE responses open
(/api/servers/events, /api/events/webhook/deliveries, /api/mcp/events).
Two connected tabs therefore exhausted the pool, and the second tab's
initialize queued in the browser until the client timed out.

- GET /api/tab/stream: one stream per tab carrying the mcp.json change
  signal, webhook deliveries and every attached session's events;
  POST /api/tab/attach routes a session onto it (same detach-grace reap,
  crash-on-startup and terminate semantics as /api/mcp/events).
- TabEventChannel (core, browser-safe) is the browser end; the remote
  transport, useServers and useWebhookDeliveries share it.
- The channel times its hello and a GET /api/tab/ping probe; past 5s the
  tab is starved and opens Active Sessions with a warning, which clears
  when the request completes. A tab re-probes when it becomes visible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Two critical lifecycle leaks remain: disconnecting during the hello write can retain the handler and subscriptions, while normal session disconnects leave attachments retaining sessions and transports in an open tab.

2 open issues
What changed in this PR

This PR addresses #3107 by consolidating the web client’s event streams into one connection per tab, reducing pressure on the browser’s HTTP/1.1 connection limit.

Changes:

  • Adds a shared tab stream for server-list changes, webhook deliveries, and MCP session events while preserving standalone routes.
  • Detects delayed backend requests and opens Active Sessions with a connection-limit warning.
  • Adds channel, transport, hook, UI, and integration coverage, plus documentation.
File Description
core/​react/​useServers.ts Supports a caller-provided change subscription.
core/​mcp/​remote/​tabStream.ts Defines tab-stream routes and event types.
core/​mcp/​remote/​tabEventChannel.ts Manages shared events, retries, and starvation detection.
core/​mcp/​remote/​remoteClientTransport.ts Routes session events through the shared channel.
core/​mcp/​remote/​node/​server.ts Adds tab stream, attachment, and probe endpoints.
core/​mcp/​remote/​index.ts Exports the channel API.
core/​mcp/​remote/​createRemoteTransport.ts Passes the channel into remote transports.
clients/​web/​src/​utils/​toasts/​toastIds.ts Removes the dedicated webhook-stream toast ID.
clients/​web/​src/​utils/​toasts/​toastIds.test.ts Updates toast-ID assertions.
clients/​web/​src/​test/​integration/​mcp/​remote/​tab-stream.test.ts Covers stream routing and session lifecycle integration.
clients/​web/​src/​test/​core/​react/​useServers.test.tsx Tests caller-provided change subscriptions.
clients/​web/​src/​test/​core/​mcp/​remote/​tabEventChannel.test.ts Tests routing, retries, and starvation detection.
clients/​web/​src/​test/​core/​mcp/​remote/​remoteClientTransport.tabChannel.test.ts Tests transport integration with the channel.
clients/​web/​src/​lib/​webhookReceiver.ts Replaces standalone streaming with frame parsing.
clients/​web/​src/​lib/​webhookReceiver.test.ts Tests webhook frame parsing.
clients/​web/​src/​lib/​tabEventChannel.ts Creates the page-scoped channel and visibility probes.
clients/​web/​src/​lib/​tabEventChannel.test.ts Tests singleton creation and visibility probing.
clients/​web/​src/​lib/​environmentFactory.ts Supplies the page channel to transports.
clients/​web/​src/​lib/​environmentFactory.test.ts Mocks the channel for environment tests.
clients/​web/​src/​hooks/​useWebhookDeliveries.ts Routes webhook frames from the shared stream.
clients/​web/​src/​hooks/​useWebhookDeliveries.test.tsx Tests webhook routing and unsubscribe behavior.
clients/​web/​src/​hooks/​useTabStarved.ts Exposes starvation state to React.
clients/​web/​src/​hooks/​useTabStarved.test.tsx Tests starvation-state updates.
clients/​web/​src/​components/​groups/​ActiveSessionsModal/​ActiveSessionsModal.tsx Displays the connection-limit warning.
clients/​web/​src/​components/​groups/​ActiveSessionsModal/​ActiveSessionsModal.test.tsx Tests warning visibility.
clients/​web/​src/​components/​groups/​ActiveSessionsModal/​ActiveSessionsModal.stories.tsx Adds a connection-limit story.
clients/​web/​src/​App.tsx Wires shared subscriptions and automatic warning display.
clients/​web/​README.md Documents shared streams and starvation behavior.

Review effort: Balanced

Comment thread core/mcp/remote/node/server.ts Resolved
Comment thread core/mcp/remote/node/server.ts Resolved
- Send the tab stream's hello as primeAndHoldSseStream's priming write,
  so a disconnect during it still runs the tab's cleanup.
- Mark a session ended on /api/mcp/disconnect, which releases its route
  on an open tab instead of keeping it until the tab closes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1, addressed in 0208f62:

  • Abort during the hello write leaks subscriptions: fixed. The hello is now primeAndHoldSseStream's priming write, so it goes out only after the abort cleanup is registered. A regression test is added.
  • Disconnect leaves stale attachments on open tabs: fixed. /api/mcp/disconnect now calls session.terminate(), which releases the route and sends session_end. A regression test covers repeated cycles on one open tab.

The local gate passes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Detaching removes only the browser-side handler, leaving the old backend session attached so its transport and stdio child can remain alive until the tab closes.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread core/mcp/remote/tabEventChannel.ts Resolved
Detaching a session from the tab channel now tells the backend
(POST /api/tab/detach), which takes it off the tab's stream and arms its
detach grace, as aborting its own /api/mcp/events stream used to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 2, addressed in 2ebb7da:

  • Session switch leaves the old backend transport attached: fixed. A channel detach now posts /api/tab/detach, which removes the session's route on an open tab and arms its detach grace. Regression tests are added.

The local gate passes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A rapid stop/start can let stale reader cleanup clear the restarted channel’s state and open an extra untracked stream, violating the one-connection guarantee.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

High severity Prevent stale reader cleanup from disrupting a restarted channel

core/​mcp/​remote/​tabEventChannel.ts:309

If stop() is followed immediately by start(), the old reader's asynchronous cleanup still clears the restarted channel's tab ID and session handlers, then schedules an extra stream. This can leave two streams open while only one is tracked by this.abort, defeating the one-connection guarantee. Make cleanup generation-aware, settle the stopped generation's handlers and waiters before restarting, and add a rapid stop/start regression test.

1 resolved since last review

Review effort: Balanced

A stream's end only touches the channel when no newer stream has opened
since, so a quick stop()/start() cannot let the old stream clear the new
one's tab id and sessions or queue a second stream beside it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 3, addressed in c24d722. The finding was in the review's previously-missed section, so it has no thread to reply in:

  • Stale reader cleanup can disrupt a restarted channel: fixed. Each open() bumps a generation counter, and a stream's end touches the channel only if no newer stream has opened since. A regression test ("ignores the end of a stream a restart replaced") fails without the fix and passes with it. In the app, only the test reset calls stop(), but the race was real.

The local gate passes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to evaluate lifecycle edge cases that can leak tab connections and backend sessions during hot reloads or lost attach responses.

No open issues 🎉

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed. Round 4 had no inline comments and no suppressed findings. Its headline asks for a human look at two lifecycle edge cases without naming a defect:

  • Dev hot reload: a hot reload of src/lib/tabEventChannel.ts can leave the old module's stream open until the page reloads.
  • Lost attach response: if the response to an attach is lost, the session stays attached on the backend until the tab closes, and is reaped after that.

Neither has been reproduced, and both are left to the reviewer. Three earlier rounds fixed a real defect each (rounds 1–3).

@cliffhall
cliffhall merged commit 821a56b into v2/main Dec 1, 2026
12 checks passed
@cliffhall
cliffhall deleted the v2/fix/3107-tab-connection-limit branch December 1, 2026 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web: a second tab on the same host can't connect — per-tab SSE streams exhaust the browser's 6-connection limit

2 participants