fix: guard log flush against OSError

This commit is contained in:
YUHAO-corn 2026-05-16 22:55:05 +08:00
parent f9f54cae42
commit 8f49e3cd2a
2 changed files with 30 additions and 1 deletions

View File

@ -66,7 +66,10 @@ class LogInterceptor(io.TextIOWrapper):
super().write(data) super().write(data)
def flush(self): def flush(self):
super().flush() try:
super().flush()
except OSError:
pass
for cb in self._flush_callbacks: for cb in self._flush_callbacks:
cb(self._logs_since_flush) cb(self._logs_since_flush)
self._logs_since_flush = [] self._logs_since_flush = []

View File

@ -0,0 +1,26 @@
import io
from app.logger import LogInterceptor
class FlushErrorBuffer(io.BytesIO):
def flush(self):
raise OSError(22, "Invalid argument")
class FlushErrorStream:
buffer = FlushErrorBuffer()
encoding = "utf-8"
line_buffering = False
def test_log_interceptor_flush_ignores_oserror_and_runs_callbacks():
interceptor = LogInterceptor(FlushErrorStream())
interceptor._logs_since_flush = [{"m": "message"}]
flushed_logs = []
interceptor.on_flush(lambda logs: flushed_logs.append(list(logs)))
interceptor.flush()
assert flushed_logs == [[{"m": "message"}]]
assert interceptor._logs_since_flush == []