Skip to content

Commit c8a7626

Browse files
thymikeeclaude
andcommitted
fix(daemon): report a failed startup lease recovery instead of leaving it unhandled
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1641aa5 commit c8a7626

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎packages/daemon/src/server/__tests__/provider-lease-expiry.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ test('a daemon refused the registration leaves the live journal byte-identical a
382382
test('daemon startup recovers inherited lease records only after it holds the registration', () => {
383383
const source = fs.readFileSync(new URL('../http/daemon-runtime.ts', import.meta.url), 'utf8');
384384
const refused = source.indexOf("reason: 'daemon_registration_refused'");
385-
const recovery = source.indexOf('expiredProviderLeaseReleaser.recoverInherited(');
385+
const recovery = source.indexOf('.recoverInherited(');
386386

387387
expect(refused).toBeGreaterThanOrEqual(0);
388388
expect(recovery).toBeGreaterThan(refused);

‎packages/daemon/src/server/http/daemon-runtime.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,15 @@ async function startDaemonRuntimeOnHost(
673673
});
674674
// Only the registered daemon may read the state directory's lease records and act on them. Loading and
675675
// adoption run before the first await, so their report reaches this startup's diagnostics.
676-
void expiredProviderLeaseReleaser.recoverInherited((diagnostic) =>
677-
startupDiagnostics.push(diagnostic),
678-
);
676+
void expiredProviderLeaseReleaser
677+
.recoverInherited((diagnostic) => startupDiagnostics.push(diagnostic))
678+
.catch((error) => {
679+
emitDiagnostic({
680+
level: 'warn',
681+
phase: 'provider_lease_startup_recovery_failed',
682+
data: { error: error instanceof Error ? error.message : String(error) },
683+
});
684+
});
679685
await reapOwnedProcessRecordsAtStartup(deps.ownedProcessRecords, {
680686
openWebSessionNames: openWebSessionNames(sessionStore),
681687
purposes: ['simctl-screen-recording'],

‎packages/daemon/src/server/provider-lease-expiry.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ export function createExpiredProviderLeaseReleaser(options: {
109109
};
110110

111111
const adoptOrphanedLeases = (onDiagnostic?: (diagnostic: ResourceDiagnostic) => void): void => {
112+
if (!options.stateDir) return;
112113
const orphaned = loadLeaseRecord(options.stateDir, LIVE_LEASES_FILE);
113-
if (orphaned.size === 0 || !options.stateDir) return;
114+
if (orphaned.size === 0) return;
114115
for (const [leaseId, lease] of orphaned) {
115116
if (!pendingRecoveryLeases.has(leaseId)) pendingRecoveryLeases.set(leaseId, lease);
116117
}
@@ -167,10 +168,11 @@ export function createExpiredProviderLeaseReleaser(options: {
167168
): Promise<void> => {
168169
if (inherited) return;
169170
inherited = true;
171+
// Everything in the expired record is durable by definition, which is what the persisted ids track.
170172
for (const [leaseId, lease] of loadLeaseRecord(options.stateDir, PENDING_RELEASES_FILE)) {
171173
pendingRecoveryLeases.set(leaseId, lease);
172-
persistedRecoveryLeaseIds.add(leaseId);
173174
}
175+
for (const leaseId of pendingRecoveryLeases.keys()) persistedRecoveryLeaseIds.add(leaseId);
174176
adoptOrphanedLeases(onDiagnostic);
175177
await retryPending();
176178
};

0 commit comments

Comments
 (0)