@@ -6,6 +6,7 @@ import { publishFileSync } from './atomic-file.ts';
66import { classifyOwnerLiveness , ownerIdentityMatches } from './owner-identity.ts' ;
77import { sleep } from './timeouts.ts' ;
88
9+ const OWNER_FILE_NAME = 'owner.json' ;
910const DEFAULT_LOCK_TIMEOUT_MS = 30_000 ;
1011const DEFAULT_LOCK_POLL_MS = 100 ;
1112const DEFAULT_LOCK_OWNER_GRACE_MS = 5_000 ;
@@ -31,7 +32,7 @@ export async function acquireProcessLock(params: {
3132 description ?: string ;
3233} ) : Promise < ( ) => Promise < void > > {
3334 const { lockDirPath, owner } = params ;
34- const ownerFilePath = path . join ( lockDirPath , 'owner.json' ) ;
35+ const ownerFilePath = path . join ( lockDirPath , OWNER_FILE_NAME ) ;
3536 const deadline = Date . now ( ) + ( params . timeoutMs ?? DEFAULT_LOCK_TIMEOUT_MS ) ;
3637 const pollMs = params . pollMs ?? DEFAULT_LOCK_POLL_MS ;
3738 const ownerGraceMs = params . ownerGraceMs ?? DEFAULT_LOCK_OWNER_GRACE_MS ;
@@ -124,7 +125,7 @@ function clearStaleProcessLock(
124125 // owner record, so its age is the only evidence available about it.
125126 if ( ! lockStats . isDirectory ( ) ) {
126127 return reclaimWhenAbandoned ( lockStats , ownerGraceMs )
127- ? reclaimProcessLockDirectory ( lockDirPath , ownerFilePath )
128+ ? reclaimProcessLockDirectory ( lockDirPath , ownerFilePath , lockStats )
128129 : false ;
129130 }
130131
@@ -133,7 +134,7 @@ function clearStaleProcessLock(
133134 if ( isLiveProcessLockOwner ( reading . owner ) ) {
134135 return false ;
135136 }
136- return reclaimProcessLockDirectory ( lockDirPath , ownerFilePath ) ;
137+ return reclaimProcessLockDirectory ( lockDirPath , ownerFilePath , lockStats ) ;
137138 }
138139 // A record we cannot read leaves an owner whose identity is unknown, which is not
139140 // evidence of death. Only a record that is genuinely absent lets the directory's
@@ -142,7 +143,7 @@ function clearStaleProcessLock(
142143 return false ;
143144 }
144145 return reclaimWhenAbandoned ( lockStats , ownerGraceMs )
145- ? reclaimProcessLockDirectory ( lockDirPath , ownerFilePath )
146+ ? reclaimProcessLockDirectory ( lockDirPath , ownerFilePath , lockStats )
146147 : false ;
147148}
148149
@@ -156,30 +157,75 @@ function reclaimWhenAbandoned(lockStats: fs.Stats, ownerGraceMs: number): boolea
156157 * the caller's side: the second succeeds silently on the path the first already
157158 * cleared, and both contenders continue as though they had freed the lock. `mkdir`'s
158159 * `EEXIST`, unchanged, stays the arbiter of the lock itself.
159- *
160- * A win32 directory with a handle open inside it refuses the rename and often the
161- * removal too, which is why a forced removal remains as the fallback: stale-clear
162- * atomicity is best effort there and exact elsewhere. The fallback re-reads the
163- * record first, because a refused rename means time passed and a live contender may
164- * have claimed the path in it.
165160 */
166- function reclaimProcessLockDirectory ( lockDirPath : string , ownerFilePath : string ) : boolean {
161+ function reclaimProcessLockDirectory (
162+ lockDirPath : string ,
163+ ownerFilePath : string ,
164+ judged : fs . Stats ,
165+ ) : boolean {
167166 const asidePath = reclaimedLockPath ( lockDirPath ) ;
168167 try {
169168 fs . renameSync ( lockDirPath , asidePath ) ;
170169 } catch ( error ) {
171- const code = ( error as NodeJS . ErrnoException | null ) ?. code ;
172- if ( code === 'ENOENT' ) return true ;
173- if ( code !== 'EPERM' && code !== 'EACCES' && code !== 'ENOTEMPTY' ) return false ;
174- const reading = readProcessLockOwner ( ownerFilePath ) ;
175- if ( reading . kind === 'owner' && isLiveProcessLockOwner ( reading . owner ) ) return false ;
176- fs . rmSync ( lockDirPath , { recursive : true , force : true } ) ;
177- return true ;
170+ return reclaimWithoutTheRename ( lockDirPath , ownerFilePath , error ) ;
171+ }
172+ if ( ! reclaimedLockIsTheOneJudged ( asidePath , judged ) ) {
173+ restoreReclaimedLockDirectory ( asidePath , lockDirPath ) ;
174+ return false ;
178175 }
179176 removeReclaimedLockDirectory ( asidePath ) ;
180177 return true ;
181178}
182179
180+ /**
181+ * A win32 directory with a handle open inside it refuses the rename and often the removal
182+ * too, which is why a forced removal remains as the fallback: stale-clear atomicity is
183+ * best effort there and exact elsewhere. The fallback re-reads the record first, because a
184+ * refused rename means time passed and a live contender may have claimed the path in it.
185+ */
186+ function reclaimWithoutTheRename (
187+ lockDirPath : string ,
188+ ownerFilePath : string ,
189+ renameError : unknown ,
190+ ) : boolean {
191+ const code = ( renameError as NodeJS . ErrnoException | null ) ?. code ;
192+ if ( code === 'ENOENT' ) return true ;
193+ if ( code !== 'EPERM' && code !== 'EACCES' && code !== 'ENOTEMPTY' ) return false ;
194+ const reading = readProcessLockOwner ( ownerFilePath ) ;
195+ if ( reading . kind === 'owner' && isLiveProcessLockOwner ( reading . owner ) ) return false ;
196+ fs . rmSync ( lockDirPath , { recursive : true , force : true } ) ;
197+ return true ;
198+ }
199+
200+ /**
201+ * A rename addresses whatever stands at the path now, not the directory whose record was
202+ * read. A contender that reclaimed first and published a live owner in the meantime has
203+ * put a different directory there, so what arrived is compared against what was judged:
204+ * the same inode, and no live owner inside.
205+ */
206+ function reclaimedLockIsTheOneJudged ( asidePath : string , judged : fs . Stats ) : boolean {
207+ let moved : fs . Stats ;
208+ try {
209+ moved = fs . statSync ( asidePath ) ;
210+ } catch {
211+ return false ;
212+ }
213+ if ( moved . ino !== judged . ino || moved . dev !== judged . dev ) return false ;
214+ const reading = readProcessLockOwner ( path . join ( asidePath , OWNER_FILE_NAME ) ) ;
215+ return ! ( reading . kind === 'owner' && isLiveProcessLockOwner ( reading . owner ) ) ;
216+ }
217+
218+ /**
219+ * Returns a directory that turned out to belong to someone else. Failure is not a
220+ * licence to delete it: its holder's own release reports an unreadable owner rather than
221+ * let this process clear a lock it does not own.
222+ */
223+ function restoreReclaimedLockDirectory ( asidePath : string , lockDirPath : string ) : void {
224+ try {
225+ fs . renameSync ( asidePath , lockDirPath ) ;
226+ } catch { }
227+ }
228+
183229/** Janitorial work after the rename already achieved the exclusion. */
184230function removeReclaimedLockDirectory ( asidePath : string ) : void {
185231 try {
0 commit comments