Repository navigation
test: stop uvicorn's logging setup leaking between tests - #143
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the order-dependent failure noted in #133:
test_input_levels.py::test_access_log_omits_only_successful_meter_postsfailed when the full suite ran in reverse, onmaintoo.Cause
The HTTP responses were all correct, and the meter filter dropped both 204s. The problem was that every remaining
uvicorn.accessrecord was captured twice, so the expected list shifted. That produced the "401/405 where 422/200 expected" mismatch.test_server_main.py::test_without_reload_the_app_is_served_directlycallsmain(), which buildsuvicorn.Config(...)with the defaultlog_config. uvicorn'sdictConfigthen setspropagate=Falseonuvicornanduvicorn.access, and nothing undid it. Thelive_serverfixture intest_live_path.pydoes the same.catching_logsattaches its capture handlers to the root logger and to every logger that does not propagate.uvicorn.accessonly. Each record then hit the capture handler onuvicorn.accessand the same handler again onuvicorn.Smallest reproduction on
main(fails; passes in the opposite order):I bisected the full reversed suite. These two tests are the only culprits: with both excluded, the reversed run passes.
Changes (tests only)
uvicorn_loggingsave/restore fixture moved fromtest_log_redaction.pytoconftest.py. The helper is inuvicorn_logging_state.py. It is used intest_server_main.py(module-wide) and by thelive_serverfixture.uvicorn.accessinstead of caplog via propagation. It now passes even if the leak comes back; I checked this by turning the fixture off.test_uvicorn_logging_set_up_by_main_is_restored. It checks thatmain()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.Skipped (5, platform):
test_launcher_platform.py:244and: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.