Skip to content

Commit 004767e

Browse files
committed
fix(lease): renew a heartbeat with no ttl for the window the lease already carries
`heartbeatLease` resolved an absent `ttlMs` through the registry's default resolver, so a caller that heartbeats without repeating its allocation TTL silently shortened the lease to the daemon default. Every renewal that is not asked to change the window now renews for the window the lease is living on, which is the rule `refreshProtectedLease` already applied to protected work; that arithmetic moves to `leaseOwnTtlMs` beside the other lease-scope rules so both callers read one definition. The caller that heartbeats without a TTL is not asking for the default, it is asking for the same lease to keep going. An admitted request does exactly that, and this is half of why a long upload expired the lease paying for its own device (#2946).
1 parent 67566f4 commit 004767e

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

‎src/daemon/__tests__/lease-registry.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,59 @@ test('releasing a lease drops the work claims recorded against it', () => {
646646
assert.equal(registry.listActiveLeases().length, 0, 'a released lease must not come back');
647647
});
648648

649+
// #2946: a caller that heartbeats without repeating its allocation TTL is asking for the same lease
650+
// to keep going, not for the registry default. Resolving an absent `ttlMs` to that default silently
651+
// shortened every lease allocated above it, which is how an upload expired the lease paying for the
652+
// device it was uploading to.
653+
test('a heartbeat with no ttlMs renews the lease for the window it already carries', () => {
654+
let now = 1_000;
655+
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 10_000 });
656+
const lease = registry.allocateLease({ tenantId: 'tenant-a', runId: 'run-1', ttlMs: 60_000 });
657+
658+
now = 2_000;
659+
const renewed = registry.heartbeatLease({ leaseId: lease.leaseId });
660+
assert.equal(renewed.heartbeatAt, 2_000);
661+
assert.equal(renewed.expiresAt, 62_000, 'the allocated 60s window, not the 10s default');
662+
});
663+
664+
test('a heartbeat with no ttlMs keeps a long-TTL lease outliving the default TTL', () => {
665+
let now = 0;
666+
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 60_000 });
667+
const lease = registry.allocateLease({
668+
tenantId: 'tenant-a',
669+
runId: 'run-1',
670+
leaseProvider: 'proxy',
671+
deviceKey: 'android:mobile:emulator-5554',
672+
ttlMs: 5 * 60_000,
673+
});
674+
675+
// The 1m47s upload from the issue, beaten every 20s by the client and admitted after it lands.
676+
for (const elapsed of [20_000, 40_000, 60_000, 80_000, 100_000, 107_000]) {
677+
now = elapsed;
678+
registry.heartbeatLease({
679+
leaseId: lease.leaseId,
680+
tenantId: 'tenant-a',
681+
runId: 'run-1',
682+
leaseProvider: 'proxy',
683+
deviceKey: 'android:mobile:emulator-5554',
684+
});
685+
}
686+
687+
const active = registry.listActiveLeases().find((entry) => entry.leaseId === lease.leaseId);
688+
assert.ok(active, 'the lease is still active at the end of the upload');
689+
assert.equal(active.expiresAt - active.heartbeatAt, 5 * 60_000);
690+
});
691+
692+
test('a heartbeat with an explicit ttlMs still sets that window', () => {
693+
let now = 1_000;
694+
const registry = new LeaseRegistry({ now: () => now, defaultLeaseTtlMs: 10_000 });
695+
const lease = registry.allocateLease({ tenantId: 'tenant-a', runId: 'run-1', ttlMs: 60_000 });
696+
697+
now = 2_000;
698+
const renewed = registry.heartbeatLease({ leaseId: lease.leaseId, ttlMs: 5_000 });
699+
assert.equal(renewed.expiresAt, 7_000, 'an explicit window is the caller asking to change it');
700+
});
701+
649702
function inFlightClaimKeys(registry: LeaseRegistry): string[] {
650703
const work = (
651704
registry as unknown as {

‎src/daemon/lease-registry-scope.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,18 @@ export function createDeviceLease(
328328
};
329329
}
330330

331+
/**
332+
* The inactivity window a lease is currently living on: whatever TTL it was last renewed for.
333+
*
334+
* Every renewal that is not asked to change the window uses this, so a renewal never quietly
335+
* re-decides it. Reading the registry default instead would shorten a lease allocated with a longer
336+
* one — and a caller that heartbeats without repeating the allocation's TTL is not asking for the
337+
* default, it is asking for the same lease to keep going.
338+
*/
339+
export function leaseOwnTtlMs(lease: Pick<DeviceLease, 'expiresAt' | 'heartbeatAt'>): number {
340+
return lease.expiresAt - lease.heartbeatAt;
341+
}
342+
331343
export function deviceLeaseBusyError(activeLease: DeviceLease): AppError {
332344
return new AppError('DEVICE_IN_USE', 'Device is already leased', {
333345
reason: 'DEVICE_LEASE_BUSY',

‎src/daemon/lease-registry.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
assertLeaseOwnerScope,
2424
assertLeaseScopeMatch,
2525
leaseDeviceBindingKey,
26+
leaseOwnTtlMs,
2627
leaseRunBindingKey,
2728
} from './lease-registry-scope.ts';
2829
import { DeviceMutationDrain } from './device/device-mutation-drain.ts';
@@ -107,13 +108,21 @@ export class LeaseRegistry {
107108
return this.refreshLease(existingLease, leaseTtlMs);
108109
}
109110

111+
/**
112+
* Extends a lease's life. A request naming a `ttlMs` asks for that inactivity window; one naming
113+
* none renews for the window the lease already carries, the way protected work renews for its
114+
* existing one (ADR 0007). Resolving an absent `ttlMs` to the registry default instead would
115+
* shorten a lease every time a caller heartbeats without repeating its allocation TTL — which is
116+
* what an admitted request does, and what expired the lease paying for a device mid-upload (#2946).
117+
*/
110118
heartbeatLease(request: HeartbeatLeaseRequest): DeviceLease {
111119
const leaseId = normalizeRequiredLeaseId(request.leaseId);
112120
this.cleanupExpiredLeases();
113121
const lease = this.getActiveLease(leaseId);
114122
assertLeaseOwnerScope(lease, request);
115123
assertLeaseScopeMatch(lease, request);
116-
const leaseTtlMs = this.resolveLeaseTtlMs(request.ttlMs);
124+
const leaseTtlMs =
125+
request.ttlMs === undefined ? leaseOwnTtlMs(lease) : this.resolveLeaseTtlMs(request.ttlMs);
117126
return this.refreshLease(lease, leaseTtlMs);
118127
}
119128

@@ -427,11 +436,7 @@ export class LeaseRegistry {
427436
private refreshProtectedLease(leaseId: string | undefined, at: number): void {
428437
const lease = leaseId ? this.leases.get(leaseId) : undefined;
429438
if (lease) {
430-
this.refreshLease(
431-
lease,
432-
lease.expiresAt - lease.heartbeatAt,
433-
Math.max(at, lease.heartbeatAt),
434-
);
439+
this.refreshLease(lease, leaseOwnTtlMs(lease), Math.max(at, lease.heartbeatAt));
435440
}
436441
}
437442

0 commit comments

Comments
 (0)