Skip to content

Commit 5a1a98f

Browse files
committed
fix(server): invalidate stopped device host readiness
1 parent f3bc60f commit 5a1a98f

9 files changed

Lines changed: 85 additions & 37 deletions

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,19 @@ export class DeviceHost extends Context.Service<
191191
) => Effect.Effect<DevicePlatformAvailability>;
192192
readonly ensureReady: (
193193
onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
194-
) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
194+
) => Effect.Effect<
195+
DeviceHostReady,
196+
DeviceHostError | DeviceHostUnavailableError | NodeRuntimeUnavailableError
197+
>;
195198
readonly ensureAgentReady: (
196199
onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
197200
) => Effect.Effect<
198201
DeviceHostAgentReady,
199-
DeviceHostError | DeviceHostTimeoutError | AgentDeviceStartError | NodeRuntimeUnavailableError
202+
| DeviceHostError
203+
| DeviceHostUnavailableError
204+
| DeviceHostTimeoutError
205+
| AgentDeviceStartError
206+
| NodeRuntimeUnavailableError
200207
>;
201208
readonly current: Effect.Effect<DeviceHostReady | null>;
202209
readonly stopAgent: Effect.Effect<void>;

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { expect, it } from "@effect/vitest";
2-
import { LOCAL_DEVICE_HOST_ID, ThreadId, type DeviceHostSummary } from "@t3tools/contracts";
2+
import {
3+
DeviceHostUnavailableError,
4+
LOCAL_DEVICE_HOST_ID,
5+
ThreadId,
6+
type DeviceHostSummary,
7+
} from "@t3tools/contracts";
38
import * as Deferred from "effect/Deferred";
49
import * as Fiber from "effect/Fiber";
510
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
@@ -145,6 +150,17 @@ it.effect("keeps hosts independent when serials collide and another host fails",
145150
expect((yield* service.state).sessions).toEqual([]);
146151
yield* service.agentReadinessIfSupported("a");
147152
expect((yield* service.state).hostStatuses.a?.status).toBe("ready");
153+
const unavailable = new DeviceHostUnavailableError({
154+
hostId: "a",
155+
reason: "Host configuration changed. Retry the operation.",
156+
});
157+
hosts.set("a", {
158+
...host("a"),
159+
ensureReady: () => Effect.fail(unavailable),
160+
ensureAgentReady: () => Effect.fail(unavailable),
161+
});
162+
expect(yield* service.readiness("a").pipe(Effect.flip)).toBe(unavailable);
163+
expect(yield* service.agentReadinessIfSupported("a").pipe(Effect.flip)).toBe(unavailable);
148164
yield* service.configure({ enabled: false });
149165
expect((yield* service.state).hostStatuses).toEqual({});
150166
}).pipe(

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -490,19 +490,20 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
490490
host,
491491
),
492492
),
493-
Effect.mapError(
494-
(error) =>
495-
new DeviceHostUnavailableError({
496-
hostId: host.id,
497-
hostLabel: host.label,
498-
reason:
499-
error._tag === "NodeRuntimeUnavailableError"
500-
? nodeRuntimeUnavailableMessage("Local device support")
501-
: error.step === "probe"
502-
? sshProbeFailureMessage(error)
503-
: `Device support failed during ${error.step}.`,
504-
cause: error,
505-
}),
493+
Effect.mapError((error) =>
494+
error._tag === "DeviceHostUnavailableError"
495+
? error
496+
: new DeviceHostUnavailableError({
497+
hostId: host.id,
498+
hostLabel: host.label,
499+
reason:
500+
error._tag === "NodeRuntimeUnavailableError"
501+
? nodeRuntimeUnavailableMessage("Local device support")
502+
: error.step === "probe"
503+
? sshProbeFailureMessage(error)
504+
: `Device support failed during ${error.step}.`,
505+
cause: error,
506+
}),
506507
),
507508
stopIfRevoked(host, false),
508509
);
@@ -561,23 +562,24 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
561562
host,
562563
),
563564
),
564-
Effect.mapError(
565-
(error) =>
566-
new DeviceHostUnavailableError({
567-
hostId: host.id,
568-
hostLabel: host.label,
569-
reason:
570-
error._tag === "NodeRuntimeUnavailableError"
571-
? nodeRuntimeUnavailableMessage("Local device support")
572-
: error._tag === "DeviceHostTimeoutError"
573-
? `Agent tools did not start within ${error.timeoutMs} ms.`
574-
: error._tag === "AgentDeviceStartError"
575-
? error.message
576-
: error.step === "probe"
577-
? sshProbeFailureMessage(error)
578-
: `Device support failed during ${error.step}.`,
579-
cause: error,
580-
}),
565+
Effect.mapError((error) =>
566+
error._tag === "DeviceHostUnavailableError"
567+
? error
568+
: new DeviceHostUnavailableError({
569+
hostId: host.id,
570+
hostLabel: host.label,
571+
reason:
572+
error._tag === "NodeRuntimeUnavailableError"
573+
? nodeRuntimeUnavailableMessage("Local device support")
574+
: error._tag === "DeviceHostTimeoutError"
575+
? `Agent tools did not start within ${error.timeoutMs} ms.`
576+
: error._tag === "AgentDeviceStartError"
577+
? error.message
578+
: error.step === "probe"
579+
? sshProbeFailureMessage(error)
580+
: `Device support failed during ${error.step}.`,
581+
cause: error,
582+
}),
581583
),
582584
stopIfRevoked(host, true),
583585
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ describe("EAS Simulator host", () => {
293293
).toBeNull();
294294

295295
yield* Scope.close(scope, Exit.void);
296+
expect(yield* host.current).toBeNull();
296297
expect(expo.named("T3StopEasSimulatorSession").map((call) => call.variables.id)).toEqual([
297298
"session-1",
298299
]);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,13 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice
819819
"EasDeviceHost.ensureReady",
820820
)(function* () {
821821
if (ready) return ready;
822+
const startedIn = epoch;
822823
yield* adoptRecorded;
823824
// Without a credential the host is still ready: opening a device then
824825
// says how to sign in, and listing reaps once one is there.
825826
yield* FiberSet.run(background)(api.pipe(Effect.flatMap(reapLeftovers), Effect.ignore));
827+
if (epoch !== startedIn)
828+
return yield* unavailable(`${config.label} was stopped while readiness started.`);
826829
ready = {
827830
nodePath,
828831
hub: { origin: "https://api.expo.dev" },
@@ -850,6 +853,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice
850853
// and stays in the ledger for the next server if this one closes first.
851854
const stopAll = Effect.gen(function* () {
852855
epoch++;
856+
ready = null;
853857
// Interrupted starts stop the sessions they created before this returns.
854858
yield* FiberSet.clear(starts);
855859
yield* FiberSet.clear(background);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,15 @@ describe("Simlock device host", () => {
241241
yield* ready.devices.shutdown({ id: leaseId, platform: "android" });
242242
expect(pool.ran("close")).toHaveLength(1);
243243
expect((yield* ready.devices.list).devices.some((device) => device.booted)).toBe(false);
244+
yield* host.stop;
245+
expect(yield* host.current).toBeNull();
246+
const stoppedStart = yield* host.ensureReady(() => host.stop).pipe(Effect.result);
247+
expect(stoppedStart._tag).toBe("Failure");
248+
expect(yield* host.current).toBeNull();
249+
yield* host.ensureReady(() => Effect.void);
250+
expect(yield* host.current).not.toBeNull();
244251
yield* Scope.close(scope, Exit.void);
252+
expect(yield* host.current).toBeNull();
245253
expect(pool.ran("close")).toHaveLength(1);
246254
}).pipe(Effect.scoped, Effect.provide(layer)),
247255
);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,13 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
597597
"SimlockDeviceHost.ensureReady",
598598
)(function* (onPhase) {
599599
if (ready) return ready;
600+
const startedIn = stops;
600601
// A missing token or unreachable gateway does not stop readiness: listing
601602
// and opening devices say what is wrong instead.
602603
yield* onPhase("installing");
603604
cli = yield* agentDeviceCli;
605+
if (stops !== startedIn)
606+
return yield* unavailable("Device host was stopped while it was starting.");
604607
const { nodePath } = cli;
605608
ready = {
606609
nodePath,
@@ -621,6 +624,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
621624

622625
const stopAll = Effect.gen(function* () {
623626
stops++;
627+
ready = null;
624628
yield* FiberMap.clear(keepAlive);
625629
yield* Effect.forEach([...leases.values()], release, { discard: true, concurrency: 4 });
626630
}).pipe(Effect.ignore);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ it.effect("preserves installed status after probes and cleans failed agent activ
201201
expect(retired._tag).toBe("Failure");
202202
expect(forwards).toBe(0);
203203
const modesAfterClose = [...modes];
204-
expect((yield* host.ensureReady(() => Effect.void).pipe(Effect.result))._tag).toBe("Failure");
204+
const unavailable = yield* host.ensureReady(() => Effect.void).pipe(Effect.flip);
205+
expect(unavailable).toMatchObject({
206+
_tag: "DeviceHostUnavailableError",
207+
reason: "Host configuration changed. Retry the operation.",
208+
});
209+
expect(unavailable.cause).toBeUndefined();
205210
expect(modes).toEqual(modesAfterClose);
206211
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
207212
);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type DeviceHostSummary,
3+
DeviceHostUnavailableError,
34
DevicePlatformAvailability,
45
DeviceToolVersions,
56
deviceToolInstallMessage,
@@ -160,10 +161,10 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
160161
const requireActive = Effect.suspend(() =>
161162
retired
162163
? Effect.fail(
163-
new DeviceHost.DeviceHostError({
164+
new DeviceHostUnavailableError({
164165
hostId: config.id,
165-
step: "starting retired host",
166-
cause: new Error("Host configuration changed. Retry the operation."),
166+
hostLabel: config.label,
167+
reason: "Host configuration changed. Retry the operation.",
167168
}),
168169
)
169170
: Effect.void,

0 commit comments

Comments
 (0)