-
Notifications
You must be signed in to change notification settings - Fork 990
fix(desktop): heal stale host-service adoption; enable tray Restart in stopped state #4395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+407
−10
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
697caff
fix(desktop): heal stale host-service adoption; enable tray Restart i…
Kitenite 8330df8
docs(desktop): mark PR1 shipped in host-service recovery plan, prep P…
Kitenite 4525732
fix(desktop): add in-app host-service reset + retry loop + recovery UI
Kitenite f9315a0
fix(desktop): route host-service-coordinator logs through electron-log
Kitenite 608b6ea
fix(desktop): drop local-host recovery screen; keep reset + auto-retry
Kitenite 4ec73dc
fix(desktop): address review feedback on host-service reset + tests
Kitenite a4991ad
refactor(desktop): trim host-service recovery PR to the load-bearing …
Kitenite a88be52
docs(desktop): replace host-service recovery plan with a short shippe…
Kitenite 359a8ca
test(desktop): fix stale 'no rename' in reset test title (wipeHostDb …
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Host-service recovery (#4299) — shipped | ||
|
|
||
| **Issue:** [superset-sh/superset#4299](https://github.com/superset-sh/superset/issues/4299) — after Cmd+R the v2 right pane goes blank because the renderer keeps getting handed a dead host-service port. | ||
|
|
||
| **Root cause:** `tryAdopt` only checked `isProcessAlive(pid)` + app-version. A live-but-not-serving host-service (hung on migrations, deadlocked, port no longer bound) got adopted as `running`, and `getConnection` returned its dead port forever — an absorbing state nothing climbed out of. | ||
|
|
||
| ## What shipped (PR #4395) | ||
|
|
||
| - **Adopt health-check** — `tryAdopt` now `pollHealthCheck`s the manifest endpoint (2s cap) before registering an adopted instance; on failure it SIGKILLs the stale pid, removes the manifest, and falls through to a clean `spawn`. This is the fix. | ||
| - **`coordinator.reset(orgId)`** + `hostServiceCoordinator.reset` tRPC mutation — force-kill (SIGKILL on whatever pid the manifest names, even if untracked) + remove manifest + respawn. No UI caller yet; intended for a support escape hatch / future Settings button. | ||
| - **Tray "Restart" enabled in `stopped`** — was gated on `isRunning`, i.e. disabled exactly when restart helps; now disabled only while a start is in flight. | ||
| - **Coordinator logs through `electron-log`** — adoption health-check failures now land in `main.log` (were bare `console.log`, invisible in packaged builds). `log.warn` on non-ESRCH SIGKILL failures. | ||
|
|
||
| ## Considered, not shipped | ||
|
|
||
| - **Full-screen "host stopped" recovery screen** in the v2-workspace layout — dropped. [#4430](https://github.com/superset-sh/superset/pull/4430) removed the analogous remote `WorkspaceHostOfflineState` ("render optimistically; downstream queries surface their own errors"); a local equivalent would swim against that. A non-blocking banner could be a future PR. | ||
| - **Renderer retry-with-backoff** in `LocalHostServiceProvider` — built, then dropped: heavier than the bug needs and invisible without the recovery screen. | ||
| - **`reset({ wipeHostDb })`** (archive `host.db` → `host.db.broken-<ts>`) + a Settings "Reset and clear local data" button — deferred until there's a caller. | ||
| - **The white-screen-before-Cmd+R variant** — tracked separately at [#4396](https://github.com/superset-sh/superset/issues/4396): `getHostId()` shells out to `ioreg` via `execFileSync` with no timeout, blocking the main event loop when subprocess spawning is sandboxed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
292 changes: 292 additions & 0 deletions
292
apps/desktop/src/main/lib/host-service-coordinator.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,292 @@ | ||
| import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| const APP_VERSION = "1.2.3"; | ||
|
|
||
| const manifestStore: { | ||
| current: { | ||
| pid: number; | ||
| endpoint: string; | ||
| authToken: string; | ||
| startedAt: number; | ||
| organizationId: string; | ||
| spawnedByAppVersion: string; | ||
| } | null; | ||
| } = { current: null }; | ||
|
|
||
| // Per-test temp dir backing the mocked `manifestDir`. A real path (not a | ||
| // fixed string) so tests stay isolated; assigned in beforeEach, removed in | ||
| // afterEach. | ||
| let testManifestRoot = ""; | ||
|
|
||
| const readManifestMock = mock(() => manifestStore.current); | ||
| const removeManifestMock = mock(() => { | ||
| manifestStore.current = null; | ||
| }); | ||
| const isProcessAliveMock = mock(() => true); | ||
|
|
||
| mock.module("./host-service-manifest", () => ({ | ||
| readManifest: readManifestMock, | ||
| removeManifest: removeManifestMock, | ||
| isProcessAlive: isProcessAliveMock, | ||
| listManifests: mock(() => []), | ||
| manifestDir: (orgId: string) => path.join(testManifestRoot, orgId), | ||
| })); | ||
|
|
||
| const pollHealthCheckMock = mock(() => Promise.resolve(true)); | ||
|
|
||
| mock.module("./host-service-utils", () => ({ | ||
| HEALTH_POLL_TIMEOUT_MS: 10_000, | ||
| MAX_HOST_LOG_BYTES: 1024, | ||
| findFreePort: mock(() => Promise.resolve(40000)), | ||
| openRotatingLogFd: mock(() => -1), | ||
| pollHealthCheck: pollHealthCheckMock, | ||
| })); | ||
|
|
||
| mock.module("electron", () => ({ | ||
| app: { | ||
| getVersion: () => APP_VERSION, | ||
| isPackaged: false, | ||
| getAppPath: () => "/tmp/app", | ||
| }, | ||
| })); | ||
|
|
||
| mock.module("electron-log/main", () => ({ | ||
| default: { | ||
| info: () => {}, | ||
| warn: () => {}, | ||
| error: () => {}, | ||
| }, | ||
| })); | ||
|
|
||
| mock.module("@superset/local-db", () => ({ settings: {} })); | ||
| mock.module("@superset/shared/host-info", () => ({ | ||
| getHostId: () => "host-1", | ||
| getHostName: () => "host", | ||
| })); | ||
| mock.module("main/env.main", () => ({ | ||
| env: { NEXT_PUBLIC_API_URL: "", RELAY_URL: "" }, | ||
| })); | ||
| mock.module("shared/env.shared", () => ({ | ||
| env: { DESKTOP_VITE_PORT: 3000, DESKTOP_NOTIFICATIONS_PORT: 4000 }, | ||
| })); | ||
| mock.module("./app-environment", () => ({ | ||
| SUPERSET_HOME_DIR: "/tmp/superset", | ||
| })); | ||
| mock.module("./local-db", () => ({ | ||
| localDb: { | ||
| select: () => ({ from: () => ({ get: () => null }) }), | ||
| }, | ||
| })); | ||
| mock.module("./terminal/env", () => ({ HOOK_PROTOCOL_VERSION: "1" })); | ||
| mock.module("../../lib/trpc/routers/workspaces/utils/shell-env", () => ({ | ||
| getProcessEnvWithShellPath: async (e: Record<string, string>) => e, | ||
| })); | ||
|
|
||
| const { HostServiceCoordinator } = await import("./host-service-coordinator"); | ||
|
|
||
| const baseManifest = (pid: number, endpoint = "http://127.0.0.1:55555") => ({ | ||
| pid, | ||
| endpoint, | ||
| authToken: "manifest-secret", | ||
| startedAt: 0, | ||
| organizationId: "org-1", | ||
| spawnedByAppVersion: APP_VERSION, | ||
| }); | ||
|
|
||
| const spawnConfig = { authToken: "token", cloudApiUrl: "https://api.example" }; | ||
|
|
||
| describe("HostServiceCoordinator.tryAdopt — adoption health check", () => { | ||
| let coordinator: InstanceType<typeof HostServiceCoordinator>; | ||
| let killedPids: Array<{ pid: number; signal: NodeJS.Signals | number }>; | ||
| let originalKill: typeof process.kill; | ||
| let spawnMock: ReturnType<typeof mock>; | ||
|
|
||
| beforeEach(() => { | ||
| manifestStore.current = null; | ||
| readManifestMock.mockClear(); | ||
| removeManifestMock.mockClear(); | ||
| isProcessAliveMock.mockClear(); | ||
| pollHealthCheckMock.mockClear(); | ||
|
|
||
| testManifestRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hsc-test-")); | ||
|
|
||
| killedPids = []; | ||
| originalKill = process.kill; | ||
| // `process.kill` is read-only in some Bun versions — assign via cast. | ||
| (process as unknown as { kill: typeof process.kill }).kill = (( | ||
| pid: number, | ||
| signal?: NodeJS.Signals | number, | ||
| ) => { | ||
| killedPids.push({ pid, signal: signal ?? "SIGTERM" }); | ||
| return true; | ||
| }) as typeof process.kill; | ||
|
|
||
| coordinator = new HostServiceCoordinator(); | ||
| // Replace spawn so a failed adoption doesn't actually launch electron. | ||
| spawnMock = mock(async () => ({ | ||
| port: 60000, | ||
| secret: "fresh-secret", | ||
| machineId: "host-1", | ||
| })); | ||
| (coordinator as unknown as { spawn: typeof spawnMock }).spawn = spawnMock; | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| afterEach(() => { | ||
| // Unconditional — if an assertion throws mid-test, the override must | ||
| // still be torn down or the next test captures the wrong `originalKill`. | ||
| (process as unknown as { kill: typeof process.kill }).kill = originalKill; | ||
| if (testManifestRoot) { | ||
| fs.rmSync(testManifestRoot, { recursive: true, force: true }); | ||
| testManifestRoot = ""; | ||
| } | ||
| }); | ||
|
|
||
| test("adopts when manifest is healthy", async () => { | ||
| manifestStore.current = baseManifest(1234); | ||
| pollHealthCheckMock.mockImplementationOnce(() => Promise.resolve(true)); | ||
|
|
||
| const conn = await coordinator.start("org-1", spawnConfig); | ||
|
|
||
| expect(conn.port).toBe(55555); | ||
| expect(conn.secret).toBe("manifest-secret"); | ||
| expect(pollHealthCheckMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).not.toHaveBeenCalled(); | ||
| expect(removeManifestMock).not.toHaveBeenCalled(); | ||
| expect(coordinator.getProcessStatus("org-1")).toBe("running"); | ||
| }); | ||
|
|
||
| test("kills the adopted pid with SIGKILL and falls through to spawn when health check fails", async () => { | ||
| manifestStore.current = baseManifest(4321); | ||
| pollHealthCheckMock.mockImplementationOnce(() => Promise.resolve(false)); | ||
|
|
||
| const conn = await coordinator.start("org-1", spawnConfig); | ||
|
|
||
| expect(pollHealthCheckMock).toHaveBeenCalledTimes(1); | ||
| expect(killedPids).toContainEqual({ pid: 4321, signal: "SIGKILL" }); | ||
| expect(removeManifestMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| expect(conn.secret).toBe("fresh-secret"); | ||
| }); | ||
|
|
||
| test("swallows SIGKILL ESRCH (pid already gone) and still respawns", async () => { | ||
| manifestStore.current = baseManifest(7777); | ||
| pollHealthCheckMock.mockImplementationOnce(() => Promise.resolve(false)); | ||
| (process as unknown as { kill: typeof process.kill }).kill = (() => { | ||
| const err: NodeJS.ErrnoException = new Error("kill ESRCH"); | ||
| err.code = "ESRCH"; | ||
| throw err; | ||
| }) as typeof process.kill; | ||
|
|
||
| const conn = await coordinator.start("org-1", spawnConfig); | ||
|
|
||
| expect(removeManifestMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| }); | ||
|
|
||
| test("kills with SIGTERM (existing behavior) on app-version mismatch, before health check", async () => { | ||
| manifestStore.current = { | ||
| ...baseManifest(5555), | ||
| spawnedByAppVersion: "0.9.0", | ||
| }; | ||
|
|
||
| const conn = await coordinator.start("org-1", spawnConfig); | ||
|
|
||
| // App-version gate runs before the new health check. | ||
| expect(pollHealthCheckMock).not.toHaveBeenCalled(); | ||
| expect(killedPids).toContainEqual({ pid: 5555, signal: "SIGTERM" }); | ||
| expect(removeManifestMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| }); | ||
| }); | ||
|
|
||
| describe("HostServiceCoordinator.reset", () => { | ||
| let coordinator: InstanceType<typeof HostServiceCoordinator>; | ||
| let killedPids: Array<{ pid: number; signal: NodeJS.Signals | number }>; | ||
| let originalKill: typeof process.kill; | ||
| let spawnMock: ReturnType<typeof mock>; | ||
|
|
||
| beforeEach(() => { | ||
| manifestStore.current = null; | ||
| readManifestMock.mockClear(); | ||
| removeManifestMock.mockClear(); | ||
| isProcessAliveMock.mockClear(); | ||
| pollHealthCheckMock.mockClear(); | ||
|
|
||
| testManifestRoot = fs.mkdtempSync(path.join(os.tmpdir(), "hsc-test-")); | ||
|
|
||
| killedPids = []; | ||
| originalKill = process.kill; | ||
| (process as unknown as { kill: typeof process.kill }).kill = (( | ||
| pid: number, | ||
| signal?: NodeJS.Signals | number, | ||
| ) => { | ||
| killedPids.push({ pid, signal: signal ?? "SIGTERM" }); | ||
| return true; | ||
| }) as typeof process.kill; | ||
|
|
||
| coordinator = new HostServiceCoordinator(); | ||
| spawnMock = mock(async () => ({ | ||
| port: 60000, | ||
| secret: "fresh-secret", | ||
| machineId: "host-1", | ||
| })); | ||
| (coordinator as unknown as { spawn: typeof spawnMock }).spawn = spawnMock; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| (process as unknown as { kill: typeof process.kill }).kill = originalKill; | ||
| if (testManifestRoot) { | ||
| fs.rmSync(testManifestRoot, { recursive: true, force: true }); | ||
| testManifestRoot = ""; | ||
| } | ||
| }); | ||
|
|
||
| test("removes manifest, SIGKILLs live pid, then spawns fresh", async () => { | ||
| manifestStore.current = baseManifest(8888); | ||
|
|
||
| const conn = await coordinator.reset("org-1", spawnConfig); | ||
|
|
||
| expect(killedPids).toContainEqual({ pid: 8888, signal: "SIGKILL" }); | ||
| expect(removeManifestMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| expect(conn.secret).toBe("fresh-secret"); | ||
| }); | ||
|
|
||
| test("SIGKILLs the manifest pid even when an instance is tracked (stop's SIGTERM may not be enough)", async () => { | ||
| // First adopt a healthy instance so it's tracked in `this.instances`. | ||
| manifestStore.current = baseManifest(2468); | ||
| pollHealthCheckMock.mockImplementationOnce(() => Promise.resolve(true)); | ||
| await coordinator.start("org-1", spawnConfig); | ||
| expect(coordinator.getProcessStatus("org-1")).toBe("running"); | ||
| killedPids.length = 0; | ||
|
|
||
| // Adoption leaves the manifest in place; reset must read its pid before | ||
| // stop() removes it, then escalate SIGTERM → SIGKILL on a wedged process. | ||
| const conn = await coordinator.reset("org-1", spawnConfig); | ||
|
|
||
| expect(killedPids).toContainEqual({ pid: 2468, signal: "SIGTERM" }); | ||
| expect(killedPids).toContainEqual({ pid: 2468, signal: "SIGKILL" }); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| }); | ||
|
|
||
| test("is safe when no manifest exists — no kill, still spawns", async () => { | ||
| manifestStore.current = null; | ||
|
|
||
| const conn = await coordinator.reset("org-1", spawnConfig); | ||
|
|
||
| expect(killedPids).toHaveLength(0); | ||
| // `removeManifest` is called unconditionally — that's fine, the impl | ||
| // in host-service-manifest treats a missing file as a no-op. | ||
| expect(removeManifestMock).toHaveBeenCalledTimes(1); | ||
| expect(spawnMock).toHaveBeenCalledTimes(1); | ||
| expect(conn.port).toBe(60000); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process.killrestore — useafterEachprocess.killis restored at the end of each test body. If any assertion fires before the restore line (e.g. test 3'sexpect(conn.port).toBe(60000)fails),process.killis left in its overridden state for the rest of the suite. The nextbeforeEachthen captures the wrong function asoriginalKill, so every subsequent test either accumulates into a stalekilledPidsarray or — worse — inherits the throwing mock from test 3. Moving the restore to a singleafterEach(() => { (process as any).kill = originalKill; })makes the cleanup unconditional.Prompt To Fix With AI