Skip to content

Fix race condition in OutputCapture #46685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

schmidti159
Copy link

There is a race condition in OutputCapture used in the OutputCaptureExtension that could lead to flaky test results.

This race could occur because of the existing cache in OutputCapture: When data was written, while it was read, any following reading of data might miss the last output. The cache was not invalidated correctly in this case.

// Thread 1 in get()
String result = existing.get();        // Gets null
if (result == null) {
    result = build(filter);            // Builds string WITHOUT synchronization
    // <- RACE WINDOW: Thread 2 can clear cache here
    existing.compareAndSet(null, result); // Succeeds, caches stale data
}

We are now versioning the cache entries to make sure this can't happen anymore. When invalidating the cache we also increase the version.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 6, 2025
This race could occur because of the existing cache in OutputCapture: When data was written, while it was read, any following reading of data might miss the last output. The cache was not invalidated correctly in this case.

We are now versioning the cache entries to make sure this can't happen anymore.

Signed-off-by: Daniel Schmidt <[email protected]>
@schmidti159 schmidti159 force-pushed the race-condition-in-output-capture branch from d87a89d to 5c9fc74 Compare August 6, 2025 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants