Repository navigation
Conversation
TempDirContext.__exit__ called TemporaryDirectory.cleanup() unguarded, so a cleanup that fails replaced whatever the with-body raised. On Windows that is routine: rmtree cannot unlink a file another handle still has open, and it raises PermissionError([WinError 32]), so a parse error surfaced as "the process cannot access the file" while the directory stayed behind. Log the cleanup failure as a warning and keep the body's exception, as TempFileContext._cleanup already does.
gouyu-hou
force-pushed
the
fix/tempdir-cleanup-keeps-the-body-error
branch
from
November 28, 2026 09:38
4f1ad55 to
627e71d
Compare
This branch has not been deployed
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.
Observed
TempDirContext.__exit__callsTemporaryDirectory.cleanup()without a guard. When cleanup cannot finish, its exception replaces the one thewithbody raised. On Windows this happens as soon as a reader still holds a file inside the directory, whichrmtreecannot unlink:The
ValueError("parse failed")the body raised is gone: the caller reads "the process cannot access the file" and has no way to tell what actually failed. The temporary directory also stays behind.Expected
The error the body raised is the one the caller sees, and a cleanup that fails is reported instead of thrown — the rule
TempFileContext._cleanupalready documents in this same module ("cleanup errors must not replace the original exception").Root cause
docreader/utils/tempfile.py:TempDirContext.__exit__runsself.temp_dir.cleanup()inside thewithteardown with notry. CPython'sTemporaryDirectory.cleanup()propagatesrmtreefailures, and on Windows unlinking an open file raisesPermissionError([WinError 32]).doc_parser.py:274wraps the LibreOffice.docconversion in twoTempDirContexts, so the failure lands on the parse path.Fix
Catch
OSErrorfromcleanup(), log it as a warning with the directory, and let the body's exception through. On hosts where the unlink is allowed nothing changes.Reproduction and verification
Test added in
docreader/tests/test_tempfile_context.py, run on Windows 11 / Python 3.13:Before (fix reverted, new tests only):
After:
Affected surface (everything that imports
docreader.parser.doc_parser):