Skip to content

Commit d2177db

Browse files
committed
fix(server): preserve device host ownership during concurrent startup
1 parent f8ca7b6 commit d2177db

4 files changed

Lines changed: 196 additions & 114 deletions

File tree

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

Lines changed: 92 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -150,71 +150,24 @@ it.effect("keeps hosts independent when serials collide and another host fails",
150150

151151
it.effect("starts other hosts while one host is still starting", () =>
152152
Effect.gen(function* () {
153-
const slowStarted = yield* Deferred.make<void>();
154-
const finishSlow = yield* Deferred.make<void>();
155-
const stops: string[] = [];
156-
const host = (id: string, start: Effect.Effect<void>): DeviceHost.DeviceHost["Service"] => {
157-
const run = () => Effect.succeed({ stdout: "", stderr: "", code: 0 });
158-
const ready: DeviceHost.DeviceHostAgentReady = {
159-
nodePath: process.execPath,
160-
hub: { origin: `http://${id}` },
161-
devices: makeDeviceHubOperations({
162-
hostId: id,
163-
origin: `http://${id}`,
164-
http: HttpClient.make(() => Effect.die("unused")),
165-
run,
166-
androidAvailable: Effect.succeed(false),
167-
}),
168-
agentDevice: null,
169-
run,
170-
helpers: { serveSimAxSettings: null, serveSimCli: null },
171-
};
172-
return {
173-
id,
174-
label: id,
175-
summary: Effect.succeed({
176-
id,
177-
label: id,
178-
kind: "ssh",
179-
hubInstalled: true,
180-
agentDeviceInstalled: true,
181-
platforms: [],
182-
}),
183-
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
184-
ensureReady: () => start.pipe(Effect.as(ready)),
185-
ensureAgentReady: () => start.pipe(Effect.as(ready)),
186-
current: Effect.succeed(null),
187-
stopAgent: Effect.void,
188-
stop: Effect.sync(() => {
189-
stops.push(id);
190-
}),
191-
};
192-
};
193-
const hosts = new Map([
194-
[
195-
"slow",
196-
host(
197-
"slow",
198-
Deferred.succeed(slowStarted, undefined).pipe(Effect.andThen(Deferred.await(finishSlow))),
199-
),
200-
],
201-
["fast", host("fast", Effect.void)],
202-
]);
153+
const slowHost = yield* pausedHost("ensureReady", "slow");
154+
const fastHost = yield* pausedHost("ensureAgentReady", "fast");
155+
const hosts = new Map([slowHost.host, fastHost.host].map((host) => [host.id, host]));
203156
const service = yield* DeviceService.makeWithHosts(hosts).pipe(
204157
Effect.provide(NodeCrypto.layer),
205158
);
206159
const slow = yield* service.readiness("slow").pipe(Effect.flip, Effect.forkChild);
207-
yield* Deferred.await(slowStarted);
160+
yield* Deferred.await(slowHost.started);
208161
expect((yield* service.readiness("fast")).hostId).toBe("fast");
209162
expect((yield* service.state).hostStatuses.fast?.status).toBe("ready");
210-
// Turning device support off while a host starts stops that host once it is up.
211163
yield* service.configure({ enabled: false });
212-
yield* Deferred.succeed(finishSlow, undefined);
164+
yield* Deferred.succeed(slowHost.finish, undefined);
213165
const error = yield* Fiber.join(slow);
214166
expect(error._tag === "DeviceHostUnavailableError" && error.reason).toBe(
215167
"Device support was turned off while the host was starting.",
216168
);
217-
expect(stops).toEqual(["slow", "fast", "slow"]);
169+
expect(slowHost.calls).toEqual(["stop", "stop"]);
170+
expect(fastHost.calls).toEqual(["stop"]);
218171
expect((yield* service.state).hostStatuses).toEqual({});
219172
}).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true }))),
220173
);
@@ -307,19 +260,21 @@ it.effect("stops a start that fails after device support was turned off before i
307260
),
308261
);
309262

310-
// A local host whose `pausing` start waits until the test lets it finish.
311-
const pausedLocalHost = (pausing: "ensureReady" | "ensureAgentReady") =>
263+
const pausedHost = (
264+
pausing: "ensureReady" | "ensureAgentReady",
265+
id: string = LOCAL_DEVICE_HOST_ID,
266+
) =>
312267
Effect.gen(function* () {
313268
const started = yield* Deferred.make<void>();
314269
const finish = yield* Deferred.make<void>();
315270
const calls: string[] = [];
316271
const run = () => Effect.succeed({ stdout: "", stderr: "", code: 0 });
317272
const ready: DeviceHost.DeviceHostAgentReady = {
318273
nodePath: process.execPath,
319-
hub: { origin: "http://local" },
274+
hub: { origin: `http://${id}` },
320275
devices: makeDeviceHubOperations({
321-
hostId: LOCAL_DEVICE_HOST_ID,
322-
origin: "http://local",
276+
hostId: id,
277+
origin: `http://${id}`,
323278
http: HttpClient.make((request) =>
324279
Effect.succeed(
325280
HttpClientResponse.fromWeb(request, Response.json({ simulators: [], emulators: [] })),
@@ -340,12 +295,12 @@ const pausedLocalHost = (pausing: "ensureReady" | "ensureAgentReady") =>
340295
)
341296
: Effect.succeed(ready);
342297
const host: DeviceHost.DeviceHost["Service"] = {
343-
id: LOCAL_DEVICE_HOST_ID,
298+
id,
344299
label: "Local",
345300
summary: Effect.succeed({
346-
id: LOCAL_DEVICE_HOST_ID,
301+
id,
347302
label: "Local",
348-
kind: "local",
303+
kind: id === LOCAL_DEVICE_HOST_ID ? "local" : "ssh",
349304
hubInstalled: true,
350305
agentDeviceInstalled: true,
351306
platforms: [{ platform: "android", available: true }],
@@ -366,7 +321,7 @@ const pausedLocalHost = (pausing: "ensureReady" | "ensureAgentReady") =>
366321

367322
it.effect("stays disabled when device support is turned off while listing starts a host", () =>
368323
Effect.gen(function* () {
369-
const { host, started, finish, calls } = yield* pausedLocalHost("ensureReady");
324+
const { host, started, finish, calls } = yield* pausedHost("ensureReady");
370325
const service = yield* DeviceService.makeWithHosts(new Map([[host.id, host]])).pipe(
371326
Effect.provide(NodeCrypto.layer),
372327
);
@@ -384,7 +339,7 @@ it.effect("stays disabled when device support is turned off while listing starts
384339

385340
it.effect("retries without agent tools when agent access is turned off while they start", () =>
386341
Effect.gen(function* () {
387-
const { host, started, finish, calls } = yield* pausedLocalHost("ensureAgentReady");
342+
const { host, started, finish, calls } = yield* pausedHost("ensureAgentReady");
388343
const service = yield* DeviceService.makeWithHosts(new Map([[host.id, host]])).pipe(
389344
Effect.provide(NodeCrypto.layer),
390345
);
@@ -402,3 +357,76 @@ it.effect("retries without agent tools when agent access is turned off while the
402357
),
403358
),
404359
);
360+
361+
it.effect("keeps the replacement ready when an old host finishes starting", () =>
362+
Effect.gen(function* () {
363+
for (const fail of [false, true]) {
364+
const { host, started, finish, calls } = yield* pausedHost("ensureReady");
365+
const pendingHost: DeviceHost.DeviceHost["Service"] = {
366+
...host,
367+
ensureReady: (onPhase) =>
368+
host.ensureReady(onPhase).pipe(
369+
Effect.tap(() => onPhase("starting")),
370+
Effect.filterOrFail(
371+
() => !fail,
372+
() =>
373+
new DeviceHost.DeviceHostError({
374+
hostId: host.id,
375+
step: "start",
376+
cause: "offline",
377+
}),
378+
),
379+
),
380+
};
381+
const hosts = new Map([[host.id, pendingHost]]);
382+
const service = yield* DeviceService.makeWithHosts(hosts).pipe(
383+
Effect.provide(NodeCrypto.layer),
384+
);
385+
const listing = yield* service.list.pipe(Effect.forkChild);
386+
yield* Deferred.await(started);
387+
hosts.set(host.id, { ...host });
388+
yield* service.setHostStatus(host.id, { status: "ready" });
389+
yield* Deferred.succeed(finish, undefined);
390+
const state = yield* Fiber.join(listing);
391+
expect(calls).toEqual(["stop"]);
392+
expect(state.hostStatuses[host.id]?.status).toBe("ready");
393+
}
394+
}).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true }))),
395+
);
396+
397+
it.effect("retries the hub when revoked agent startup fails", () =>
398+
Effect.gen(function* () {
399+
const { host, started, finish } = yield* pausedHost("ensureAgentReady");
400+
const failingHost = {
401+
...host,
402+
ensureAgentReady: () =>
403+
host
404+
.ensureAgentReady(() => Effect.void)
405+
.pipe(
406+
Effect.andThen(
407+
Effect.fail(
408+
new DeviceHost.DeviceHostError({
409+
hostId: host.id,
410+
step: "agent",
411+
cause: "offline",
412+
}),
413+
),
414+
),
415+
),
416+
};
417+
const service = yield* DeviceService.makeWithHosts(new Map([[host.id, failingHost]])).pipe(
418+
Effect.provide(NodeCrypto.layer),
419+
);
420+
const retrying = yield* service.retryHost(host.id).pipe(Effect.forkChild);
421+
yield* Deferred.await(started);
422+
yield* service.configure({ agentAccessEnabled: false });
423+
yield* Deferred.succeed(finish, undefined);
424+
const state = yield* Fiber.join(retrying);
425+
expect(state.agentAccessEnabled).toBe(false);
426+
expect(state.hostStatuses[host.id]?.status).toBe("ready");
427+
}).pipe(
428+
Effect.provide(
429+
ServerSettings.layerTest({ enableDeviceSupport: true, enableAgentDeviceAccess: true }),
430+
),
431+
),
432+
);

0 commit comments

Comments
 (0)