@@ -370,52 +370,80 @@ export async function acquireProcessLockAcquisition(
370370) : Promise < ProcessLockAcquisition > {
371371 const deadline = Date . now ( ) + ( params . timeoutMs ?? DEFAULT_LOCK_TIMEOUT_MS ) ;
372372 const pollMs = params . pollMs ?? DEFAULT_LOCK_POLL_MS ;
373- const ownerFilePath = path . join ( params . lockDirPath , OWNER_FILE_NAME ) ;
374- const observedOwner = createOwnerObservationReuse ( ) ;
375- const observedGuardHolder = createGuardHolderObservationReuse ( ) ;
376- if ( params . signal ?. aborted ) throw createRequestCanceledError ( ) ;
377- const guardHolderStartTime = ( await readCurrentOwnerIdentityAsync ( ) ) . startTime ;
373+ throwIfCanceled ( params . signal ) ;
374+ const observe = await createWaitObservers ( ) ;
378375 fs . mkdirSync ( path . dirname ( params . lockDirPath ) , { recursive : true } ) ;
379376 do {
380- if ( params . signal ?. aborted ) throw createRequestCanceledError ( ) ;
381- // Every process probe a wait spends runs off the event loop, before the synchronous attempt.
382- const reading = readProcessLockOwner ( ownerFilePath ) ;
383- const liveness =
384- reading . kind === 'owner' ? await observedOwner . classify ( reading . owner ) : undefined ;
385- const abandonedGuard = await findAbandonedGuardAsync ( params . lockDirPath , observedGuardHolder ) ;
386- const attempt = attemptProcessLock ( params , {
387- standing : judgeStandingClaim ( reading , liveness ) ,
388- // A record that changed since it was judged is reported without a fresh probe.
389- judgeOwner : ( owner ) =>
390- reading . kind === 'owner' && liveness && isSameClaim ( owner , reading . owner )
391- ? liveness
392- : 'unknown' ,
393- abandonedGuard,
394- guardHolderStartTime,
395- } ) ;
377+ throwIfCanceled ( params . signal ) ;
378+ const attempt = attemptProcessLock ( params , await gatherWaitEvidence ( params , observe ) ) ;
396379 if ( attempt . status === 'acquired' ) return attempt . acquisition ;
397380 const remaining = deadline - Date . now ( ) ;
398381 if ( remaining <= 0 ) break ;
399382 await sleep ( Math . min ( pollMs , remaining ) , params . signal ) ;
400- if ( params . signal ?. aborted ) throw createRequestCanceledError ( ) ;
383+ throwIfCanceled ( params . signal ) ;
401384 } while ( Date . now ( ) < deadline ) ;
402- const reading = readProcessLockOwner ( ownerFilePath ) ;
385+ throw await processLockTimeoutError ( params ) ;
386+ }
387+
388+ function throwIfCanceled ( signal : AbortSignal | undefined ) : void {
389+ if ( signal ?. aborted ) throw createRequestCanceledError ( ) ;
390+ }
391+
392+ type WaitObservers = Awaited < ReturnType < typeof createWaitObservers > > ;
393+
394+ /** What one waiter reuses across its polls: its own guard identity and the observations it reads. */
395+ async function createWaitObservers ( ) {
396+ return {
397+ owner : createOwnerObservationReuse ( ) ,
398+ guardHolder : createGuardHolderObservationReuse ( ) ,
399+ guardHolderStartTime : ( await readCurrentOwnerIdentityAsync ( ) ) . startTime ,
400+ } ;
401+ }
402+
403+ /** Every process probe a wait spends runs here, off the event loop, before the synchronous attempt. */
404+ async function gatherWaitEvidence (
405+ params : ProcessLockOptions ,
406+ observe : WaitObservers ,
407+ ) : Promise < AttemptEvidence > {
408+ const reading = readProcessLockOwner ( path . join ( params . lockDirPath , OWNER_FILE_NAME ) ) ;
409+ const liveness =
410+ reading . kind === 'owner' ? await observe . owner . classify ( reading . owner ) : undefined ;
411+ return {
412+ standing : judgeStandingClaim ( reading , liveness ) ,
413+ judgeOwner : judgeOwnerFromReading ( reading , liveness ) ,
414+ abandonedGuard : await findAbandonedGuardAsync ( params . lockDirPath , observe . guardHolder ) ,
415+ guardHolderStartTime : observe . guardHolderStartTime ,
416+ } ;
417+ }
418+
419+ /** Reports the record judged here by that judgment; a record that changed since is `unknown`. */
420+ function judgeOwnerFromReading (
421+ reading : ProcessLockOwnerReading ,
422+ liveness : OwnerLiveness | undefined ,
423+ ) : ( owner : ProcessLockOwnerRecord ) => OwnerLiveness {
424+ return ( owner ) =>
425+ reading . kind === 'owner' && liveness && isSameClaim ( owner , reading . owner )
426+ ? liveness
427+ : 'unknown' ;
428+ }
429+
430+ async function processLockTimeoutError ( params : ProcessLockOptions ) : Promise < AppError > {
431+ const reading = readProcessLockOwner ( path . join ( params . lockDirPath , OWNER_FILE_NAME ) ) ;
403432 const ownerLiveness =
404433 reading . kind === 'owner'
405434 ? await classifyOwnerLivenessAsync ( { owner : reading . owner } )
406435 : undefined ;
407- const judgeOwner = ( owner : ProcessLockOwnerRecord ) : OwnerLiveness =>
408- reading . kind === 'owner' && ownerLiveness && isSameClaim ( owner , reading . owner )
409- ? ownerLiveness
410- : 'unknown' ;
411- throw new AppError (
436+ return new AppError (
412437 'COMMAND_FAILED' ,
413438 `Timed out waiting for ${ params . description ?? 'process lock' } ` ,
414439 {
415440 lockDirPath : params . lockDirPath ,
416441 reason : 'process_lock_timeout' ,
417442 ...readProcessLockDiagnostics ( params . lockDirPath , reading , ownerLiveness ) ,
418- inspection : inspectProcessLockWith ( params . lockDirPath , judgeOwner ) ,
443+ inspection : inspectProcessLockWith (
444+ params . lockDirPath ,
445+ judgeOwnerFromReading ( reading , ownerLiveness ) ,
446+ ) ,
419447 hint : staleLockHint ( params . lockDirPath ) ,
420448 } ,
421449 ) ;
0 commit comments