Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/__tests__/helpers/createTestDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,32 @@ export async function createTestDb(): Promise<TestDb> {
// Close before unlinking: the file, and its WAL/SHM siblings, stay
// locked on Windows while the handle is open.
await db.close()
await fs.rm(path.dirname(tmpFile), { recursive: true, force: true })
await rmWithRetry(path.dirname(tmpFile))
},
}
}

/**
* Remove a test DB directory, retrying EBUSY with exponential backoff.
*
* On Windows the OS can keep the WAL/SHM siblings locked for a while after
* the last SQLite handle is closed — the release is asynchronous, and
* real-time antivirus scanners may hold a freshly written file for seconds
* (observed up to ~5s on CI workstations). Every handle in the process is
* already closed, so retrying is safe and turns a flaky teardown into a
* clean one; on POSIX the first attempt always succeeds.
*/
async function rmWithRetry(dir: string): Promise<void> {
const deadline = Date.now() + 15_000
let waitMs = 100
for (;;) {
try {
await fs.rm(dir, { recursive: true, force: true })
return
} catch (err) {
if ((err as NodeJS.ErrnoException)?.code !== 'EBUSY' || Date.now() + waitMs > deadline) throw err
await new Promise((resolve) => setTimeout(resolve, waitMs))
waitMs = Math.min(waitMs * 2, 1000)
}
}
}
Loading