Skip to content

fix(proxy): defer file logging install to create_app()#304

Merged
chopratejas merged 1 commit intochopratejas:mainfrom
SwiftWing21:fix/file-logging-app-lifespan
Apr 29, 2026
Merged

fix(proxy): defer file logging install to create_app()#304
chopratejas merged 1 commit intochopratejas:mainfrom
SwiftWing21:fix/file-logging-app-lifespan

Conversation

@SwiftWing21
Copy link
Copy Markdown
Contributor

Summary

Move _setup_file_logging() from module-level import to inside create_app(), so importing headroom.proxy.server in tests or library contexts no longer silently attaches a RotatingFileHandler to the user's live ~/.headroom/logs/proxy.log.

This is the follow-up agreed on in PR #303's review thread — the test-log-pollution side finding from the compression-timeout diagnostics work.

Why this matters

Previously, anything that did from headroom.proxy.server import … (the test suite, scripts, library users) would inherit the rotating file handler at import time. That's how PR #303's test_proxy_anthropic_compression_diagnostics.py ended up writing Optimization failed: TimeoutError entries into the user's real proxy.log and was briefly mistaken for a live #296 reproduction.

Approach

  • Remove the module-level call to _setup_file_logging() from headroom/proxy/server.py.
  • Call it once at the top of create_app() instead — that's the only path where a real proxy server is being stood up.
  • Existing idempotency guard (if not any(isinstance(h, RotatingFileHandler) …)) still prevents duplicate handlers when create_app() is called multiple times in a process.

Regression test

Added test_importing_server_does_not_install_file_handler (in tests/test_pr208_changes.py::TestSetupFileLogging). It launches a subprocess, imports headroom.proxy.server, and asserts no RotatingFileHandler is attached to the headroom logger. The subprocess is needed because once the parent test process imports the module the side effect would persist across tests.

Test plan

  • pytest tests/test_pr208_changes.py::TestSetupFileLogging — 3 passed
  • pytest tests/test_pr208_changes.py tests/test_proxy_anthropic_cache_stability.py tests/test_proxy_healthchecks.py tests/test_proxy_compress_endpoint.py — 82 passed, 1 skipped
  • Manual: create_app(ProxyConfig()) still installs the handler (verified before_create_app=False after_create_app=True)

🤖 Generated with Claude Code

Move _setup_file_logging() from module import to create_app() so
importing headroom.proxy.server in tests or library contexts no longer
silently attaches a RotatingFileHandler to the user's live proxy.log.

This was discovered via PR chopratejas#303: running the test suite produced
"Optimization failed: TimeoutError" entries in ~/.headroom/logs/proxy.log
because TestClient instantiations from test_proxy_anthropic_compression_diagnostics.py
inherited the module-level handler.

Add a regression test that imports the server module in a subprocess
and asserts no RotatingFileHandler is attached to the headroom logger.

Refs chopratejas#303 (review thread).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@chopratejas chopratejas merged commit 6801820 into chopratejas:main Apr 29, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants