fix(ci_visibility): set up Test Optimization logging before pytest log capture [backport 3.3] #13065
+17
−1
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.
Backport 2db08ec from #13030 to 3.3.
We have a recurring problem where emitting logs at the end of a pytest test session results in errors like:
This happens because our plugin calls
take_over_logger_stream_handler()
, which callslogging.StreamHandler()
, which defaults to usingsys.stderr
as its output stream, but by the time we call it, pytest'spytest_load_initial_conftests
hook fromcapture.py
has already executed and replacedsys.stderr
with aFileIO
buffer that is closed by the end of the test session, so we grab a reference to this internalFileIO
buffer instead of the actualsys.stderr
. This PR makes our ownpytest_load_initial_conftests
hook run earlier than pytest'scapture.py
one.Additionally, this fixes
take_over_logger_stream_handler()
to copy the handler list before callingremoveHandler()
, as this mutates the list we are iterating through.One user-visible side effect of this is that now we always print these logs at the beginning of the test session, regardless of whether
pytest -s
is used:Whether this is a bug or a feature is up to discussion. Previously we only printed this if
pytest
was called with-s
, more by accident than by design.The way I managed to reproduce it was by raising an exception right after tracer shutdown in
CIVisibility._stop_service()
:Checklist
Reviewer Checklist