Compare commits

...

2 Commits

Author SHA1 Message Date
Yuhao Chen
969beae1c7
Merge 8f49e3cd2a into 3fe9f5fecb 2026-07-04 14:10:46 +08:00
YUHAO-corn
8f49e3cd2a fix: guard log flush against OSError 2026-05-26 15:38:13 +08:00
2 changed files with 30 additions and 1 deletions

View File

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