StreamPollFeeder: declare done and exception as volatile - #387
Open
elharo wants to merge 5 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a Java Memory Model visibility bug in StreamPollFeeder by ensuring cross-thread state (done, exception) is safely published, and adds a regression test that exercises the previously-hanging scenario with continuously-available input.
Changes:
- Declare
StreamPollFeeder.doneasvolatileto guarantee termination visibility when the feeder loop doesn’t enter thesynchronized(lock)block. - Declare
StreamPollFeeder.exceptionasvolatileso concurrent readers don’t observe a stalenull. - Add a JUnit regression test that simulates continuously available data and asserts
waitUntilDone()completes within a timeout.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/shared/utils/cli/StreamPollFeeder.java | Marks done and exception as volatile to fix cross-thread visibility and prevent hangs/stale reads. |
| src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java | Adds a timeout-based regression test targeting the continuous-data hang scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
StreamPollFeeder.doneandStreamPollFeeder.exceptionare accessed from multiple threads withoutvolatile, violating the Java Memory Model.done(line 40): read inrun()outside anysynchronizedblock (line 61), written inwaitUntilDone()insidesynchronized (lock)(line 107). When the feeder thread is continuously reading data (input.available() > 0), it never enters thesynchronizedblock, so no happens‑before guarantees the write todoneis ever seen. The thread loops forever andwaitUntilDone()'sjoin()call hangs.exception(line 38): written by therun()thread (lines 79, 92) and read from other threads viagetException()(line 101). Withoutvolatile, a thread callinggetException()may see a stalenull.StreamPumperalready declares its equivalent field asvolatile—StreamPollFeedershould follow the same pattern.Fixes #385
Fixes #386