WeKnora/docreader/utils
林SO f307d3dc94
fix: remove temp file when TempFileContext write fails inside __enter__ (#3495)
* fix: remove temp file when TempFileContext write fails inside __enter__

TempFileContext.__enter__ creates a NamedTemporaryFile(delete=False) and then
calls write()/flush() with no error handling. If either raises (ENOSPC, quota,
any I/O error), the exception escapes __enter__, and Python's with-statement
never calls __exit__ for a context manager whose __enter__ failed. __exit__ is
the only place the file is closed and removed, so the file leaks permanently;
delete=False means GC will not remove it either.

doc_parser.py uses this to spill every uploaded .doc's full bytes to disk, so
one disk-pressure event leaves the whole document behind and makes the
pressure worse.

Wrap write/flush in try/except, close and remove the file, then re-raise.
Success path and the exception-inside-body path are unchanged.

Adds docreader/tests/test_tempfile_context.py covering the failure-injection
case (fails on the old code), the normal roundtrip, and body exceptions.

* fix: unlink temp file even if close() re-raises after ENOSPC

close() flushes again, so the ENOSPC path can skip os.remove and still leak.
Share one cleanup helper that always unlinks and preserves the original error.

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-21 14:21:07 +08:00
..
__init__.py chore: remove vendored archives and unused Python helpers 2026-09-16 15:49:36 +08:00
endecode.py fix: Improve image decoding by using original format if available 2025-12-03 15:25:46 +08:00
request.py feat: 新增文档模型类,调整配置与解析逻辑,优化日志及导入 2025-11-18 22:37:01 +08:00
split.py style: add necessary comments to improve code quality 2025-12-01 17:43:26 +08:00
ssrf.py fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
ssrf_proxy.py fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
tempfile.py fix: remove temp file when TempFileContext write fails inside __enter__ (#3495) 2026-09-21 14:21:07 +08:00