Skip to content

Commit e8f387d

Browse files
amankansal-ltclaude
andcommitted
fix(daemon): keep the creation-time TTL for a lease allocated without a provider
The no-provider allocation test ran on a frozen clock, so it could not tell a preserved TTL from one renewed when allocation completed. On an advancing clock it showed the handler did renew: the lease work pass was held, and its release restarted the TTL, even when no provider allocated anything. Hold the work pass only while a lease lifecycle provider allocates, and run the no-provider test on a clock that advances on every read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2d4981a commit e8f387d

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎src/daemon/handlers/__tests__/lease.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ test('a lease whose provider allocation outlasts its TTL is active when allocati
156156
});
157157

158158
test('a lease allocated without a provider keeps the TTL it was created with', async () => {
159-
const now = 5_000;
160-
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 60_000 });
159+
let now = 5_000;
160+
const registry = new LeaseRegistry({
161+
now: () => (now += 1_000),
162+
defaultLeaseTtlMs: 60_000,
163+
});
161164
const response = await handleLeaseCommands({
162165
req: allocateRequest(),
163166
sessionName: 'lease-ttl-test',
@@ -167,8 +170,8 @@ test('a lease allocated without a provider keeps the TTL it was created with', a
167170

168171
assert.equal(response?.ok, true);
169172
const lease = (response?.ok ? response.data?.lease : undefined) as DeviceLease;
170-
assert.equal(lease.createdAt, 5_000);
171-
assert.equal(lease.heartbeatAt, 5_000);
172-
assert.equal(lease.expiresAt, 65_000);
173+
assert.ok(now > lease.createdAt, 'the clock advanced while the lease was allocated');
174+
assert.equal(lease.heartbeatAt, lease.createdAt);
175+
assert.equal(lease.expiresAt, lease.createdAt + 60_000);
173176
assert.deepEqual(registry.listActiveLeases(), [lease]);
174177
});

‎src/daemon/handlers/lease.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export async function handleLeaseCommands(args: LeaseHandlerArgs): Promise<Daemo
7676
let providerData: Record<string, unknown> | undefined;
7777
// A hosted provider can take longer than the lease TTL to create its session; the work
7878
// pass keeps the lease alive until it does, and ending the pass restarts the TTL then.
79-
const work = leaseRegistry.retainLeaseWork(lease, () => !isRequestCanceled(requestId));
79+
const work = leaseLifecycleProvider?.allocate
80+
? leaseRegistry.retainLeaseWork(lease, () => !isRequestCanceled(requestId))
81+
: undefined;
8082
try {
8183
providerData = await leaseLifecycleProvider?.allocate?.(lease, {
8284
...leaseLifecycleContext(req),
@@ -88,7 +90,7 @@ export async function handleLeaseCommands(args: LeaseHandlerArgs): Promise<Daemo
8890
leaseRegistry.releaseLease(leaseReleaseRequestFor(lease));
8991
throw error;
9092
} finally {
91-
work.release();
93+
work?.release();
9294
}
9395
if (isRequestCanceled(requestId)) {
9496
// The requester left while the provider was allocating; the lease it

0 commit comments

Comments
 (0)