From 398fafb72d6ed6214c98d71cd5e6c5817194c242 Mon Sep 17 00:00:00 2001 From: cesnimda Date: Thu, 19 Nov 2026 20:57:15 +0100 Subject: [PATCH] fix(documents): clean up only snapshots created by the current copy (cherry picked from commit fa8154da9e3905d9486838b8e1a11cc6ba7a80db) --- .../SubmittedApplicationPackageTests.cs | 11 +++++++++++ JobTrackerApi/Services/AttachmentStorage.cs | 4 +++- docs/cv-integration-qa/PACKAGE_REVISION_CAPTURE.md | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/JobTrackerApi.Tests/SubmittedApplicationPackageTests.cs b/JobTrackerApi.Tests/SubmittedApplicationPackageTests.cs index b99bc76d..3bba389b 100644 --- a/JobTrackerApi.Tests/SubmittedApplicationPackageTests.cs +++ b/JobTrackerApi.Tests/SubmittedApplicationPackageTests.cs @@ -30,6 +30,17 @@ public sealed class SubmittedApplicationPackageTests Assert.Equal("exact submitted bytes", await File.ReadAllTextAsync(destination)); Assert.Equal("32283fd0838392ef9d8c707f521096966432939d5d83624bbfce150cde3ce26c", result.Sha256); + + await File.WriteAllTextAsync(source, "a later draft"); + await Assert.ThrowsAsync(() => storage.SnapshotAsync(source, destination, default)); + Assert.Equal("exact submitted bytes", await File.ReadAllTextAsync(destination)); + + var next = storage.CreatePackagePath(7, 2, "source.txt"); + await Assert.ThrowsAnyAsync(() => + storage.SnapshotAsync(source, next, new CancellationToken(canceled: true))); + Assert.False(File.Exists(next)); + await storage.SnapshotAsync(source, next, default); + Assert.Equal("a later draft", await File.ReadAllTextAsync(next)); } finally { diff --git a/JobTrackerApi/Services/AttachmentStorage.cs b/JobTrackerApi/Services/AttachmentStorage.cs index ff997a32..619ceefe 100644 --- a/JobTrackerApi/Services/AttachmentStorage.cs +++ b/JobTrackerApi/Services/AttachmentStorage.cs @@ -92,10 +92,12 @@ public sealed class AttachmentStorage : IAttachmentStorage var destination = EnsureManagedPath(destinationPath, allowMissingLeaf: true); if (File.Exists(destination)) throw new IOException("A submitted package file already exists at the destination."); + var created = false; try { await using var input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); await using var output = new FileStream(destination, FileMode.CreateNew, FileAccess.Write, FileShare.None); + created = true; using var hash = System.Security.Cryptography.IncrementalHash.CreateHash(System.Security.Cryptography.HashAlgorithmName.SHA256); var buffer = new byte[81920]; int read; @@ -111,7 +113,7 @@ public sealed class AttachmentStorage : IAttachmentStorage } catch { - if (File.Exists(destination)) File.Delete(destination); + if (created) File.Delete(destination); throw; } } diff --git a/docs/cv-integration-qa/PACKAGE_REVISION_CAPTURE.md b/docs/cv-integration-qa/PACKAGE_REVISION_CAPTURE.md index 5f1b19ad..c287f7fb 100644 --- a/docs/cv-integration-qa/PACKAGE_REVISION_CAPTURE.md +++ b/docs/cv-integration-qa/PACKAGE_REVISION_CAPTURE.md @@ -13,3 +13,9 @@ The first build could not execute tests because C: was full. Only this run's fre Remaining investigation:concurrent Career Profile changes during render may require their own snapshot binding. AttachmentStorage.SnapshotAsync has a separate source-backed destination-ownership cleanup concern; no deterministic race reproduction or fix is claimed here. MariaDB, actual HTTP/browser capture, real models/providers, native device/AT, physical PDF acceptance, integrated security and recovery were not rerun. Existing accepted PDF metrics/visuals remain source-bound. Rollback:revert the source commit, rebuild on the isolated development branch and rerun the affected suite. No migration/data repair is involved. UI callers retain the existing revision-conflict/review flow; an aborted capture publishes no package. This does not establish atomic consistency across every package source entity or filesystem race. + +## Submitted attachment cleanup ownership + +On 19 November, review of `AttachmentStorage.SnapshotAsync` found that its exception path treated the presence of a file as permission to delete it. The destination may already have been claimed by another writer. The snapshotter now records success of its own exclusive `FileMode.CreateNew` open. Only that invocation's partial destination is eligible for cleanup. The existing early duplicate rejection and error message are preserved. + +The storage lifecycle test uses real files under a disposable fixture root. It verifies the captured bytes and hash, a refused duplicate following a change to the source, preservation of the first capture, cancellation cleanup and a successful retry into the second destination. All 29 selected submitted-package, attachment consistency and attachment-controller tests passed on the dedicated Release build. The package service still records a path for outer rollback only after a successful snapshot. No customer data, migration or exporter was involved. There was no forced two-writer interleaving or live store rehearsal.