Skip to content

Commit 5266965

Browse files
committed
fix(lock): use recursive mkdir and add explicit existence check
- Changed mkdirSync to use recursive: true to create parent directories - Added explicit existsSync check before mkdir since recursive mode doesn't throw EEXIST - Updated test expectations to handle both error types from pRetry wrapping - Fixed deeply nested path test to expect success with recursive mode
1 parent 26f38e5 commit 5266965

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

src/process-lock.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,13 @@ class ProcessLockManager {
256256
}
257257
}
258258

259-
// Atomic lock acquisition via mkdir.
260-
mkdirSync(lockPath, { recursive: false })
259+
// Check if lock already exists before creating.
260+
if (existsSync(lockPath)) {
261+
throw new Error(`Lock already exists: ${lockPath}`)
262+
}
263+
264+
// Atomic lock acquisition via mkdir with recursive to create parent dirs.
265+
mkdirSync(lockPath, { recursive: true })
261266

262267
// Track lock for cleanup.
263268
this.activeLocks.add(lockPath)

test/process-lock.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe.sequential('process-lock', () => {
4949
// Second acquire should fail
5050
await expect(
5151
processLock.acquire(testLockPath, { retries: 1, baseDelayMs: 10 }),
52-
).rejects.toThrow(/Lock already exists/)
52+
).rejects.toThrow(/Lock already exists|Failed to acquire lock/)
5353

5454
release1()
5555
})
@@ -246,7 +246,7 @@ describe.sequential('process-lock', () => {
246246
baseDelayMs: 10,
247247
staleMs: 10_000,
248248
}),
249-
).rejects.toThrow(/Lock already exists/)
249+
).rejects.toThrow(/Lock already exists|Failed to acquire lock/)
250250

251251
release()
252252
})
@@ -308,13 +308,11 @@ describe.sequential('process-lock', () => {
308308
'path',
309309
)
310310

311-
// Should work even with nested path
312-
await expect(
313-
processLock.acquire(deepPath, { retries: 1 }),
314-
).rejects.toThrow()
315-
316-
// mkdir with recursive: false should fail for non-existent parent
317-
// This is expected behavior
311+
// Should work with nested path (recursive: true creates parent dirs)
312+
const release = await processLock.acquire(deepPath, { retries: 1 })
313+
expect(existsSync(deepPath)).toBe(true)
314+
release()
315+
expect(existsSync(deepPath)).toBe(false)
318316
})
319317
})
320318
})

0 commit comments

Comments
 (0)