Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Race condition in RPC filesystem cache. #7531

Merged
merged 4 commits into from
Mar 7, 2025
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
42 changes: 39 additions & 3 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { RuntimeRPC } from '../../types/rpc'
import type { TestProject } from '../project'
import type { ResolveSnapshotPathHandlerContext } from '../types/config'
import { mkdirSync } from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { join } from 'pathe'
import { rename, stat, unlink, writeFile } from 'node:fs/promises'
import { dirname, join } from 'pathe'
import { hash } from '../hash'

const created = new Set()
Expand Down Expand Up @@ -65,7 +65,11 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
}
promises.set(
tmp,
writeFile(tmp, code, 'utf-8').finally(() => promises.delete(tmp)),

atomicWriteFile(tmp, code)
// Fallback to non-atomic write for windows case where file already exists:
.catch(() => writeFile(tmp, code, 'utf-8'))
.finally(() => promises.delete(tmp)),
)
await promises.get(tmp)
Object.assign(result, { id: tmp })
Expand Down Expand Up @@ -146,3 +150,35 @@ function handleRollupError(e: unknown): never {
}
throw e
}

/**
* Performs an atomic write operation using the write-then-rename pattern.
*
* Why we need this:
* - Ensures file integrity by never leaving partially written files on disk
* - Prevents other processes from reading incomplete data during writes
* - Particularly important for test files where incomplete writes could cause test failures
*
* The implementation writes to a temporary file first, then renames it to the target path.
* This rename operation is atomic on most filesystems (including POSIX-compliant ones),
* guaranteeing that other processes will only ever see the complete file.
*
* Added in https://github.com/vitest-dev/vitest/pull/7531
*/
async function atomicWriteFile(realFilePath: string, data: string): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a few comments why we do this, and a link to this PR

const dir = dirname(realFilePath)
const tmpFilePath = join(dir, `.tmp-${Date.now()}-${Math.random().toString(36).slice(2)}`)

try {
await writeFile(tmpFilePath, data, 'utf-8')
await rename(tmpFilePath, realFilePath)
}
finally {
try {
if (await stat(tmpFilePath)) {
await unlink(tmpFilePath)
}
}
catch {}
}
}
Loading