Skip to content

The agent that grows with you

fix(logging): unavailable-log notice is re-armed by a successful write, not by open()

teknium1f876ba60ff63
diff --git a/hermes_logging.py b/hermes_logging.py
index 5461256c1f3..d870851151f 100644
--- a/hermes_logging.py
+++ b/hermes_logging.py
@@ -325,6 +325,11 @@ class _ManagedRotatingFileHandler(RotatingFileHandler):
if self.stream is not None or os.path.exists(self.baseFilename):
self._reopen_if_externally_rotated()
super().emit(record)
+ # A record actually reached the file: only now has the destination recovered. Resetting
+ # in _open() is wrong — open() succeeds on a device whose write/flush still raise EIO,
+ # which re-armed the report and printed the path once per record.
+ if self.stream is not None:
+ self._unavailable_reported = False
def handleError(self, record: logging.LogRecord) -> None:
"""Suppress the known Windows ``concurrent-log-handler`` lock timeout.
@@ -354,7 +359,6 @@ class _ManagedRotatingFileHandler(RotatingFileHandler):
def _open(self):
stream = super()._open()
self._chmod_if_managed()
- self._unavailable_reported = False # recovered: report again if it breaks anew
return stream
def doRollover(self):
diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.py
index 0b2f6cdc6ff..91cf4e4b8b2 100644
--- a/tests/test_hermes_logging.py
+++ b/tests/test_hermes_logging.py
@@ -639,6 +639,38 @@ def test_eio_from_file_handler_names_the_path_once_then_recovers(tmp_path, capsy
handler.close()
+def test_eio_after_successful_reopen_still_names_the_path_once(tmp_path, capsys):
+ """The reported case: open() succeeds but every write/seek/flush raises EIO. Reopening must
+ not re-arm the notice, or a stuck device prints the path once per record."""
+ import io
+
+ class _SickStream(io.TextIOBase):
+ def writable(self):
+ return True
+
+ def write(self, *_a):
+ raise OSError(5, "Input/output error")
+
+ seek = tell = flush = write
+
+ path = tmp_path / "agent.log"
+ handler = hermes_logging._ManagedRotatingFileHandler(
+ str(path), maxBytes=1024, backupCount=1, encoding="utf-8",
+ )
+ handler.setFormatter(logging.Formatter("%(message)s"))
+ try:
+ handler._builtin_open = lambda *_a, **_kw: _SickStream()
+ handler.stream.close()
+ handler.stream = _SickStream()
+ for i in range(25):
+ handler.handle(logging.LogRecord("t", logging.INFO, __file__, 0, f"sick {i}", (), None))
+ err = capsys.readouterr().err
+ assert "--- Logging error ---" not in err
+ assert err.count(str(path)) == 1
+ finally:
+ handler.close()
+
+
class TestSafeStderr:
"""Tests for _safe_stderr() — Unicode tolerance on Windows console."""