Skip to content

Commit 4fc7657

Browse files
Fix color scheme races on webview swap and restore
Re-read the tab's current color scheme after the control session attaches instead of trusting the caller's snapshot, and re-read the webContentsId after recording a scheme change so the override lands on a guest swapped in mid-flight. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 1f5aff3 commit 4fc7657

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13901390
wc.getZoomFactor(),
13911391
);
13921392
yield* attachListeners(tabId, wc);
1393-
runFork(restoreControlSession(tabId, wc, tab.colorScheme));
1393+
runFork(restoreControlSession(tabId, wc));
13941394
const registeredAt = yield* currentIso;
13951395
const registration = yield* SynchronizedRef.modify(tabsRef, (tabs) => {
13961396
const current = tabs.get(tabId);
@@ -1530,14 +1530,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
15301530
yield* detachControlSession(wc.id);
15311531
yield* attempt({ operation: "openDevTools", tabId, webContentsId: wc.id }, () => {
15321532
wc.once("devtools-closed", () => {
1533-
if (wc.isDestroyed()) return;
1534-
runFork(
1535-
SynchronizedRef.get(tabsRef).pipe(
1536-
Effect.flatMap((tabs) =>
1537-
restoreControlSession(tabId, wc, tabs.get(tabId)?.colorScheme ?? "system"),
1538-
),
1539-
),
1540-
);
1533+
if (!wc.isDestroyed()) runFork(restoreControlSession(tabId, wc));
15411534
});
15421535
wc.openDevTools({ mode: "detach" });
15431536
});
@@ -1719,16 +1712,18 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17191712
});
17201713

17211714
// Re-establish the control session after a detach, restoring any
1722-
// color-scheme override the tab carries.
1723-
const restoreControlSession = (
1724-
tabId: string,
1725-
wc: Electron.WebContents,
1726-
colorScheme: DesktopPreviewColorScheme,
1727-
) =>
1728-
(colorScheme === "system"
1729-
? ensureControlSession(wc)
1730-
: applyColorScheme(tabId, wc, colorScheme)
1731-
).pipe(Effect.ignore);
1715+
// color-scheme override the tab carries. The scheme is read after the
1716+
// session attaches so a concurrent setColorScheme is not overwritten with
1717+
// a stale snapshot.
1718+
const restoreControlSession = (tabId: string, wc: Electron.WebContents) =>
1719+
ensureControlSession(wc).pipe(
1720+
Effect.andThen(SynchronizedRef.get(tabsRef)),
1721+
Effect.flatMap((tabs) => {
1722+
const colorScheme = tabs.get(tabId)?.colorScheme ?? "system";
1723+
return colorScheme === "system" ? Effect.void : applyColorScheme(tabId, wc, colorScheme);
1724+
}),
1725+
Effect.ignore,
1726+
);
17321727

17331728
const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
17341729
tabId: string,
@@ -1744,8 +1739,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17441739
// next control-session (re)attach.
17451740
yield* update(tabId, { colorScheme });
17461741
}
1747-
if (tab.webContentsId == null) return;
1748-
const wc = webContents.fromId(tab.webContentsId);
1742+
// Re-read after the update: registerWebview may have swapped the guest
1743+
// in the meantime and the override must land on the current one.
1744+
const webContentsId = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.webContentsId;
1745+
if (webContentsId == null) return;
1746+
const wc = webContents.fromId(webContentsId);
17491747
if (!wc || wc.isDestroyed()) return;
17501748
yield* applyColorScheme(tabId, wc, colorScheme);
17511749
});

0 commit comments

Comments
 (0)