Skip to content

Commit f313d0a

Browse files
fix(server): stop device daemon alongside slow hub shutdown (#33566)
Co-authored-by: maria-rcks <[email protected]>
1 parent 9a35ab0 commit f313d0a

2 files changed

Lines changed: 61 additions & 20 deletions

File tree

‎apps/server/src/device/LocalDeviceHost.test.ts‎

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import * as NodePath from "@effect/platform-node/NodePath";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import * as Deferred from "effect/Deferred";
45
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
57
import * as Layer from "effect/Layer";
68
import * as Option from "effect/Option";
79
import * as Path from "effect/Path";
@@ -45,7 +47,8 @@ const agentFixture = Effect.fn("agentFixture")(function* (
4547
const fs = yield* FileSystem.FileSystem;
4648
const path = yield* Path.Path;
4749
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>>();
4952
const stateDir = agentDeviceStateDir(path, path.join(baseDir, "userdata"));
5053
const daemonHealthy = yield* Ref.make(true);
5154
const runs: Array<ProcessRunner.ProcessRunInput> = [];
@@ -80,20 +83,22 @@ const agentFixture = Effect.fn("agentFixture")(function* (
8083
description: "Hub entry missing",
8184
}),
8285
)
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+
),
97102
),
98103
),
99104
),
@@ -102,7 +107,9 @@ const agentFixture = Effect.fn("agentFixture")(function* (
102107
runs.push(input);
103108
return input.args.includes("devices")
104109
? 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));
106113
},
107114
}),
108115
Effect.provideService(
@@ -122,7 +129,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
122129
),
123130
);
124131
yield* host.ensureReady(() => Effect.void);
125-
return { host, fs, baseDir, stateDir, hub, daemonHealthy, runs };
132+
return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStopRequested, runs };
126133
});
127134

128135
describe("AgentDevice startup", () => {
@@ -353,6 +360,33 @@ describe("AgentDevice startup", () => {
353360
expect(yield* fs.exists(path.join(stateDir, "daemon.json"))).toBe(false);
354361
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
355362
);
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+
);
356390
});
357391

358392
const diagnose = (

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,16 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
870870
const stop: DeviceHost.DeviceHost["Service"]["stop"] = startLock.withPermits(1)(
871871
Effect.gen(function* () {
872872
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+
);
876883
}),
877884
);
878885

0 commit comments

Comments
 (0)