Skip to content

Commit cc92740

Browse files
fix(server): Simlock renewals no longer exceed the gateway's lease TTL cap (#35988)
1 parent 939b288 commit cc92740

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SecretProviderId, type SimlockDeviceHostConfig } from "@t3tools/contrac
44
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
55
import * as HostProcess from "@t3tools/shared/HostProcess";
66
import * as Effect from "effect/Effect";
7+
import * as Clock from "effect/Clock";
78
import * as Fiber from "effect/Fiber";
89
import * as Deferred from "effect/Deferred";
910
import * as Exit from "effect/Exit";
@@ -56,6 +57,8 @@ const fakePool = (
5657
readonly entered: Deferred.Deferred<void>;
5758
readonly proceed: Deferred.Deferred<void>;
5859
};
60+
/** The gateway's `lease.maxTtlMs`: leases expire one TTL after their last renewal. */
61+
readonly maxTtlMs?: number;
5962
} = {},
6063
) =>
6164
Effect.gen(function* () {
@@ -66,15 +69,28 @@ const fakePool = (
6669
let leases = 0;
6770
let renewStatus = 200;
6871
let catalog: unknown = CATALOG;
72+
let leaseDeadline = Infinity;
6973
const http = HttpClient.make((request) =>
70-
Effect.sync(() => {
74+
Effect.gen(function* () {
75+
const now = yield* Clock.currentTimeMillis;
7176
const url = new URL(request.url);
7277
requests.push(`${request.method} ${url.pathname}`);
7378
const json = (body: unknown, status = 200) =>
7479
HttpClientResponse.fromWeb(request, Response.json(body, { status }));
7580
if (request.headers.authorization !== "Bearer agent-token")
7681
return json({ error: { code: "UNAUTHENTICATED", message: "Unknown bearer token" } }, 401);
7782
if (url.pathname === "/v1/catalog") return json(catalog);
83+
if (url.pathname.endsWith("/renew") && options.maxTtlMs !== undefined) {
84+
if (now >= leaseDeadline) return json({}, 404);
85+
const body =
86+
request.body._tag === "Uint8Array"
87+
? (JSON.parse(new TextDecoder().decode(request.body.body)) as { ttlMs?: number })
88+
: {};
89+
if ((body.ttlMs ?? 0) > options.maxTtlMs)
90+
return json({ error: { code: "BAD_REQUEST", message: "ttlMs exceeds maxTtlMs" } }, 400);
91+
leaseDeadline = now + (body.ttlMs ?? options.maxTtlMs);
92+
return json({});
93+
}
7894
if (url.pathname.endsWith("/renew")) return json({}, renewStatus);
7995
if (request.method === "DELETE") return json({}, 202);
8096
return json({}, 404);
@@ -114,6 +130,8 @@ const fakePool = (
114130
path.join(dir, `${session}.json`),
115131
JSON.stringify({ leaseId: `lease-${++leases}` }),
116132
);
133+
if (options.maxTtlMs !== undefined)
134+
leaseDeadline = (yield* Clock.currentTimeMillis) + options.maxTtlMs;
117135
}
118136
if (command === "screenshot") yield* fs.writeFile(args[1]!, new Uint8Array([1, 2, 3]));
119137
return {
@@ -475,6 +493,21 @@ describe("Simlock device host", () => {
475493
}).pipe(Effect.scoped, Effect.provide(layer)),
476494
);
477495

496+
it.effect("keeps a watched lease renewed when the gateway caps lease TTLs", () =>
497+
Effect.gen(function* () {
498+
const pool = yield* fakePool({ maxTtlMs: 5 * 60_000 });
499+
const host = yield* makeHost(pool, config(), yield* Scope.make());
500+
const ready = yield* host.ensureReady(() => Effect.void);
501+
const [iphone] = (yield* ready.devices.list).devices;
502+
const leaseId = yield* ready.devices.boot(iphone!);
503+
504+
yield* TestClock.adjust("6 minutes");
505+
expect((yield* ready.devices.list).devices.some((device) => device.id === leaseId)).toBe(
506+
true,
507+
);
508+
}).pipe(Effect.scoped, Effect.provide(layer)),
509+
);
510+
478511
it.effect("returns a lease whose open finishes after the host stopped", () =>
479512
Effect.gen(function* () {
480513
const holdOpen = {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const REQUEST_TIMEOUT = Duration.seconds(15);
4242
// The gateway queues a lease request for up to five minutes when every device is busy.
4343
const OPEN_TIMEOUT = Duration.minutes(7);
4444
const COMMAND_TIMEOUT = Duration.minutes(2);
45-
// Renewal keeps a watched but idle device; a lease this server stops renewing ends after LEASE_TTL.
45+
// Renewal keeps a watched but idle device. It sends no ttlMs: Simlock then keeps the lease's own
46+
// length, while one above the gateway's lease.maxTtlMs is refused rather than shortened.
4647
const RENEW_INTERVAL = Duration.minutes(2);
47-
const LEASE_TTL_MS = 10 * 60_000;
4848
const STREAM_PATH = /^\/vendor\/serve-sim\/helper\/([^/]+)\/stream\.avcc$/;
4949
const INPUT_PATH = "/vendor/serve-sim/helper/ws";
5050

@@ -422,7 +422,6 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
422422
lease.token,
423423
"POST",
424424
`/v1/leases/${encodeURIComponent(lease.leaseId)}/renew`,
425-
{ ttlMs: LEASE_TTL_MS },
426425
).pipe(Effect.result);
427426
if (renewed._tag === "Success") continue;
428427
const error = renewed.failure;

0 commit comments

Comments
 (0)