From a2a6d3abc29822ee94faf958e6a9acbe690c9770 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 17 Nov 2026 08:26:59 -0500 Subject: [PATCH] fix(io): close a nest file that fails to read NestReader opened the file and closed it only after a successful read, so a file that is not a nest, or a nest that failed part-way, stayed open until the garbage collector ran. On Windows that keeps the file from being renamed, replaced or deleted; the Desktop template-migration tests caught it in the Windows release build. Co-Authored-By: Claude Opus 5.5 --- .../NestReaderFileReleaseTests.cs | 44 +++++++++++++++++++ OpenNest.IO/NestReader.cs | 26 +++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 OpenNest.IO.Tests/NestReaderFileReleaseTests.cs diff --git a/OpenNest.IO.Tests/NestReaderFileReleaseTests.cs b/OpenNest.IO.Tests/NestReaderFileReleaseTests.cs new file mode 100644 index 00000000..5af97828 --- /dev/null +++ b/OpenNest.IO.Tests/NestReaderFileReleaseTests.cs @@ -0,0 +1,44 @@ +using System.IO.Compression; +using OpenNest.IO; + +namespace OpenNest.IO.Tests; + +/// +/// A file that fails to read must not stay open: on Windows an open handle keeps the user from +/// renaming, replacing or deleting it until the garbage collector runs. +/// +public sealed class NestReaderFileReleaseTests : IDisposable +{ + private readonly string directory = + Directory.CreateTempSubdirectory("opennest-reader-release-").FullName; + + public void Dispose() => Directory.Delete(directory, recursive: true); + + private static void AssertNotOpen(string path) + { + using var exclusive = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + } + + [Fact] + public void AFileThatIsNotANest_IsClosedWhenTheReaderRefusesIt() + { + var path = Path.Combine(directory, "broken.nest"); + File.WriteAllText(path, "not a nest"); + + Assert.ThrowsAny(() => new NestReader(path).Read()); + + AssertNotOpen(path); + } + + [Fact] + public void AnArchiveWithoutANest_IsClosedWhenReadFails() + { + var path = Path.Combine(directory, "empty.nest"); + using (var zip = ZipFile.Open(path, ZipArchiveMode.Create)) + zip.CreateEntry("readme.txt"); + + Assert.ThrowsAny(() => new NestReader(path).Read()); + + AssertNotOpen(path); + } +} diff --git a/OpenNest.IO/NestReader.cs b/OpenNest.IO/NestReader.cs index a3d51f59..9c84d6d0 100644 --- a/OpenNest.IO/NestReader.cs +++ b/OpenNest.IO/NestReader.cs @@ -48,7 +48,15 @@ namespace OpenNest.IO public NestReader(string file) { stream = new FileStream(file, FileMode.Open, FileAccess.Read); - zipArchive = new ZipArchive(stream, ZipArchiveMode.Read); + try + { + zipArchive = new ZipArchive(stream, ZipArchiveMode.Read); + } + catch + { + stream.Dispose(); + throw; + } } public NestReader(Stream stream) @@ -58,6 +66,19 @@ namespace OpenNest.IO } public Nest Read() + { + try + { + return ReadNest(); + } + finally + { + zipArchive.Dispose(); + stream.Close(); + } + } + + private Nest ReadNest() { var nestJson = ReadEntry("nest.json"); var dto = JsonSerializer.Deserialize(nestJson, JsonOptions); @@ -72,9 +93,6 @@ namespace OpenNest.IO // back to the drawing so removing lead-ins does not remove the marks too. DrawingMarkRecovery.Apply(nest); - zipArchive.Dispose(); - stream.Close(); - return nest; }