Skip to content

fix(utils): let a temp-dir cleanup failure keep the error that caused it - #5864

Open
gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/tempdir-cleanup-keeps-the-body-error
Open

gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/tempdir-cleanup-keeps-the-body-error

Conversation

@gouyu-hou

Copy link
Copy Markdown
Contributor

Observed

TempDirContext.__exit__ calls TemporaryDirectory.cleanup() without a guard. When cleanup cannot finish, its exception replaces the one the with body raised. On Windows this happens as soon as a reader still holds a file inside the directory, which rmtree cannot unlink:

$ python -c "from docreader.utils.tempfile import TempDirContext; ..."
raised: PermissionError [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\Users\...\Temp\tmp0x70zk34\a.bin'
dir still there: True

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._cleanup already documents in this same module ("cleanup errors must not replace the original exception").

Root cause

docreader/utils/tempfile.py: TempDirContext.__exit__ runs self.temp_dir.cleanup() inside the with teardown with no try. CPython's TemporaryDirectory.cleanup() propagates rmtree failures, and on Windows unlinking an open file raises PermissionError([WinError 32]). doc_parser.py:274 wraps the LibreOffice .doc conversion in two TempDirContexts, so the failure lands on the parse path.

Fix

Catch OSError from cleanup(), 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):

FAILED tests/test_tempfile_context.py::TempDirContextTest::test_a_reader_holding_a_file_does_not_replace_the_body_error
FAILED tests/test_tempfile_context.py::TempDirContextTest::test_cleanup_failure_does_not_replace_the_body_error
2 failed, 5 passed in 0.19s

After:

$ python -m pytest docreader/tests/test_tempfile_context.py -q
7 passed in 0.05s

Affected surface (everything that imports docreader.parser.doc_parser):

$ python -m pytest docreader/tests/test_docx_tables.py -q        # 19 passed
$ python -m pytest docreader/tests/test_parser_routing.py -q      # 10 passed
$ python -m pytest docreader/tests/test_sandbox_executor.py -q    # 1 passed, 7 skipped
$ python -m compileall -q docreader/utils/tempfile.py             # no output

Copilot AI balanced review requested due to automatic review settings November 23, 2026 07:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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
gouyu-hou force-pushed the fix/tempdir-cleanup-keeps-the-body-error branch from 4f1ad55 to 627e71d Compare November 28, 2026 09:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants