Skip to content

Commit d5d3256

Browse files
committed
fix(sqlite): preserve replaced snapshot targets on Windows
1 parent ad1dc60 commit d5d3256

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

‎src/infra/sqlite-snapshot.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ export async function publishVerifiedSqliteFile(
437437
let targetPinFileDescriptor: number | undefined;
438438
let failedPublicationIdentity: FileIdentityStat | undefined;
439439
let publishedIdentity: Stats | undefined;
440-
let ownershipPinned = false;
441440
try {
442441
stagingIdentity = await fs.lstat(stagingDir);
443442
await fs.chmod(stagingDir, 0o700);
@@ -521,7 +520,6 @@ export async function publishVerifiedSqliteFile(
521520
const initialPublishedIdentity = publishedIdentity;
522521
target = await fs.open(options.targetPath, "r");
523522
await assertOpenFileIdentity(target, options.targetPath, initialPublishedIdentity);
524-
ownershipPinned = true;
525523
// Retire the writable staging hard link before the final byte verification.
526524
await fs.unlink(stagedPath);
527525
const expectedIdentity = await target.stat();
@@ -539,10 +537,8 @@ export async function publishVerifiedSqliteFile(
539537
);
540538
await target.close();
541539
target = undefined;
542-
ownershipPinned = false;
543540
targetPinFileDescriptor = fsSync.openSync(options.targetPath, "r");
544541
assertOpenFileIdentitySync(targetPinFileDescriptor, options.targetPath, expectedIdentity);
545-
ownershipPinned = true;
546542

547543
const guard: PublishedSqliteFileGuard = {
548544
assertTargetMatchesExpectedContent: (finalCheck) => {
@@ -567,22 +563,18 @@ export async function publishVerifiedSqliteFile(
567563
}
568564
fsSync.closeSync(targetPinFileDescriptor);
569565
targetPinFileDescriptor = undefined;
570-
ownershipPinned = false;
571566
} catch (error) {
572567
if (target && publishedIdentity) {
573568
const openedIdentity = await target.stat().catch(() => undefined);
574569
if (openedIdentity && sameFileIdentity(openedIdentity, publishedIdentity)) {
575570
publishedIdentity = openedIdentity;
576-
ownershipPinned = true;
577571
}
578572
}
579573
const cleanupIdentity = publishedIdentity ?? failedPublicationIdentity;
580574
if (cleanupIdentity) {
581-
const removed = removePublishedTargetIfOwned(
582-
options.targetPath,
583-
cleanupIdentity,
584-
!ownershipPinned,
585-
);
575+
// Windows can reuse a deleted file's identity while our old handle is still pinned.
576+
// Require the full fingerprint so cleanup never unlinks a caller replacement.
577+
const removed = removePublishedTargetIfOwned(options.targetPath, cleanupIdentity, true);
586578
if (removed) {
587579
await syncDirectory(targetDirectoryReceipt).catch(() => undefined);
588580
}

0 commit comments

Comments
 (0)