Skip to content

fix(logging): avoid race condition in live logging with background threads#14564

Closed
goingforstudying-ctrl wants to merge 7 commits into
pytest-dev:mainfrom
goingforstudying-ctrl:fix/live-logging-race-condition
Closed

fix(logging): avoid race condition in live logging with background threads#14564
goingforstudying-ctrl wants to merge 7 commits into
pytest-dev:mainfrom
goingforstudying-ctrl:fix/live-logging-race-condition

Conversation

@goingforstudying-ctrl

Copy link
Copy Markdown

Fixes #13693

Replace capture suspension in _LiveLoggingStreamHandler.emit() with direct writes to the original stdout stream. This prevents race conditions where background threads write to stdout/stderr while capture is suspended, causing lost output in capfd/capsys fixtures.

The previous implementation used capture_manager.global_and_fixture_disabled() which temporarily restored sys.stdout/stderr to their original values. When background threads logged during this window, their writes bypassed capture. The fix creates a temporary TerminalWriter pointing to the original stdout, allowing log messages to appear on the terminal while keeping capture active for all threads.

@goingforstudying-ctrl

This comment was marked as low quality.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 6, 2026
@goingforstudying-ctrl

This comment was marked as low quality.

@goingforstudying-ctrl

This comment was marked as low quality.

goingforstudying-ctrl and others added 7 commits June 9, 2026 13:14
…reads

Replace global capture suspension in _LiveLoggingStreamHandler.emit()
with a temporary redirect of the TerminalWriter's output file to the
original stdout. This prevents race conditions where background threads
write to stdout/stderr while capture is suspended, causing lost output
in capfd/capsys fixtures.

Fixes pytest-dev#13693
runpytest_subprocess('-s') omits progress percentages,
so PASSED lines won't include '*50%*' etc. patterns.
The minversion setting was accidentally commented out in a previous commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live logging from background threads is racy and can make capfd/capsys lose messages

2 participants