mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
Merge remote-tracking branch 'origin/cleanup/log-safety-helpers' into cleanup/memory-cache-storage-logging
This commit is contained in:
@@ -38,8 +38,8 @@ def describe_exception(exc: BaseException) -> str:
|
||||
|
||||
Gives each exception in the cause chain as its type plus the innermost code
|
||||
locations, e.g. ``ValueError at headroom/x.py:12 in parse; caused by
|
||||
KeyError at ...``. An ``OSError`` keeps its errno and strerror, which never
|
||||
carry payload. With ``HEADROOM_DEBUG_DUMP=full`` it returns the full
|
||||
KeyError at ...``. An ``OSError`` keeps its errno and the system's text for
|
||||
it, never the exception's own strerror. With ``HEADROOM_DEBUG_DUMP=full`` it returns the full
|
||||
traceback, messages included.
|
||||
"""
|
||||
if content_logging_enabled():
|
||||
@@ -58,9 +58,10 @@ def describe_exception(exc: BaseException) -> str:
|
||||
|
||||
def _describe_one(exc: BaseException) -> str:
|
||||
text = type(exc).__qualname__
|
||||
if isinstance(exc, OSError) and exc.errno is not None:
|
||||
# strerror is free text in some OSError subclasses (ssl.SSLError), so bound it.
|
||||
text += f" [Errno {exc.errno}] {str(exc.strerror)[:_ID_MAX_CHARS]}"
|
||||
if isinstance(exc, OSError) and isinstance(exc.errno, int):
|
||||
# The system's own text for the errno; exc.strerror is free text in some
|
||||
# subclasses (ssl.SSLError) and can quote a path or a peer's message.
|
||||
text += f" [Errno {exc.errno}] {os.strerror(exc.errno)}"
|
||||
frames = traceback.extract_tb(exc.__traceback__)[-_MAX_FRAMES:]
|
||||
if frames:
|
||||
where = " <- ".join(
|
||||
@@ -117,7 +118,8 @@ class WarnOnce:
|
||||
failure that hits many distinct keys (paths, rows, models) cannot flood the
|
||||
log on every pass. A key is used up only when WARNING is enabled, so a
|
||||
warning suppressed by the log level is still emitted later. ``forget``
|
||||
re-arms a key once its failure has cleared.
|
||||
re-arms a key once its failure has cleared; after the overflow notice the
|
||||
guard stays quiet for good, so forgetting cannot reopen the cap.
|
||||
"""
|
||||
|
||||
def __init__(self, limit: int, what: str) -> None:
|
||||
@@ -136,7 +138,7 @@ class WarnOnce:
|
||||
with self._lock:
|
||||
if key in self._keys:
|
||||
return False
|
||||
if len(self._keys) < self._limit:
|
||||
if not self._overflowed and len(self._keys) < self._limit:
|
||||
self._keys.add(key)
|
||||
return True
|
||||
report_overflow = not self._overflowed
|
||||
|
||||
@@ -43,6 +43,13 @@ def test_oserror_keeps_errno_but_not_its_filename(monkeypatch: pytest.MonkeyPatc
|
||||
assert text == "PermissionError [Errno 13] Permission denied"
|
||||
|
||||
|
||||
def test_oserror_free_text_strerror_is_replaced_by_the_system_text() -> None:
|
||||
text = describe_exception(OSError(2, f"token={CANARY}"))
|
||||
|
||||
assert "CANARY" not in text
|
||||
assert text == f"FileNotFoundError [Errno 2] {os.strerror(2)}"
|
||||
|
||||
|
||||
def test_content_opt_in_gives_the_full_traceback(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("HEADROOM_DEBUG_DUMP", "full")
|
||||
|
||||
@@ -152,3 +159,19 @@ def test_a_cause_cycle_is_described_once() -> None:
|
||||
second.__cause__ = first
|
||||
|
||||
assert describe_exception(first) == "ValueError; caused by KeyError"
|
||||
|
||||
|
||||
def test_forgetting_after_overflow_does_not_reopen_the_cap(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
log = logging.getLogger("headroom.test_log_safety.reopen")
|
||||
guard = WarnOnce(limit=2, what="failures")
|
||||
|
||||
with caplog.at_level(logging.INFO, logger=log.name):
|
||||
assert guard.first("a", log) and guard.first("b", log)
|
||||
assert guard.first("c", log) is False # overflow notice
|
||||
for cycle in range(5):
|
||||
guard.forget("a")
|
||||
assert guard.first(f"new-{cycle}", log) is False
|
||||
|
||||
assert len(_warnings(caplog)) == 1
|
||||
|
||||
Reference in New Issue
Block a user