diff --git a/src/officecli/Handlers/ExcelHandler.cs b/src/officecli/Handlers/ExcelHandler.cs index 3cd25716e..f07cd227d 100644 --- a/src/officecli/Handlers/ExcelHandler.cs +++ b/src/officecli/Handlers/ExcelHandler.cs @@ -96,10 +96,17 @@ public ExcelHandler(string filePath, bool editable) .Where(n => !string.IsNullOrEmpty(n)) ?? Enumerable.Empty(), StringComparer.OrdinalIgnoreCase); } - catch (DocumentFormat.OpenXml.Packaging.OpenXmlPackageException ex) + catch { - throw new InvalidOperationException( - $"Cannot open {Path.GetFileName(filePath)}: {ex.Message}", ex); + // A failed open must not leak the backing FileStream — the + // factory's repair-and-retry paths (FixXmlEncoding / + // StripDanglingPackageRels) reopen the file for in-place fixes + // and would hit "file is being used by another process". + // Matches the cleanup pattern in WordHandler's constructor. + _doc?.Dispose(); + _filteredPackageStream?.Dispose(); + _backingStream?.Dispose(); + throw; } } diff --git a/src/officecli/Handlers/PowerPointHandler.cs b/src/officecli/Handlers/PowerPointHandler.cs index 857907231..62eec0a0d 100644 --- a/src/officecli/Handlers/PowerPointHandler.cs +++ b/src/officecli/Handlers/PowerPointHandler.cs @@ -115,9 +115,23 @@ public PowerPointHandler(string filePath, bool editable) var share = editable ? FileShare.Read : FileShare.ReadWrite; var access = editable ? FileAccess.ReadWrite : FileAccess.Read; _backingStream = new FileStream(filePath, FileMode.Open, access, share); - _doc = PresentationDocument.Open(_backingStream, editable); - if (editable) - InitShapeIdCounter(); + try + { + _doc = PresentationDocument.Open(_backingStream, editable); + if (editable) + InitShapeIdCounter(); + } + catch + { + // A failed open must not leak the backing FileStream — the + // factory's repair-and-retry paths (FixXmlEncoding / + // StripDanglingPackageRels) reopen the file for in-place fixes + // and would hit "file is being used by another process". + // Matches the cleanup pattern in WordHandler's constructor. + _doc?.Dispose(); + _backingStream?.Dispose(); + throw; + } } ///