@@ -606,6 +606,116 @@ describe("withFileLock", () => {
606606 expect ( await fs . readdir ( dir ) ) . toEqual ( [ "memory.jsonl.lock" ] ) ;
607607 } ) ;
608608
609+ // Stage a successor's live lock in place of the stale one before the
610+ // breaker takes it aside, as onceBeforeRemoval does, and fail the first
611+ // `failures` attempts to put it back with `code`. Returns a count of
612+ // those attempts.
613+ function stageSuccessorAndFailRestore (
614+ failures : number ,
615+ code : string ,
616+ ) : ( ) => number {
617+ const realRename = fs . rename ;
618+ let staged = false ;
619+ let restores = 0 ;
620+ vi . spyOn ( fs , "rename" ) . mockImplementation ( async ( from , to ) => {
621+ if ( ! staged && from === lockPath ) {
622+ staged = true ;
623+ await fs . unlink ( lockPath ) ;
624+ await fs . writeFile ( lockPath , "successor" ) ;
625+ }
626+ if ( String ( from ) . endsWith ( ".stale" ) && to === lockPath ) {
627+ restores += 1 ;
628+ if ( restores <= failures ) {
629+ throw Object . assign ( new Error ( code ) , { code } ) ;
630+ }
631+ }
632+ return realRename ( from , to ) ;
633+ } ) ;
634+ return ( ) => restores ;
635+ }
636+
637+ // On Windows a rename over a file that anything holds open fails, and the
638+ // writer that created the lock in the gap keeps opening it while it waits
639+ // out the break. Giving up then left the live lock aside and let that
640+ // writer run beside its holder (#5635).
641+ it ( "keeps putting a live lock back while Windows refuses to replace the lock created in the gap" , async ( ) => {
642+ await writeStaleLock ( ) ;
643+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
644+ vi . spyOn ( process , "platform" , "get" ) . mockReturnValue ( "win32" ) ;
645+ const opens = watchOpens ( ) ;
646+ const restores = stageSuccessorAndFailRestore ( 2 , "EPERM" ) ;
647+ let thirdRan ! : ( ) => void ;
648+ const thirdStarted = new Promise < void > ( ( resolve ) => ( thirdRan = resolve ) ) ;
649+ const third = vi . fn ( async ( ) => {
650+ thirdRan ( ) ;
651+ return "third ran" ;
652+ } ) ;
653+ let thirdOutcome : Promise < unknown > | undefined ;
654+ onceLockAside ( async ( ) => {
655+ const sawBreak = opens . opened ( guardPath ( ) , "r" ) ;
656+ thirdOutcome = withFileLock ( lockPath , third , fast ) . catch (
657+ ( error : unknown ) => error ,
658+ ) ;
659+ await Promise . race ( [ thirdStarted , sawBreak ] ) ;
660+ } ) ;
661+
662+ await expect (
663+ withFileLock ( lockPath , async ( ) => "breaker ran" , fast ) ,
664+ ) . rejects . toThrow ( "Timed out" ) ;
665+ const outcome = await thirdOutcome ;
666+
667+ expect ( restores ( ) ) . toBe ( 3 ) ;
668+ expect ( third ) . not . toHaveBeenCalled ( ) ;
669+ expect ( ( outcome as Error ) . message ) . toContain ( "Timed out" ) ;
670+ expect ( await fs . readFile ( lockPath , "utf-8" ) ) . toBe ( "successor" ) ;
671+ expect ( heldLockPaths ( ) ) . toEqual ( [ ] ) ;
672+ expect ( await fs . readdir ( dir ) ) . toEqual ( [ "memory.jsonl.lock" ] ) ;
673+ } ) ;
674+
675+ it ( "does not retry a failed put-back off Windows, where the failure lasts" , async ( ) => {
676+ await writeStaleLock ( ) ;
677+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
678+ vi . spyOn ( process , "platform" , "get" ) . mockReturnValue ( "linux" ) ;
679+ const restores = stageSuccessorAndFailRestore ( 1 , "EPERM" ) ;
680+
681+ await expect (
682+ withFileLock ( lockPath , async ( ) => "ran" , fast ) ,
683+ ) . rejects . toThrow ( "EPERM" ) ;
684+
685+ expect ( restores ( ) ) . toBe ( 1 ) ;
686+ expect ( heldLockPaths ( ) ) . toEqual ( [ ] ) ;
687+ expect ( await exists ( guardPath ( ) ) ) . toBe ( false ) ;
688+ } ) ;
689+
690+ // Windows' EPERM also means a busy lock to the acquire loop, so a put-back
691+ // that never lands must fail with an error of its own, or the breaker
692+ // would go back to acquiring with the live lock still aside.
693+ it ( "fails the acquisition on Windows once a live lock cannot be put back before the guard would be taken for a crash's" , async ( ) => {
694+ await writeStaleLock ( ) ;
695+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
696+ vi . spyOn ( process , "platform" , "get" ) . mockReturnValue ( "win32" ) ;
697+ const restores = stageSuccessorAndFailRestore ( Infinity , "EPERM" ) ;
698+ const operation = vi . fn ( async ( ) => "ran" ) ;
699+
700+ const error : unknown = await withFileLock ( lockPath , operation , {
701+ staleMs : 50 ,
702+ timeoutMs : 5_000 ,
703+ retryMs : 5 ,
704+ } ) . catch ( ( e : unknown ) => e ) ;
705+
706+ expect ( error ) . toBeInstanceOf ( Error ) ;
707+ expect ( ( error as Error ) . message ) . toContain (
708+ "Could not put back the live memory file lock" ,
709+ ) ;
710+ expect ( ( ( error as Error ) . cause as NodeJS . ErrnoException ) . code ) . toBe (
711+ "EPERM" ,
712+ ) ;
713+ expect ( operation ) . not . toHaveBeenCalled ( ) ;
714+ expect ( restores ( ) ) . toBeGreaterThan ( 1 ) ;
715+ expect ( heldLockPaths ( ) ) . toEqual ( [ ] ) ;
716+ expect ( await exists ( guardPath ( ) ) ) . toBe ( false ) ;
717+ } ) ;
718+
609719 it ( "does not restore a lock its holder released while it was aside" , async ( ) => {
610720 await writeStaleLock ( ) ;
611721 vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
0 commit comments