@devries-cameron20 / t3code Mirror · read-only / commits / f313d0a6cb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(server): stop device daemon alongside slow hub shutdown (#33566)

Co-authored-by: maria-rcks <maria@kuuro.net>

shivam committed Nov 13, 2026 at 20:10 UTC f313d0a6cb843551fb60e57a454a71e71a3a8c04
2 files changed +61 -20
apps/server/src/device/LocalDeviceHost.test.ts
+51 -17
@@ -1,7 +1,9 @@
1 import { describe, expect, it } from "@effect/vitest";
2 import * as NodePath from "@effect/platform-node/NodePath";
3 import * as HostProcess from "@t3tools/shared/HostProcess";
4 +import * as Deferred from "effect/Deferred";
5 import * as Effect from "effect/Effect";
6 +import * as Fiber from "effect/Fiber";
7 import * as Layer from "effect/Layer";
8 import * as Option from "effect/Option";
9 import * as Path from "effect/Path";
@@ -45,7 +47,8 @@ const agentFixture = Effect.fn("agentFixture")(function* (
47 const fs = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-agent-start-" });
48 - const hub = { running: true, spawnFails: false };
50 + const hub = { running: true, spawnFails: false, cleanup: Effect.void };
51 + const daemonStopRequested = yield* Deferred.make<ReadonlyArray<string>>();
52 const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
53 const daemonHealthy = yield* Ref.make(true);
54 const runs: Array<ProcessRunner.ProcessRunInput> = [];
@@ -80,20 +83,22 @@ const agentFixture = Effect.fn("agentFixture")(function* (
83 description: "Hub entry missing",
84 }),
85 )
83 - : Effect.succeed(
84 - ChildProcessSpawner.makeHandle({
85 - pid: ChildProcessSpawner.ProcessId(123),
86 - stdout: Stream.empty,
87 - stderr: Stream.empty,
88 - all: Stream.empty,
89 - exitCode: Effect.never,
90 - isRunning: Effect.sync(() => hub.running),
91 - kill: () => Effect.void,
92 - stdin: Sink.drain,
93 - getInputFd: () => Sink.drain,
94 - getOutputFd: () => Stream.empty,
95 - unref: Effect.succeed(Effect.void),
96 - }),
86 + : Effect.addFinalizer(() => hub.cleanup).pipe(
87 + Effect.as(
88 + ChildProcessSpawner.makeHandle({
89 + pid: ChildProcessSpawner.ProcessId(123),
90 + stdout: Stream.empty,
91 + stderr: Stream.empty,
92 + all: Stream.empty,
93 + exitCode: Effect.never,
94 + isRunning: Effect.sync(() => hub.running),
95 + kill: () => Effect.void,
96 + stdin: Sink.drain,
97 + getInputFd: () => Sink.drain,
98 + getOutputFd: () => Stream.empty,
99 + unref: Effect.succeed(Effect.void),
100 + }),
101 + ),
102 ),
103 ),
104 ),
@@ -102,7 +107,9 @@ const agentFixture = Effect.fn("agentFixture")(function* (
107 runs.push(input);
108 return input.args.includes("devices")
109 ? startup(stateDir)
105 - : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0));
110 + : input.args.includes("daemon")
111 + ? Deferred.succeed(daemonStopRequested, input.args).pipe(Effect.as(processOutput(0)))
112 + : Effect.succeed(input.args.includes("-list-avds") ? avdOutput : processOutput(0));
113 },
114 }),
115 Effect.provideService(
@@ -122,7 +129,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
129 ),
130 );
131 yield* host.ensureReady(() => Effect.void);
125 - return { host, fs, baseDir, stateDir, hub, daemonHealthy, runs };
132 + return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStopRequested, runs };
133 });
134
135 describe("AgentDevice startup", () => {
@@ -353,6 +360,33 @@ describe("AgentDevice startup", () => {
360 expect(yield* fs.exists(path.join(stateDir, "daemon.json"))).toBe(false);
361 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
362 );
363 +
364 + it.effect("stops the agent daemon while the hub is still shutting down", () =>
365 + Effect.gen(function* () {
366 + const fs = yield* FileSystem.FileSystem;
367 + const { host, hub, stateDir, daemonStopRequested } = yield* agentFixture((stateDir) =>
368 + fs
369 + .writeFileString(
370 + `${stateDir}/daemon.json`,
371 + JSON.stringify({ httpPort: 12345, token: "test-token" }),
372 + )
373 + .pipe(Effect.orDie, Effect.as(processOutput(0))),
374 + );
375 + yield* host.ensureAgentReady(() => Effect.void);
376 + const hubExited = yield* Deferred.make<void>();
377 + hub.cleanup = Deferred.await(hubExited);
378 + const stopping = yield* Effect.forkChild(host.stop);
379 + expect((yield* Deferred.await(daemonStopRequested)).slice(1)).toEqual([
380 + "daemon",
381 + "stop",
382 + "--state-dir",
383 + stateDir,
384 + ]);
385 + yield* Deferred.succeed(hubExited, undefined);
386 + yield* Fiber.join(stopping);
387 + expect(yield* fs.exists(`${stateDir}/hub.json`)).toBe(false);
388 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
389 + );
390 });
391
392 const diagnose = (
apps/server/src/device/LocalDeviceHost.ts
+10 -3
@@ -870,9 +870,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
870 const stop: DeviceHost.DeviceHost["Service"]["stop"] = startLock.withPermits(1)(
871 Effect.gen(function* () {
872 const running = yield* Ref.getAndSet(runningRef, null);
873 - yield* stopHub(running?.hub);
874 - yield* fs.remove(hubStatePath(), { force: true }).pipe(Effect.ignore);
875 - yield* stopAgentDeviceDaemon;
873 + // Hub cleanup waits for its exit unbounded; it must not strand the detached agent daemon.
874 + yield* Effect.all(
875 + [
876 + stopHub(running?.hub).pipe(
877 + Effect.andThen(fs.remove(hubStatePath(), { force: true }).pipe(Effect.ignore)),
878 + ),
879 + stopAgentDeviceDaemon,
880 + ],
881 + { concurrency: "unbounded", discard: true },
882 + );
883 }),
884 );
885