fix(preview): server browser tabs nobody watches stop rendering #21902
Closedspencermorris04(GitHub profile, opens in a new tab)wants to merge 7 commits into
main fromrefs/pull/21902/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/preview/ServerBrowser.ts line 1382Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1213,0 +1224,21 @@1224+ // renders on the CPU here. Hidden like a background tab, it stops painting.1225+ // The desktop renders its own pages, so only headless tabs hide.1226+ const watched = (tab: ServerTab) =>1227+ tab.viewers.size > 0 ||1228+ tab.operations > 0 ||1229+ tab.recording !== null ||1230+ tab.recordingStart !== null;1231+1232+ const hideWhenUnwatched = (tab: ServerTab) => {1233+ if (tab.hideTimer !== null) clearTimeout(tab.hideTimer);1234+ tab.hideTimer = null;1235+ if (tab.desktop || tab.closing || tab.hidden || watched(tab)) return;1236+ tab.hideTimer = setTimeout(() => {1237+ tab.hideTimer = null;1238+ if (tab.closing || watched(tab)) return;1239+ tab.hidden = true;1240+ void setPageHidden(tab.cdp, true).catch(constVoid);1241+ }, UNWATCHED_TAB_HIDE_MS);1242+ };1243+1244+ const show = async (tab: ServerTab) => {
- apps/server/src/preview/ServerBrowser.ts lines 1244 to 1245OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1212,2 +1225,21 @@12121225 };121312261227+ // A headless page paints every frame even when nobody sees it, and WebGL1228+ // renders on the CPU here. Hidden like a background tab, it stops painting.1229+ // The desktop renders its own pages, so only headless tabs hide.1230+ const watched = (tab: ServerTab) =>1231+ tab.viewers.size > 0 ||1232+ tab.operations > 0 ||1233+ tab.recording !== null ||1234+ tab.recordingStart !== null;1235+1236+ // `hidden` is the desired state, set synchronously. The window itself is1237+ // moved off a per-tab serialized chain, so concurrent show/hide callers can't1238+ // interleave Browser.setWindowBounds, and a caller that finds a restore1239+ // already in flight waits for it instead of racing an interaction ahead of it.1240+ const syncVisibility = (tab: ServerTab): Promise<void> => {1241+ const next = (tab.visibilitySync ?? Promise.resolve()).then(async () => {1242+ if (tab.closing || tab.appliedHidden === tab.hidden) return;1243+ const target = tab.hidden;1244+ await setPageHidden(tab.cdp, target).catch(constVoid);1245+ tab.appliedHidden = target;apps/server/src/preview/ServerBrowser.ts lines 1266 to 1268OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1213,0 +1248,21 @@1248+ return next;1249+ };1250+1251+ const hideWhenUnwatched = (tab: ServerTab) => {1252+ if (tab.hideTimer !== null) clearTimeout(tab.hideTimer);1253+ tab.hideTimer = null;1254+ if (tab.desktop || tab.closing || tab.hidden || watched(tab)) return;1255+ tab.hideTimer = setTimeout(() => {1256+ tab.hideTimer = null;1257+ if (tab.closing || watched(tab)) return;1258+ tab.hidden = true;1259+ void syncVisibility(tab);1260+ }, UNWATCHED_TAB_HIDE_MS);1261+ };1262+1263+ const show = async (tab: ServerTab) => {1264+ if (tab.hideTimer !== null) clearTimeout(tab.hideTimer);1265+ tab.hideTimer = null;1266+ if (!tab.hidden && tab.appliedHidden === false) return;1267+ tab.hidden = false;1268+ await syncVisibility(tab);
- @@ -1213,0 +1230,21 @@1230+ const watched = (tab: ServerTab) =>1231+ tab.viewers.size > 0 ||1232+ tab.operations > 0 ||1233+ tab.recording !== null ||1234+ tab.recordingStart !== null;1235+1236+ // `hidden` is the desired state, set synchronously by show/hide. Each call1237+ // appends "drive the window to the desired state" onto a per-tab chain and1238+ // returns its tail, so callers serialize behind any change already in flight1239+ // instead of racing Browser.setWindowBounds or an interaction ahead of it.1240+ // `appliedHidden` is what actually reached the window; it only advances on1241+ // success, so a failed change is left for the next call to retry.1242+ const driveVisibility = async (tab: ServerTab) => {1243+ if (tab.closing || tab.appliedHidden === tab.hidden) return;1244+ const target = tab.hidden;1245+ try {1246+ await setPageHidden(tab.cdp, target);1247+ tab.appliedHidden = target;1248+ } catch {1249+ // Leave appliedHidden unchanged; the next show/hide retries the change.1250+ }
- apps/server/src/preview/ServerBrowser.ts line 2598OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2526,7 +2590,9 @@25262590 Effect.promise(async () => {25272591 await tab.control.disconnect(viewer.id, () => releaseViewerInput(viewer, session));25282592 tab.viewers.delete(viewer);2593+ hideWhenUnwatched(tab);25292594 broadcastControl(tab);25302595 reportLiveTabs();25312596 }),25322597 );2598+ yield* Effect.promise(() => show(tab));
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-rebase
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.




Findings#
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
be722848-8458-4a6d-b1a0-adc7274e62f2Reviewing files that changed from the base of the PR and between ca2152a3f8c35627de5cef872705e0be6bda27c8 and 365934975c7d5f480c9498c8cb9b8b3fd825d137.
pnpm-lock.yamlis excluded by!**/pnpm-lock.yamlapps/server/src/preview/ServerBrowser.test.tsapps/server/src/preview/ServerBrowser.tsapps/server/src/preview/ServerBrowserContexts.test.tsapps/server/src/preview/ServerBrowserContexts.tspnpm-workspace.yamlLimit details: You’ve used all 10 included reviews currently available.
Walkthrough
ServerBrowser hides unwatched headless tabs after 60 seconds and restores them for viewers and agent operations. Chromium visibility changes are serialized. A Playwright patch disables focus emulation for server browser launches. Tests cover unit and real-browser visibility behavior.
Changes: Headless tab visibility
patches/playwright-core@1.60.0.patch,pnpm-workspace.yaml,apps/server/src/preview/ServerBrowserContexts.ts,apps/server/src/preview/ServerBrowserContexts.test.tsfocusEmulationand prevents focus emulation when it is false. Server browser launch options disable it.setPageHiddenminimizes or restores a page through CDP. Real-browser tests cover painting, focus, and updates made while hidden.apps/server/src/preview/ServerBrowser.ts,apps/server/src/preview/ServerBrowser.test.tsEstimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix
Sequence Diagram(s)#
Possibly related PRs
Suggested labels:
bugfixSuggested reviewers:
juliusmarminge,maria-rcks,t3dotggMerge Risk:
Low · previous review
36593A failed viewer attachment is not reported through its declared error channel, and the desktop visibility test may miss a regression. These are bounded issues to address or explicitly accept before merging.
Security architecture risk:
Low · up to
e4e8dThe change does not demonstrably grant new access or weaken browser ownership controls. Remaining uncertainty concerns visibility recovery and whether window-level hiding can affect neighboring tabs.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
Pre-merge checks:
4
#18165, a detailed Change section, Verification with specific tests, CPU measurements, and a che…Full details: Title check
Explanation
The title names the main change: server browser tabs that nobody watches stop rendering, which matches the changeset's hide-after-60s-idle behavior and the setPageHidden and focusEmulation patch. It uses the conventional commit form with a fix(preview) scope and is specific enough for a teammate scanning history.
Full details: Description check
Explanation
The description follows the repository template closely. It has a Problem-like opening that links issue
#18165, a detailed Change section, Verification with specific tests, CPU measurements, and a checklist. The Scope and approval section is not filled in. The description links issue#18165but does not state the triaged or maintainer-approved scope, and it does not explain why the change qualifies without prior approval. The template requires this. The Problem section is also not labelled, and the description does not state expected behavior in a separate Problem section. Overall the description is mostly complete, so the check passes, but the missing approval information should be added.Finishing Touches
Generate unit tests (beta)