Skip to content

Approve no longer hangs when a review is open in two tabs - #4302

Merged
backnotprop merged 7 commits into
mainfrom
fix/review-approve-two-tabs
Dec 1, 2026
Merged

backnotprop merged 7 commits into
mainfrom
fix/review-approve-two-tabs

Conversation

@backnotprop

@backnotprop backnotprop commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Fixes #4218

Before: with a plan, annotate or code review open in two tabs of one browser, pressing Approve (or Send Feedback) in one of them spun forever: buttons disabled, no error, nothing written, the agent waiting. Each tab held its own event streams (annotate: external annotations, the file watch, the client lease), one HTTP/1.1 connection each, and two tabs used all six connections Chromium allows per host, so the POST sat in the browser's queue. Closing the other tab let it through.

After: tabs share one connection per stream. SharedEventSource (packages/ui/utils/sharedEventSource.ts) replaces new EventSource(url) for every review stream: the tabs that open the same URL elect a leader with a Web Lock; the leader holds the connection and passes frames and errors on over a BroadcastChannel. A tab that starts listening later asks the leader to reconnect, so it gets the stream's first frames (snapshot, ready). When the leader closes, the next tab takes the stream. No server change: the servers still see a connection while any tab is open, which is all the client lease and the agent-gone check ask.

Fallback: each tab opens its own stream, as before, where Web Locks or BroadcastChannel is missing (a remote session over plain http), where the browser refuses the lock request (for example a site that may not store data), or where the channel cannot be made. Without this, a refused lock left the tab with no stream and no error: no polling fallback, and an annotate --gate session whose only tab closed never expired.

A suspended leader (the stand-in): a leader tab the browser freezes (background tabs on iPad or Android, for example) keeps its lock and sends nothing. Followers ping it over the channel every 5 s; a running leader answers, even hidden. After 15 s without an answer, one follower takes a second lock (ifAvailable, never steal), opens its own stream and relays it; when the leader is heard again it steps down and asks for fresh first frames. A role granted after the leader was heard is let go at once. If the stand-in is suspended too (30 s of silence with the role refused), a tab opens its own stream and relays it to no one until a relayer is heard. Each relayed connection has an id announced before its frames, and tabs take frames of the connection they follow only, so the frames a woken leader had queued are not applied twice (no doubled agent logs, no deleted note coming back). A tab holds at most one connection per URL. While a stand-in relays beside a frozen leader whose connections stay open, the connection count is the leader's plus one set, so a decision can again wait for the 30 s deadline below.

Decision POSTs also get a 30 s deadline, so a send that never gets through ends with the existing "not sent" toast and Retry instead of spinning. No new words.

Measured (annotate --gate with the client lease, built from source, sandboxed, headless Chromium, two pages in one context): on main, tab 1 opens 3 streams, tab 2 three more; Approve in tab 2 was still pending after 11.5 s and went through only when tab 1 closed. With this branch tab 2 opens no stream and Approve answers 200 in about 100 ms. Tab 2 shows an external note posted before it opened, one posted while both are open, and one posted after tab 1 closed, each exactly once; after tab 1 closes, tab 2 opens the three streams itself.

Tests:

  • packages/ui/utils/sharedEventSource.test.ts: one connection for two tabs, named events, a late tab gets the first frames, the lease does not reconnect the leader, handover, errors, no sharing across URLs, no locks; a refused lock and a channel that throws (own stream, frames and errors arrive); a hidden leader that answers keeps one connection; a frozen leader (one stand-in relays to the others and steps down when the leader wakes); a woken leader's queued frame applied once; a stand-in suspended too (own stream after 30 s, back to following when the leader wakes); a stand-in role granted late is let go; a stand-in that becomes the leader keeps one connection.
  • packages/ui/utils/sendFailure.timeout.test.ts.
  • tests/e2e/annotate-two-tabs-approve.spec.ts: (1) two tabs, Approve in the second completes, the second opens no stream of its own; (2) the second tab gets each external note exactly once, posted before it opened, while both are open, and after the first tab closed, and takes the streams over. The browser shards skip draft PRs, so the spec has not run in CI yet.

Not proven: Safari and Firefox; a real frozen tab (the stand-in is covered by unit tests with fake timers and a fake frozen tab).

…goes through (#4218)

Each plan, annotate and code review tab held its own EventSource streams
(three in annotate: external annotations, the file watch, the client lease),
one HTTP/1.1 connection each. Two tabs of one review used all six connections
Chromium allows per host, so the Approve POST waited in the browser's queue
forever with the buttons disabled.

SharedEventSource (packages/ui/utils) is a drop-in for new EventSource(url):
tabs that open the same URL elect a leader with a Web Lock, the leader holds
the one connection and passes frames and errors on over a BroadcastChannel.
A tab that starts listening later asks the leader to reconnect so it gets the
stream's first frames. Without Web Locks each tab opens its own, as before.

Decision POSTs also carry a 30 s signal: one that is never answered ends as
unreachable and shows the existing not-sent toast with Retry.
…fused; stand in for a frozen leader

- A rejected lock request (or a throwing request or BroadcastChannel) left the
  tab with no stream and no error, so no polling fallback and no client lease
  (an annotate --gate session whose only tab closed never expired). The tab
  now opens its own stream.
- A frozen leader stalled every follower. Followers ping over the channel
  every 5 s; after 15 s unanswered one follower (an ifAvailable second lock,
  never steal) relays its own stream until the leader answers, then steps
  down with a join. At most one connection per URL per tab.
- e2e: the second tab gets an agent's note posted before it opened, while both
  are open, and after the first tab closed.
- HANDOFF: the default streams share one connection per URL across tabs.
…over when the stand-in sleeps too

- Each relayed connection has an id, announced before its frames; followers
  take frames of the connection they last heard announced only, so a frozen
  leader's queued frames (already relayed by the stand-in) are dropped.
- A stand-in role granted after a relayer was heard is let go at once.
- With no answer for 30 s and the stand-in role refused (the stand-in is
  suspended too), a tab opens its own stream and relays it to no one until a
  relayer is heard again.
- e2e counts each note once in the second tab.
@backnotprop
backnotprop marked this pull request as ready for review November 30, 2026 20:39
@backnotprop backnotprop added the ci:e2e Run the full browser suite (Inbox window e2e) on this PR label Nov 30, 2026
@backnotprop
backnotprop merged commit 681d48d into main Dec 1, 2026
87 of 127 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:e2e Run the full browser suite (Inbox window e2e) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Approve hangs forever when the review is open in two tabs (HTTP/1.1 connection limit exhausted by EventSource streams)

1 participant