Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/baselines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NoDriverError } from "./driver-catalog.js";
import type { ReclaimCoordinator, SavedSource } from "./reclaim-coordinator.js";
import type { Registry, ReleasedLease } from "./registry.js";
import type { DeviceOperationClaim, DeviceOperationClaims } from "./device-operation-claims.js";
import { DeviceBusyError } from "./physical-enrollment.js";
import type { ManagedDeviceLifecycle } from "./managed-device-lifecycle.js";
import type { SerializedDecision } from "./serialized-decision.js";

Expand Down Expand Up @@ -432,7 +433,7 @@ export class BaselineService {
const claim = this.options.claims.tryClaim(device.id, "cleanup");
if (claim === undefined) {
for (const held of claimed) held.claim.release();
throw new Error(`Device ${device.id} made from baseline ${name} is busy`);
throw new DeviceBusyError(device.id, device.state === "reclaiming" ? "reclaiming" : "busy");
}
claimed.push({ claim, device });
}
Expand Down
109 changes: 109 additions & 0 deletions src/daemon/dispatcher-baselines.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { describe, expect, it, vi } from "vitest";

import { buildDispatcher, session } from "./test-dispatcher.js";

describe("Dispatcher: baseline.remove while a copy is reclaiming", () => {
it("refuses the remove with DEVICE_BUSY naming the reclaiming device, and removes the baseline once its reclaim ends", async () => {
const { clock, dispatcher, registry } = await buildDispatcher({
driverOptions: { latencyMs: { shutdown: 1_000 } },
});
const admin = session({ role: "admin" });
const operator = await dispatcher.dispatch(
"lease.request",
{ model: "iPhone 17 Pro", osVersion: "26.5", platform: "ios" },
session(),
);
await dispatcher.dispatch(
"baseline.save",
{ leaseId: operator.lease.id, name: "checkout" },
admin,
);
const copy = await dispatcher.dispatch(
"lease.request",
{ baseline: "checkout", platform: "ios" },
session(),
);
const stateOf = (): string | undefined =>
registry.snapshot.devices.find((device) => device.id === copy.device.id)?.state;
await dispatcher.dispatch("lease.release", { leaseId: copy.lease.id }, session());
await vi.waitFor(() => expect(stateOf()).toBe("reclaiming"));

const refusal = await dispatcher.dispatch("baseline.remove", { name: "checkout" }, admin).then(
() => undefined,
(rejected: unknown) => rejected,
);

expect(refusal).toMatchObject({
code: "DEVICE_BUSY",
details: { id: copy.device.id, reason: "reclaiming" },
});
await vi.waitFor(() => {
clock.advance(1_000);
expect(stateOf()).toBe("deleted");
});
expect(await dispatcher.dispatch("baseline.remove", { name: "checkout" }, admin)).toEqual({
deletedDevices: [],
name: "checkout",
versions: [1],
});
});
});

describe("Dispatcher: baseline.remove while a copy is being made", () => {
it("refuses the remove with DEVICE_BUSY and reason busy, naming the device, until the copy is leased", async () => {
const { clock, dispatcher, registry } = await buildDispatcher({
driverOptions: { latencyMs: { makeReady: 1_000 } },
});
const admin = session({ role: "admin" });
const finish = async <Result>(pending: Promise<Result>): Promise<Result> => {
let done = false;
void pending.then(
() => (done = true),
() => (done = true),
);
await vi.waitFor(() => {
clock.advance(1_000);
expect(done).toBe(true);
});
return pending;
};
const operator = await finish(
dispatcher.dispatch(
"lease.request",
{ model: "iPhone 17 Pro", osVersion: "26.5", platform: "ios" },
session(),
),
);
await dispatcher.dispatch(
"baseline.save",
{ leaseId: operator.lease.id, name: "checkout" },
admin,
);
const copyRequest = dispatcher.dispatch(
"lease.request",
{ baseline: "checkout", platform: "ios" },
session(),
);
await vi.waitFor(() =>
expect(
registry.snapshot.virtualDevices.some(
(device) => device.spec.baseline?.name === "checkout" && device.state !== "deleted",
),
).toBe(true),
);
const making = registry.snapshot.virtualDevices.find(
(device) => device.spec.baseline?.name === "checkout" && device.state !== "deleted",
);

const refusal = await dispatcher.dispatch("baseline.remove", { name: "checkout" }, admin).then(
() => undefined,
(rejected: unknown) => rejected,
);

expect(refusal).toMatchObject({
code: "DEVICE_BUSY",
details: { id: making?.id, reason: "busy" },
});
await finish(copyRequest);
});
});
2 changes: 1 addition & 1 deletion src/daemon/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ export class Dispatcher {
if (error instanceof UnknownBaselineError) {
throw new DispatchError("UNKNOWN_BASELINE", error.message, { name: error.baselineName });
}
throw error;
throw describeEnrollmentError(error);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/http/admin-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const baselineRemoveRoute = declareRoute({
errors: {
400: ["BAD_REQUEST"],
404: ["UNKNOWN_BASELINE", "UNKNOWN_WORKER"],
409: ["BASELINE_IN_USE", "WORKER_NOT_CONNECTED"],
409: ["BASELINE_IN_USE", "DEVICE_BUSY", "WORKER_NOT_CONNECTED"],
},
});

Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ Remove baseline login v1? [y/N] y
Removed baseline login v1.
```

When idle devices were made from it, the line ends with ` Deleted 2 idle devices made from it.` An unknown name is `UNKNOWN_BASELINE`; a baseline a lease uses is refused with `BASELINE_IN_USE` and nothing is deleted. With `--worker wrk_a` the prompt and the line end in ` from wrk_a`.
When idle devices were made from it, the line ends with ` Deleted 2 idle devices made from it.` An unknown name is `UNKNOWN_BASELINE`; a baseline a lease uses is refused with `BASELINE_IN_USE` and nothing is deleted. A copy that is being cleaned up or held by another operation is refused with `DEVICE_BUSY`; run the command again once it ends. With `--worker wrk_a` the prompt and the line end in ` from wrk_a`.

## simlock device

Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/docs/reference/errors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ In a script, check `code`, not the message: the wording of a message can change,
| Code | Exit | HTTP | Meaning | What to do |
| --- | --- | --- | --- | --- |
| `DEVICE_NOT_ENROLLABLE` | 12 | 422 | `device add` of a device that cannot be enrolled. The reason is one of: not connected, network only, not trusted, Developer Mode off, OS too old, unsupported kind, locked, unreadable, a simulator or emulator, or enrolled on the other platform. | Fix what the message names (connect it by USB, trust this computer, turn on Developer Mode, unlock it) and run `device add` again. |
| `DEVICE_BUSY` | 11 | 409 | `device add` or `device remove` of a device that is leased, being cleaned, or held by another operation. | Try again once that ends. |
| `DEVICE_BUSY` | 11 | 409 | `device add` or `device remove` of a device that is leased, being cleaned, or held by another operation. `baseline remove` of a baseline whose copy is being cleaned up or held by another operation (a leased copy gets `BASELINE_IN_USE`). | Try again once that ends. |
| `UNKNOWN_DEVICE` | 12 | 404 | `device remove` of an ID that is not an enrolled device. | Check the ID with `simlock list --devices`. |

## Tunnels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Each of these does what the matching `simlock` command does:
| `listBaselines()` | `simlock baseline list` | `{ baselines }`: the newest version of each name. On a gateway it covers every worker, each entry with its `workerId`. |
| `removeBaseline({ name, workerId? })` | `simlock baseline remove` | `{ name, versions, deletedDevices }`. On a gateway, `workerId` is required and names the worker to remove it from. |

The baseline calls can throw `UNKNOWN_LEASE`, `BASELINE_SAVE_FAILED`, `UNKNOWN_BASELINE` and `BASELINE_IN_USE`; see [Baselines](/simlock/docs/worker/baselines).
The baseline calls can throw `UNKNOWN_LEASE`, `BASELINE_SAVE_FAILED`, `UNKNOWN_BASELINE` and `BASELINE_IN_USE`; `removeBaseline` can also throw `DEVICE_BUSY`; see [Baselines](/simlock/docs/worker/baselines).

### Update the daemon

Expand Down
3 changes: 2 additions & 1 deletion website/src/content/docs/docs/worker/baselines.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Remove baseline login v1? [y/N] y
Removed baseline login v1.
```

This deletes every version of `login` and the idle copies made from it. If an agent is still using a copy, Simlock refuses with `BASELINE_IN_USE` and names the lease; remove it after that lease ends. Pass `--yes` to skip the question, for example in a script.
This deletes every version of `login` and the idle copies made from it. If an agent is still using a copy, Simlock refuses with `BASELINE_IN_USE` and names the lease; remove it after that lease ends. If a copy is still being cleaned up after its lease ended, Simlock refuses with `DEVICE_BUSY`; try again in a moment. Pass `--yes` to skip the question, for example in a script.

`simlock nuke --delete-devices` removes every baseline on the worker.

Expand All @@ -97,6 +97,7 @@ This deletes every version of `login` and the idle copies made from it. If an ag
| `UNKNOWN_BASELINE` | No baseline on this worker has that name. | Check the name with `simlock baseline list`. |
| `BASELINE_STALE` | The runtime or image the baseline needs is gone. | Install it again, or save a new version. |
| `BASELINE_IN_USE` | `remove` was refused because a lease uses a copy. | Wait for that lease to end, then remove. |
| `DEVICE_BUSY` | `remove` was refused because a copy is being cleaned up or another operation holds it. | Try again in a moment. |
| `FORBIDDEN` | The session does not have the admin role. | Run the command as an admin. |

`simlock doctor` lists each baseline's files on disk under `sources`. Files that belong to no baseline are reported as orphans; only `simlock doctor --purge-orphans` deletes them.
Expand Down
Loading