From d5f8be355df7c12c1b9c2ecde2fdeb9179402d2f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 30 Jul 2026 00:40:20 -0700 Subject: [PATCH] fix(sqlite): preserve replaced snapshot targets on Windows (#116284) --- src/infra/sqlite-snapshot.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/infra/sqlite-snapshot.ts b/src/infra/sqlite-snapshot.ts index 715e186dbac3..0253944986b6 100644 --- a/src/infra/sqlite-snapshot.ts +++ b/src/infra/sqlite-snapshot.ts @@ -437,7 +437,6 @@ export async function publishVerifiedSqliteFile( let targetPinFileDescriptor: number | undefined; let failedPublicationIdentity: FileIdentityStat | undefined; let publishedIdentity: Stats | undefined; - let ownershipPinned = false; try { stagingIdentity = await fs.lstat(stagingDir); await fs.chmod(stagingDir, 0o700); @@ -521,7 +520,6 @@ export async function publishVerifiedSqliteFile( const initialPublishedIdentity = publishedIdentity; target = await fs.open(options.targetPath, "r"); await assertOpenFileIdentity(target, options.targetPath, initialPublishedIdentity); - ownershipPinned = true; // Retire the writable staging hard link before the final byte verification. await fs.unlink(stagedPath); const expectedIdentity = await target.stat(); @@ -539,10 +537,8 @@ export async function publishVerifiedSqliteFile( ); await target.close(); target = undefined; - ownershipPinned = false; targetPinFileDescriptor = fsSync.openSync(options.targetPath, "r"); assertOpenFileIdentitySync(targetPinFileDescriptor, options.targetPath, expectedIdentity); - ownershipPinned = true; const guard: PublishedSqliteFileGuard = { assertTargetMatchesExpectedContent: (finalCheck) => { @@ -567,22 +563,18 @@ export async function publishVerifiedSqliteFile( } fsSync.closeSync(targetPinFileDescriptor); targetPinFileDescriptor = undefined; - ownershipPinned = false; } catch (error) { if (target && publishedIdentity) { const openedIdentity = await target.stat().catch(() => undefined); if (openedIdentity && sameFileIdentity(openedIdentity, publishedIdentity)) { publishedIdentity = openedIdentity; - ownershipPinned = true; } } const cleanupIdentity = publishedIdentity ?? failedPublicationIdentity; if (cleanupIdentity) { - const removed = removePublishedTargetIfOwned( - options.targetPath, - cleanupIdentity, - !ownershipPinned, - ); + // Windows can reuse a deleted file's identity while our old handle is still pinned. + // Require the full fingerprint so cleanup never unlinks a caller replacement. + const removed = removePublishedTargetIfOwned(options.targetPath, cleanupIdentity, true); if (removed) { await syncDirectory(targetDirectoryReceipt).catch(() => undefined); }