History

[codex] Fix desktop black screen fallback (#1051)

* Fix desktop black screen fallback * Add desktop black screen changeset

Rhys Sullivan committed 5 months ago

Commit cfda0ac1 parent af96bae6 files · +355 −18Browse files

Showing the changes to apps (3 of 6 files). Show all

Unified · Split

3 files
  1. apps/desktop/src/main/index.ts +83 −17
  2. apps/desktop/src/main/supervised-connection.test.ts +79 −0
  3. apps/desktop/src/main/supervised-connection.ts +22 −0
apps/desktop/src/main/index.ts+83−17
@@ -35,6 +35,7 @@ import { · 34 unmodified lines
3535 reportAProblem,
3636 } from "./diagnostics";
3737 import { sidecarCrashHtml } from "./crash-screen";
38+import { replaceSupervisedDaemonForDesktop } from "./supervised-connection";
3839 import {
3940 bundledExecutorPath,
4041 installSupervisedService,
@@ -88,6 +89,11 @@ const liveMainWindow = (): BrowserWindow | null => { · 47 unmodified lines
8889 return window;
8990 };
9091
92+const destroyWindow = (window: BrowserWindow) => {
93+ if (mainWindow === window) mainWindow = null;
94+ if (!window.isDestroyed()) window.destroy();
95+};
96+
9197 const focusMainWindow = () => {
9298 const window = liveMainWindow();
9399 if (!window) {
@@ -208,17 +214,21 @@ const ensureSupervisedConnection = async (): Promise<SidecarConnection | null> = · 114 unmodified lines
208214 if (attached) {
209215 if (!shouldReplaceDaemonForDesktop(attached)) return attached;
210216 const settings = getServerSettings();
211− // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: desktop launch should attach to the running daemon if automatic upgrade fails
212− try {
213− await installSupervisedService({
214− port: settings.port,
215− dataDir: DESKTOP_DATA_DIR,
216− });
217− return (await waitForSupervisedAttach(30_000, { port: settings.port })) ?? attached;
218− } catch (error) {
219− log.warn("Failed to replace older supervised daemon; attaching to the running daemon", error);
220− return attached;
221− }
217+ return replaceSupervisedDaemonForDesktop(attached, {
218+ install: () =>
219+ installSupervisedService({
220+ port: settings.port,
221+ dataDir: DESKTOP_DATA_DIR,
222+ }),
223+ waitForAttach: () => waitForSupervisedAttach(30_000, { port: settings.port }),
224+ attach: attachToSupervisedDaemon,
225+ onInstallFailure: (error) => {
226+ log.warn(
227+ "Failed to replace older supervised daemon; re-checking before falling back",
228+ error,
229+ );
230+ },
231+ });
222232 }
223233
224234 const status = await supervisedServiceStatus();
@@ -452,7 +462,18 @@ const createWindow = async (conn: SidecarConnection) => { · 227 unmodified lines
452462 return { action: "deny" };
453463 });
454464
455− await window.loadURL(webUrlForConnection(conn));
465+ // A supervised daemon can pass the health probe and still disappear before
466+ // navigation begins. Treat that as a failed connection instead of leaving the
467+ // user with a visible BrowserWindow that only shows the black background.
468+ // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: Electron navigation rejects when the sidecar vanishes during startup
469+ try {
470+ await window.loadURL(webUrlForConnection(conn));
471+ } catch (error) {
472+ log.error("Failed to load Executor web UI", error);
473+ destroyWindow(window);
474+ // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: caller decides whether to fall back or surface startup failure
475+ throw error;
476+ }
456477 };
457478
458479 const showPortInUseDialog = async (port: number) => {
@@ -852,10 +873,18 @@ const boot = async () => { · 393 unmodified lines
852873 const supervised = await ensureSupervisedConnection();
853874 if (supervised) {
854875 connection = supervised;
855− await createWindow(supervised); // installs the bearer-auth header itself
856− armSupervisedMonitor();
857− void runUpdateCheck({ alertOnFail: false });
858− return;
876+ // createWindow installs the bearer-auth header itself.
877+ // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: supervised attach can race with daemon shutdown; fall back to managed spawn
878+ try {
879+ await createWindow(supervised);
880+ armSupervisedMonitor();
881+ void runUpdateCheck({ alertOnFail: false });
882+ return;
883+ } catch (error) {
884+ log.warn("Failed to load supervised daemon; falling back to managed sidecar", error);
885+ stopSupervisedMonitor();
886+ connection = null;
887+ }
859888 }
860889 }
861890 connection = await startWithCurrentSettings();
@@ -878,7 +907,44 @@ const boot = async () => { · 16 unmodified lines
878907 app.quit();
879908 return;
880909 }
881− await createWindow(connection);
910+ // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: renderer navigation failure must show the startup recovery dialog, not a black window
911+ try {
912+ await createWindow(connection);
913+ } catch (error) {
914+ log.error("Failed to load managed sidecar web UI", error);
915+ const failedConnection = connection;
916+ connection = null;
917+ await stopConnection(failedConnection);
918+
919+ const retryAfterReset = await handleFatalSidecarFailure(error);
920+ if (!retryAfterReset) {
921+ app.quit();
922+ return;
923+ }
924+
925+ lastSidecarStartError = null;
926+ connection = await startWithCurrentSettings();
927+ if (!connection && lastSidecarStartError != null) {
928+ await handleFatalSidecarFailure(lastSidecarStartError);
929+ }
930+ if (!connection) {
931+ app.quit();
932+ return;
933+ }
934+
935+ // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: one post-reset retry before giving the user the final startup failure
936+ try {
937+ await createWindow(connection);
938+ } catch (retryError) {
939+ log.error("Failed to load managed sidecar web UI after reset", retryError);
940+ const failedRetryConnection = connection;
941+ connection = null;
942+ await stopConnection(failedRetryConnection);
943+ await handleFatalSidecarFailure(retryError);
944+ app.quit();
945+ return;
946+ }
947+ }
882948 // Check at boot. If an update is available, autoDownload pulls it and
883949 // the 'update-downloaded' handler fires the install dialog. Silent on
884950 // no-update / failure so we don't bother users on every launch.
apps/desktop/src/main/supervised-connection.test.ts+79−0
@@ -0,0 +1,79 @@
1+import { describe, expect, it } from "@effect/vitest";
2+import { Effect } from "effect";
3+import type { SidecarConnection } from "./sidecar";
4+import { replaceSupervisedDaemonForDesktop } from "./supervised-connection";
5+
6+const connection = (baseUrl: string): SidecarConnection => ({
7+ baseUrl,
8+ hostname: "localhost",
9+ port: 4789,
10+ username: "executor",
11+ authToken: "token",
12+ child: null,
13+ supervisedDaemon: true,
14+ ownerVersion: "1.5.12",
15+ ownerClient: "cli",
16+ ownerExecutablePath: "/tmp/old-executor",
17+});
18+
19+const rejectedInstall = (error: unknown): Promise<void> => Effect.runPromise(Effect.fail(error));
20+
21+describe("replaceSupervisedDaemonForDesktop", () => {
22+ it("uses the replacement daemon after install succeeds", async () => {
23+ const stale = connection("http://localhost:4789");
24+ const fresh = connection("http://localhost:55334");
25+ let installCalls = 0;
26+ let waitCalls = 0;
27+ let attachCalls = 0;
28+
29+ const result = await replaceSupervisedDaemonForDesktop(stale, {
30+ install: async () => {
31+ installCalls += 1;
32+ },
33+ waitForAttach: async () => {
34+ waitCalls += 1;
35+ return fresh;
36+ },
37+ attach: async () => {
38+ attachCalls += 1;
39+ return stale;
40+ },
41+ });
42+
43+ expect(result).toBe(fresh);
44+ expect(installCalls).toBe(1);
45+ expect(waitCalls).toBe(1);
46+ expect(attachCalls).toBe(0);
47+ });
48+
49+ it("re-probes after install fails instead of returning the stale daemon", async () => {
50+ const stale = connection("http://localhost:4789");
51+ const installFailure = { _tag: "InstallFailure" };
52+ let loggedError: unknown;
53+ let loggedConnection: SidecarConnection | null = null;
54+ let waitCalls = 0;
55+ let attachCalls = 0;
56+
57+ const result = await replaceSupervisedDaemonForDesktop(stale, {
58+ install: () => rejectedInstall(installFailure),
59+ waitForAttach: async () => {
60+ waitCalls += 1;
61+ return stale;
62+ },
63+ attach: async () => {
64+ attachCalls += 1;
65+ return null;
66+ },
67+ onInstallFailure: (error, connection) => {
68+ loggedError = error;
69+ loggedConnection = connection;
70+ },
71+ });
72+
73+ expect(result).toBeNull();
74+ expect(waitCalls).toBe(0);
75+ expect(attachCalls).toBe(1);
76+ expect(loggedError).toBe(installFailure);
77+ expect(loggedConnection).toBe(stale);
78+ });
79+});
apps/desktop/src/main/supervised-connection.ts+22−0
@@ -0,0 +1,22 @@
1+/* oxlint-disable executor/no-try-catch-or-throw -- boundary: Electron startup must convert service-install failures into a fresh daemon probe */
2+import type { SidecarConnection } from "./sidecar";
3+
4+export interface ReplaceSupervisedDaemonInput {
5+ readonly install: () => Promise<void>;
6+ readonly waitForAttach: () => Promise<SidecarConnection | null>;
7+ readonly attach: () => Promise<SidecarConnection | null>;
8+ readonly onInstallFailure?: (error: unknown, staleConnection: SidecarConnection) => void;
9+}
10+
11+export const replaceSupervisedDaemonForDesktop = async (
12+ staleConnection: SidecarConnection,
13+ input: ReplaceSupervisedDaemonInput,
14+): Promise<SidecarConnection | null> => {
15+ try {
16+ await input.install();
17+ return (await input.waitForAttach()) ?? (await input.attach());
18+ } catch (error) {
19+ input.onInstallFailure?.(error, staleConnection);
20+ return await input.attach();
21+ }
22+};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t