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>
main
林SO 2026-09-21 14:21:07 +08:00 committed by GitHub
parent 4a9aa06274
commit f307d3dc94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 119 additions and 7 deletions

View File

@ -0,0 +1,89 @@
import os
import tempfile
import unittest
from unittest.mock import patch
from docreader.utils.tempfile import TempFileContext
class TempFileContextTest(unittest.TestCase):
def test_removes_file_when_write_fails_inside_enter(self):
created = []
real_ntf = tempfile.NamedTemporaryFile
def failing_ntf(*args, **kwargs):
f = real_ntf(*args, **kwargs)
created.append(f.name)
class Wrapper:
def __getattr__(self, name):
return getattr(f, name)
def write(self, data):
raise OSError(28, "No space left on device")
return Wrapper()
with patch("docreader.utils.tempfile.tempfile.NamedTemporaryFile", failing_ntf):
with self.assertRaises(OSError):
with TempFileContext(b"payload", ".doc"):
pass
self.assertEqual(len(created), 1)
self.assertFalse(os.path.exists(created[0]))
def test_removes_file_when_close_reraises_after_flush_fails(self):
# CPython NamedTemporaryFile.close() flushes again; after ENOSPC,
# close() typically re-raises and must not skip unlink or replace
# the original write/flush error.
created = []
real_ntf = tempfile.NamedTemporaryFile
def failing_ntf(*args, **kwargs):
f = real_ntf(*args, **kwargs)
created.append(f.name)
class Wrapper:
def __init__(self):
self.name = f.name
def write(self, data):
f.write(data)
def flush(self):
raise OSError(28, "No space left on device")
def close(self):
try:
raise OSError(5, "Input/output error")
finally:
f.close()
return Wrapper()
with patch("docreader.utils.tempfile.tempfile.NamedTemporaryFile", failing_ntf):
with self.assertRaises(OSError) as ctx:
with TempFileContext(b"payload", ".doc"):
pass
self.assertEqual(ctx.exception.errno, 28)
self.assertEqual(len(created), 1)
self.assertFalse(os.path.exists(created[0]))
def test_normal_roundtrip_and_cleanup(self):
payload = bytes(range(256)) * 4
with TempFileContext(payload, ".doc") as path:
self.assertTrue(path.endswith(".doc"))
with open(path, "rb") as f:
self.assertEqual(f.read(), payload)
self.assertFalse(os.path.exists(path))
def test_exception_inside_body_still_cleans_up(self):
with self.assertRaises(ValueError):
with TempFileContext(b"x", ".doc") as path:
raise ValueError("body")
self.assertFalse(os.path.exists(path))
if __name__ == "__main__":
unittest.main()

View File

@ -16,13 +16,37 @@ class TempFileContext:
self.suffix = suffix
self.file = None
def _cleanup(self):
"""Close and unlink the temp file. close() may re-raise a prior flush
error (ENOSPC), so unlink must still run and cleanup errors must not
replace the original exception.
"""
temp_file = getattr(self, "temp_file", None)
if not temp_file:
return
try:
temp_file.close()
except OSError:
pass
try:
os.remove(temp_file.name)
except OSError:
pass
def __enter__(self):
"""
Create file when entering context
"""
self.temp_file = tempfile.NamedTemporaryFile(suffix=self.suffix, delete=False)
self.temp_file.write(self.file_content)
self.temp_file.flush()
# If write/flush fails (e.g. ENOSPC), __exit__ is never called because
# the exception escapes __enter__, so clean up here or the file leaks
# permanently (delete=False means GC will not remove it either).
try:
self.temp_file.write(self.file_content)
self.temp_file.flush()
except BaseException:
self._cleanup()
raise
logger.info(
f"Saved {self.suffix} content to temporary file: {self.temp_file.name}"
)
@ -32,11 +56,10 @@ class TempFileContext:
"""
Delete file when exiting context
"""
if self.temp_file:
self.temp_file.close()
if os.path.exists(self.temp_file.name):
os.remove(self.temp_file.name)
logger.info(f"File {self.temp_file.name} has been deleted.")
if getattr(self, "temp_file", None):
name = self.temp_file.name
self._cleanup()
logger.info(f"File {name} has been deleted.")
# Return False to propagate exception (if any exception occurred)
return False