Skip to content

test: stop uvicorn's logging setup leaking between tests - #143

Merged
Ezo-mas merged 1 commit into
mainfrom
test/uvicorn-logging-isolation
Nov 25, 2026
Merged

Ezo-mas merged 1 commit into
mainfrom
test/uvicorn-logging-isolation

Conversation

@Ezo-mas

@Ezo-mas Ezo-mas commented Nov 25, 2026

Copy link
Copy Markdown
Member

Fixes the order-dependent failure noted in #133: test_input_levels.py::test_access_log_omits_only_successful_meter_posts failed when the full suite ran in reverse, on main too.

Cause

The HTTP responses were all correct, and the meter filter dropped both 204s. The problem was that every remaining uvicorn.access record was captured twice, so the expected list shifted. That produced the "401/405 where 422/200 expected" mismatch.

  1. test_server_main.py::test_without_reload_the_app_is_served_directly calls main(), which builds uvicorn.Config(...) with the default log_config. uvicorn's dictConfig then sets propagate=False on uvicorn and uvicorn.access, and nothing undid it. The live_server fixture in test_live_path.py does the same.
  2. At the start of each phase, pytest's catching_logs attaches its capture handlers to the root logger and to every logger that does not propagate.
  3. The meter test turned propagation back on for uvicorn.access only. Each record then hit the capture handler on uvicorn.access and the same handler again on uvicorn.

Smallest reproduction on main (fails; passes in the opposite order):

pytest "backend/tests/test_server_main.py::test_without_reload_the_app_is_served_directly" "backend/tests/test_input_levels.py::test_access_log_omits_only_successful_meter_posts"

I bisected the full reversed suite. These two tests are the only culprits: with both excluded, the reversed run passes.

Changes (tests only)

  • The uvicorn_logging save/restore fixture moved from test_log_redaction.py to conftest.py. The helper is in uvicorn_logging_state.py. It is used in test_server_main.py (module-wide) and by the live_server fixture.
  • The meter test collects records with its own handler on uvicorn.access instead of caplog via propagation. It now passes even if the leak comes back; I checked this by turning the fixture off.
  • New regression test test_uvicorn_logging_set_up_by_main_is_restored. It checks that main() really changes the loggers and that the helper restores handlers, filters, level and propagate.

Checks (local, Windows, Python 3.13)

  • ruff check .: all checks passed. ruff format --check .: 251 files already formatted.
  • mypy backend/core backend/audio backend/launcher benchmarks: no issues in 117 source files.
  • pytest backend/tests: 2296 passed, 5 skipped.
  • Full suite in reverse order: 2296 passed, 5 skipped. It was 1 failed before the fix.
  • The two-test reproduction above: 2 passed.

Skipped (5, platform): test_launcher_platform.py:244 and :252 (POSIX process groups), :384 (needs Quartz, macOS), :410 (needs AVFoundation, macOS), test_launcher_process.py:762 (POSIX process groups). ffmpeg was on PATH, so the live-path tests ran.

Building a uvicorn.Config with the default log_config runs uvicorn's
dictConfig, which turns off propagation on the uvicorn and uvicorn.access
loggers and was never undone. pytest then attaches its capture handlers
directly to those non-propagating loggers, so
test_access_log_omits_only_successful_meter_posts (which turns propagation
back on) captured every access record twice when it ran after
test_server_main.py or test_live_path.py, as in a reverse-order run.

- Move the uvicorn_logging restore fixture to conftest (helper in
  uvicorn_logging_state.py) and use it in test_server_main.py and the
  live_server fixture.
- Collect the meter test's access records with its own handler instead
  of caplog via propagation, so it no longer depends on logger state.
- Add a regression test that main() really changes uvicorn's loggers and
  the helper puts them back.
@Ezo-mas
Ezo-mas merged commit 6b65570 into main Nov 25, 2026
3 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.

1 participant