From 1b37750b0a65d7da64e12a5331632a9e18ae6cf9 Mon Sep 17 00:00:00 2001 From: bessim boujebli Date: Wed, 18 Nov 2026 13:27:56 +0100 Subject: [PATCH 1/3] fix(server): retire inherited device daemons before reuse Fixes #38588. A new local host has no session records for a surviving daemon. Retire that daemon before exposing automation, preserve retained registrations, and keep healthy reuse within the current server. --- .../server/src/device/LocalDeviceHost.test.ts | 78 +++++++++++++++---- apps/server/src/device/LocalDeviceHost.ts | 26 ++++++- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index b29b80a647837..498de74f2f4a9 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -52,6 +52,7 @@ const agentFixture = Effect.fn("agentFixture")(function* ( const daemonStopRequested = yield* Deferred.make>(); const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata")); const daemonHealthy = yield* Ref.make(true); + const daemonStop = { output: processOutput(0), removeRecord: true }; const runs: Array = []; for (const [name, version, entry] of [ ["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"], @@ -109,7 +110,18 @@ const agentFixture = Effect.fn("agentFixture")(function* ( return input.args.includes("devices") ? startup(stateDir) : input.args.includes("daemon") - ? Deferred.succeed(daemonStopRequested, input.args).pipe(Effect.as(processOutput(0))) + ? Deferred.succeed(daemonStopRequested, input.args).pipe( + Effect.andThen(() => + daemonStop.removeRecord && + daemonStop.output.code === 0 && + !daemonStop.output.timedOut + ? fs + .remove(path.join(stateDir, "daemon.json"), { force: true }) + .pipe(Effect.orDie) + : Effect.void, + ), + Effect.as(daemonStop.output), + ) : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)); }, }), @@ -130,27 +142,36 @@ const agentFixture = Effect.fn("agentFixture")(function* ( ), ); yield* host.ensureReady(() => Effect.void); - return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStopRequested, runs }; + return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStop, daemonStopRequested, runs }; }); describe("AgentDevice startup", () => { - it.effect("restarts a cached daemon that no longer answers its health check", () => + it.effect("replaces an inherited daemon and reuses only its own healthy daemon", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; let attempts = 0; - const { host, daemonHealthy } = yield* agentFixture((stateDir) => - Effect.gen(function* () { - attempts++; - yield* fs - .writeFileString( - `${stateDir}/daemon.json`, - JSON.stringify({ httpPort: 12345 + attempts, token: `token-${attempts}` }), - ) - .pipe(Effect.orDie); - return processOutput(0); - }), + const { host, daemonHealthy, runs } = yield* agentFixture( + (stateDir) => + Effect.gen(function* () { + attempts++; + yield* fs + .writeFileString( + `${stateDir}/daemon.json`, + JSON.stringify({ httpPort: 12345 + attempts, token: `token-${attempts}` }), + ) + .pipe(Effect.orDie); + return processOutput(0); + }), + processOutput(0), + { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: AGENT_DEVICE_VERSION, + }, ); const first = yield* host.ensureAgentReady(() => Effect.void); + expect(first.agentDevice?.token).toBe("token-1"); expect((yield* host.ensureAgentReady(() => Effect.void)).agentDevice).toEqual( first.agentDevice, ); @@ -159,6 +180,7 @@ describe("AgentDevice startup", () => { yield* Ref.set(daemonHealthy, false); const restarted = yield* host.ensureAgentReady(() => Effect.void); expect(attempts).toBe(2); + expect(runs.filter((run) => run.args.includes("daemon"))).toHaveLength(1); expect(restarted.agentDevice).toMatchObject({ baseUrl: "http://127.0.0.1:12347", token: "token-2", @@ -166,6 +188,34 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + it.effect.each([ + { scenario: "failed stop", output: processOutput(1) }, + { scenario: "timed-out stop", output: processOutput(0, "", "", true) }, + { scenario: "retained registration", output: processOutput(0) }, + ])("does not adopt the previous daemon after a $scenario", ({ output }) => + Effect.gen(function* () { + const previous = { + httpPort: 12345, + token: "previous-token", + pid: process.pid, + version: AGENT_DEVICE_VERSION, + }; + const { host, fs, stateDir, daemonStop } = yield* agentFixture( + () => Effect.die("Do not start a replacement until the old daemon has retired"), + processOutput(0), + previous, + ); + daemonStop.output = output; + daemonStop.removeRecord = false; + const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "AgentDeviceStartError", + reason: "daemon_retirement_unconfirmed", + }); + expect(JSON.parse(yield* fs.readFileString(`${stateDir}/daemon.json`))).toEqual(previous); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + it.effect("uses the SDK a later shell environment selects", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index 50089af431ff8..f968384a5935a 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -530,6 +530,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const startAgentDeviceDaemon = Effect.fn("LocalDeviceHost.startAgentDeviceDaemon")(function* ( agentTool: Pick, nodePath: string, + currentEndpoint?: DeviceHost.AgentDaemonEndpoint, ): Effect.fn.Return< DeviceHost.AgentDeviceEndpoint, | DeviceHost.DeviceHostError @@ -567,8 +568,28 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { Effect.scoped, Effect.orElseSucceed(() => false), ); - if (alive) return toEndpoint(existing.value); - yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + if (alive) { + const endpoint = toEndpoint(existing.value); + if ( + currentEndpoint?.baseUrl === endpoint.baseUrl && + currentEndpoint.token === endpoint.token + ) { + return endpoint; + } + // A previous server's in-memory device sessions cannot be recovered by this host. + yield* stopAgentDeviceDaemon; + if (yield* fs.exists(daemonFilePath()).pipe(Effect.orElseSucceed(() => true))) { + return yield* new DeviceHost.AgentDeviceStartError({ + hostId, + stateDir, + exitCode: null, + reason: "daemon_retirement_unconfirmed", + cause: new Error("The previous agent-device daemon retained its registration."), + }); + } + } else { + yield* fs.remove(daemonFilePath(), { force: true }).pipe(Effect.ignore); + } } // No `daemon start` exists; `devices` is the cheapest command that starts and awaits it. const runResult = yield* runner @@ -758,6 +779,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { const agentDevice = yield* startAgentDeviceDaemon( running.agentDevice, running.hub.nodePath, + running.agentDevice, ); yield* Ref.set(runningRef, { ...running, agentDevice }); return { ...toReady(running), agentDevice }; From f0e3edfbbaabf7a1baecf8a91cf9817dafbc6d89 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 12:47:06 +0000 Subject: [PATCH 2/3] test(server): collapse daemon retirement cases into the one path they exercise Co-Authored-By: Claude Opus 5.5 --- .../server/src/device/LocalDeviceHost.test.ts | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/apps/server/src/device/LocalDeviceHost.test.ts b/apps/server/src/device/LocalDeviceHost.test.ts index 498de74f2f4a9..62964ebb38caa 100644 --- a/apps/server/src/device/LocalDeviceHost.test.ts +++ b/apps/server/src/device/LocalDeviceHost.test.ts @@ -52,7 +52,7 @@ const agentFixture = Effect.fn("agentFixture")(function* ( const daemonStopRequested = yield* Deferred.make>(); const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata")); const daemonHealthy = yield* Ref.make(true); - const daemonStop = { output: processOutput(0), removeRecord: true }; + const daemonStop = { retainsRecord: false }; const runs: Array = []; for (const [name, version, entry] of [ ["expo-device-hub", DEVICE_HUB_VERSION, "dist/server/cli.mjs"], @@ -112,15 +112,13 @@ const agentFixture = Effect.fn("agentFixture")(function* ( : input.args.includes("daemon") ? Deferred.succeed(daemonStopRequested, input.args).pipe( Effect.andThen(() => - daemonStop.removeRecord && - daemonStop.output.code === 0 && - !daemonStop.output.timedOut - ? fs + daemonStop.retainsRecord + ? Effect.void + : fs .remove(path.join(stateDir, "daemon.json"), { force: true }) - .pipe(Effect.orDie) - : Effect.void, + .pipe(Effect.orDie), ), - Effect.as(daemonStop.output), + Effect.as(processOutput(0)), ) : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0)); }, @@ -188,11 +186,7 @@ describe("AgentDevice startup", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); - it.effect.each([ - { scenario: "failed stop", output: processOutput(1) }, - { scenario: "timed-out stop", output: processOutput(0, "", "", true) }, - { scenario: "retained registration", output: processOutput(0) }, - ])("does not adopt the previous daemon after a $scenario", ({ output }) => + it.effect("does not adopt an inherited daemon that keeps its registration after stop", () => Effect.gen(function* () { const previous = { httpPort: 12345, @@ -205,8 +199,7 @@ describe("AgentDevice startup", () => { processOutput(0), previous, ); - daemonStop.output = output; - daemonStop.removeRecord = false; + daemonStop.retainsRecord = true; const error = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.flip); expect(error).toMatchObject({ _tag: "AgentDeviceStartError", From baee643269a3db52c123c8a2a129c71adc2f73ac Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 13:06:55 +0000 Subject: [PATCH 3/3] fix(server): give the daemon stop CLI time to escalate to SIGKILL Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/LocalDeviceHost.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/device/LocalDeviceHost.ts b/apps/server/src/device/LocalDeviceHost.ts index f968384a5935a..6e27faad1134c 100644 --- a/apps/server/src/device/LocalDeviceHost.ts +++ b/apps/server/src/device/LocalDeviceHost.ts @@ -55,6 +55,8 @@ import { const HUB_READY_TIMEOUT_MS = 30_000; const DAEMON_READY_TIMEOUT_MS = 30_000; const DAEMON_POLL_MS = 100; +// `daemon stop` waits 10s after SIGTERM and 2s after SIGKILL before it retires the registration. +const DAEMON_STOP_TIMEOUT_MS = 15_000; const HUB_RESTART_STABLE_UPTIME_MS = 60_000; const HUB_RESTART_MAX_DELAY_MS = 30_000; @@ -682,7 +684,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () { agentDeviceStateDir(path, config.stateDir), ], env: { ...hostEnvironment, AGENT_DEVICE_NO_UPDATE_NOTIFIER: "1" }, - timeout: Duration.seconds(10), + timeout: Duration.millis(DAEMON_STOP_TIMEOUT_MS), timeoutBehavior: "timedOutResult", }); }).pipe(