@@ -110,7 +110,7 @@ export function createExpiredProviderLeaseReleaser(options: {
110110
111111 const adoptOrphanedLeases = ( onDiagnostic ?: ( diagnostic : ResourceDiagnostic ) => void ) : void => {
112112 if ( ! options . stateDir ) return ;
113- const orphaned = loadLeaseRecord ( options . stateDir , LIVE_LEASES_FILE ) ;
113+ const orphaned = loadLeaseRecord ( options . stateDir , LIVE_LEASES_FILE , onDiagnostic ) ;
114114 if ( orphaned . size === 0 ) return ;
115115 for ( const [ leaseId , lease ] of orphaned ) {
116116 if ( ! pendingRecoveryLeases . has ( leaseId ) ) pendingRecoveryLeases . set ( leaseId , lease ) ;
@@ -169,7 +169,11 @@ export function createExpiredProviderLeaseReleaser(options: {
169169 if ( inherited ) return ;
170170 inherited = true ;
171171 // Everything in the expired record is durable by definition, which is what the persisted ids track.
172- for ( const [ leaseId , lease ] of loadLeaseRecord ( options . stateDir , PENDING_RELEASES_FILE ) ) {
172+ for ( const [ leaseId , lease ] of loadLeaseRecord (
173+ options . stateDir ,
174+ PENDING_RELEASES_FILE ,
175+ onDiagnostic ,
176+ ) ) {
173177 pendingRecoveryLeases . set ( leaseId , lease ) ;
174178 }
175179 for ( const leaseId of pendingRecoveryLeases . keys ( ) ) persistedRecoveryLeaseIds . add ( leaseId ) ;
@@ -414,22 +418,30 @@ function writeLeaseRecord(stateDir: string, file: string, leases: readonly Devic
414418 fs . chmodSync ( filePath , 0o600 ) ;
415419}
416420
417- function loadLeaseRecord ( stateDir : string | undefined , file : string ) : Map < string , DeviceLease > {
421+ /**
422+ * Reads one lease record. A record that exists but cannot be read is left on disk and reported with its
423+ * path, so an operator can tell which record it is and end its leases by hand.
424+ */
425+ function loadLeaseRecord (
426+ stateDir : string | undefined ,
427+ file : string ,
428+ onDiagnostic ?: ( diagnostic : ResourceDiagnostic ) => void ,
429+ ) : Map < string , DeviceLease > {
418430 if ( ! stateDir ) return new Map ( ) ;
419431 const filePath = leaseRecordPath ( stateDir , file ) ;
420432 try {
421433 const parsed = JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) as PersistedProviderLeaseRecord ;
422434 if ( parsed . version !== LEASE_RECORD_VERSION || ! Array . isArray ( parsed . leases ) ) {
423- throw new Error ( ' unsupported expiry release record' ) ;
435+ throw new Error ( ` unsupported lease record (version ${ String ( parsed . version ) } )` ) ;
424436 }
425437 return new Map (
426438 parsed . leases . filter ( isPersistedDeviceLease ) . map ( ( lease ) => [ lease . leaseId , lease ] ) ,
427439 ) ;
428440 } catch ( error ) {
429441 if ( ( error as NodeJS . ErrnoException ) . code === 'ENOENT' ) return new Map ( ) ;
430- emitDiagnostic ( {
431- level : 'warn ' ,
432- phase : 'provider_lease_expiry_record_read_failed' ,
442+ onDiagnostic ?. ( {
443+ phase : 'provider_lease_record_read_failed ' ,
444+ resourcePath : filePath ,
433445 data : { error : error instanceof Error ? error . message : String ( error ) } ,
434446 } ) ;
435447 return new Map ( ) ;
0 commit comments