fix: dispose backing FileStream on ExcelHandler/PowerPointHandler constructor failure - #224
Open
Marnie0415 wants to merge 1 commit into
Open
Conversation
…structor failure ExcelHandler and PowerPointHandler constructors create a backing FileStream but only catch OpenXmlPackageException (ExcelHandler) or no exceptions at all (PowerPointHandler). If PresentationDocument.Open or SpreadsheetDocument.Open fails for other reasons (IOException, UnauthorizedAccessException), the FileStream is never disposed — the file handle leaks until process exit. WordHandler already has the correct pattern: try/catch that disposes both _doc and _backingStream on failure. Apply the same cleanup to ExcelHandler (also disposing _filteredPackageStream for the bloat- filter path) and PowerPointHandler. Both handlers now use catch-all (matching WordHandler) so the original exception propagates directly — the factory's repair-and-retry paths (IsEncodingException, IsDanglingPartException) walk the exception chain and continue to work correctly.
Marnie0415
force-pushed
the
fix/handler-constructor-filestream-leak
branch
from
July 14, 2026 23:08
52bdc25 to
cf4162e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ExcelHandlerconstructor only catchesOpenXmlPackageException; other failures (IOException,UnauthorizedAccessException) leave_backingStreamundisposedPowerPointHandlerconstructor has no try/catch — any failure fromPresentationDocument.Openleaks the FileStreamWordHandleralready has the correct pattern: try/catch that disposes both_docand_backingStreamon failureRoot cause
When opening a corrupted or locked Office file, the SDK's
Openmethod may throw exceptions beyondOpenXmlPackageException. The factory's repair-and-retry paths (FixXmlEncoding/StripDanglingPackageRels) reopen the file for in-place fixes — but the leaked FileStream from the first attempt causes "file is being used by another process".Fix
Apply the same cleanup pattern as
WordHandler(line 342-350):_doc,_filteredPackageStream, and_backingStreamPresentationDocument.Open+InitShapeIdCounterin try/catch; dispose_docand_backingStreamBoth handlers now use catch-all (matching WordHandler) so the original exception propagates directly — the factory's
IsEncodingException/IsDanglingPartExceptionchecks walk the exception chain and continue to work correctly.Verification
Testing
Breaking Changes
None. The exception propagation path changes slightly (original exception rethrown directly instead of wrapped in
InvalidOperationException), but the factory's catch chain handles both forms correctly. End users see the samecorrupt_fileCLI error.Notes
WordHandlerpattern exactly (~27 lines across 2 files)