Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): stop headroom logger from suppressing propagation to stdout/stderr (#3096)

Sudhindra Desaif78e66f7cc5e
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index 61a982de2..c2955a37a 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -1845,11 +1845,10 @@ def _setup_file_logging(
return
_warn_once_if_owner_only_unsupported(log_path)
# Attach to the headroom root logger so all sub-loggers are captured.
- # Disable propagation to root to avoid duplicate writes when
- # wrap.py redirects stderr to the same log file.
+ # Keep root propagation enabled for container stdout/stderr while
+ # this handler writes the separate port/worker-specific proxy log.
headroom_logger = logging.getLogger("headroom")
headroom_logger.setLevel(logging.INFO)
- headroom_logger.propagate = False
# Decide BEFORE constructing the handler: constructing a
# RotatingFileHandler opens (creates) the file, so building one only to
# discard it would leave an empty stray worker log and leak
diff --git a/tests/conftest.py b/tests/conftest.py
index 488325375..cf09e399d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -250,13 +250,11 @@ def _null_binary_pins():
def _reset_headroom_logger_propagation():
"""Keep `headroom.*` log records flowing to pytest's caplog handler.
- Two sources disable propagation on the headroom logger tree and never
- restore it, which then makes later `caplog`-based assertions flaky in
+ A benchmark helper disables propagation on the headroom logger tree and
+ never restores it, which then makes later `caplog`-based assertions flaky in
full-suite runs (caplog attaches to root, so a `propagate=False` anywhere
on the chain silently drops the records):
- - ``headroom.proxy.helpers._setup_file_logging`` sets
- ``getLogger("headroom").propagate = False`` on proxy startup.
- ``benchmarks.claude_session_mode_benchmark._disable_headroom_benchmark_logging``
(exercised by ``test_claude_session_mode_benchmark``) sets
``propagate = False`` + ``CRITICAL`` on ``headroom``, ``headroom.proxy``,
diff --git a/tests/test_backend_nonstreaming_cache_metrics.py b/tests/test_backend_nonstreaming_cache_metrics.py
index 943aec661..bdcccc8c6 100644
--- a/tests/test_backend_nonstreaming_cache_metrics.py
+++ b/tests/test_backend_nonstreaming_cache_metrics.py
@@ -61,14 +61,7 @@ def _find_perf_record(records: list[logging.LogRecord]) -> tuple[int, int, int]:
class _ListHandler(logging.Handler):
- """Tiny direct handler that survives the proxy disabling propagation.
-
- ``caplog`` attaches to root; ``headroom.proxy.helpers._setup_file_logging``
- flips ``logging.getLogger("headroom").propagate = False`` once a proxy
- instance is constructed in the test, after which root-attached handlers
- stop receiving headroom-namespaced records. Attaching directly to
- ``headroom.proxy`` sidesteps that.
- """
+ """Tiny direct handler immune to unrelated propagation mutations."""
def __init__(self) -> None:
super().__init__(level=logging.INFO)
diff --git a/tests/test_backend_streaming_cache_metrics.py b/tests/test_backend_streaming_cache_metrics.py
index f9a31d107..0b08c7e0d 100644
--- a/tests/test_backend_streaming_cache_metrics.py
+++ b/tests/test_backend_streaming_cache_metrics.py
@@ -57,14 +57,7 @@ def _find_perf_record(records: list[logging.LogRecord]) -> tuple[int, int, int]:
class _ListHandler(logging.Handler):
- """Tiny direct handler that survives the proxy disabling propagation.
-
- ``caplog`` attaches to root; ``headroom.proxy.helpers._setup_file_logging``
- flips ``logging.getLogger("headroom").propagate = False`` once a proxy
- instance is constructed in the test, after which root-attached handlers
- stop receiving headroom-namespaced records. Attaching directly to
- ``headroom.proxy`` sidesteps that.
- """
+ """Tiny direct handler immune to unrelated propagation mutations."""
def __init__(self) -> None:
super().__init__(level=logging.INFO)
diff --git a/tests/test_perf_per_port_logs.py b/tests/test_perf_per_port_logs.py
index 848a8eadd..4447bd999 100644
--- a/tests/test_perf_per_port_logs.py
+++ b/tests/test_perf_per_port_logs.py
@@ -81,6 +81,43 @@ def test_setup_file_logging_targets_per_port_file(workspace: Path) -> None:
logger.addHandler(h)
+def test_setup_file_logging_keeps_root_propagation_and_writes_once_per_sink(
+ workspace: Path,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ logger = logging.getLogger("headroom")
+ original_handlers = list(logger.handlers)
+ original_propagate = logger.propagate
+ for handler in original_handlers:
+ logger.removeHandler(handler)
+
+ try:
+ logger.propagate = True
+ with caplog.at_level(logging.INFO):
+ _setup_file_logging(8888)
+ logger.info("propagation-regression-record")
+
+ for handler in logger.handlers:
+ handler.flush()
+
+ propagated = [
+ record
+ for record in caplog.records
+ if record.getMessage() == "propagation-regression-record"
+ ]
+ file_text = (workspace / "logs" / "proxy-8888.log").read_text()
+ assert len(propagated) == 1
+ assert file_text.count("propagation-regression-record") == 1
+ finally:
+ for handler in list(logger.handlers):
+ if isinstance(handler, RotatingFileHandler):
+ handler.close()
+ logger.removeHandler(handler)
+ logger.propagate = original_propagate
+ for handler in original_handlers:
+ logger.addHandler(handler)
+
+
def test_setup_file_logging_targets_worker_file(workspace: Path) -> None:
logger = logging.getLogger("headroom")
original = list(logger.handlers)
diff --git a/tests/test_proxy_eager_preload_bind.py b/tests/test_proxy_eager_preload_bind.py
index a5923ca6a..cfdd0baed 100644
--- a/tests/test_proxy_eager_preload_bind.py
+++ b/tests/test_proxy_eager_preload_bind.py
@@ -141,8 +141,8 @@ async def test_startup_reports_deferred_kompress(caplog):
proxy.openai_pipeline = _FakePipeline([])
try:
- # Proxy setup disables propagation on the ``headroom`` logger, so
- # attach caplog's handler directly to the logger that emits this line.
+ # Attach caplog directly so unrelated propagation mutations cannot
+ # affect this assertion.
server_mod.logger.addHandler(caplog.handler)
try:
with caplog.at_level(logging.INFO, logger=server_mod.logger.name):
diff --git a/tests/test_proxy_response_cache_replay.py b/tests/test_proxy_response_cache_replay.py
index 738b3ad15..40b6b249b 100644
--- a/tests/test_proxy_response_cache_replay.py
+++ b/tests/test_proxy_response_cache_replay.py
@@ -59,9 +59,8 @@ class _CapturingHandler(logging.Handler):
def proxy_log_capture():
"""Capture ``headroom.proxy`` records.
- ``_setup_file_logging`` sets ``propagate = False`` on this logger, so
- ``caplog`` (which hangs off the root) never sees them — the same reason
- ``tests/test_anthropic_stage_timings.py`` attaches its own handler.
+ A direct handler keeps this fixture isolated from unrelated propagation
+ mutations elsewhere in the suite.
"""
target = logging.getLogger("headroom.proxy")
handler = _CapturingHandler()
diff --git a/tests/test_request_outcome.py b/tests/test_request_outcome.py
index 5fda51fba..fa284d12f 100644
--- a/tests/test_request_outcome.py
+++ b/tests/test_request_outcome.py
@@ -417,8 +417,8 @@ async def test_funnel_emits_perf_log_with_canonical_shape(
tok_after, tok_saved, cache_read, cache_write, cache_hit_pct,
opt_ms, transforms — in that order, space-separated."""
h = _FunnelHarness()
- # Direct handler attach: caplog otherwise drops propagation-disabled
- # records (the proxy disables ``headroom.*`` propagation once started).
+ # Direct handler attach keeps capture isolated from unrelated propagation
+ # mutations elsewhere in the suite.
target = logging.getLogger("headroom.proxy")
captured: list[logging.LogRecord] = []