fix: close the input stream in withSideEffect - #133
Open
nickita-khylkouski wants to merge 3 commits into
Open
Conversation
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class MoreStreamsCloseTest { |
Collaborator
There was a problem hiding this comment.
Perhaps add these to MoreSteamsTest, next to the existing withSideEffect_ tests?
| * @since 4.9 | ||
| */ | ||
| public static <T> Stream<T> withSideEffect(Stream<T> stream, Consumer<? super T> sideEffect) { | ||
| requireNonNull(stream); |
Collaborator
There was a problem hiding this comment.
I think we can lose this line. .onClose(stream::close) would throw NPE if stream is null.
Collaborator
|
Btw, please sign the CLA so I can merge the PR. Thanks for sending the fix! |
nickita-khylkouski
force-pushed
the
fix/with-side-effect-close
branch
from
September 10, 2026 21:31
f3ae967 to
92bcd47
Compare
nickita-khylkouski
force-pushed
the
fix/with-side-effect-close
branch
from
September 11, 2026 17:16
81f4cbc to
1a6f5d8
Compare
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.
hey, found a small cleanup bug in MoreStreams.withSideEffect(). It creates a new stream without forwarding the input’s close handler, so wrapping Files.lines(...) still leaves the file open even when the returned stream is used in try-with-resources. The fix just adds .onClose(stream::close), like dice() already does. No changes to traversal or side-effect timing. Added tests for closing without consuming, stopping early, and generate() closing a consumed fan-out stream. Checked before/after in a small Java harness, including a real file-handle check. The handle stays open before the fix and closes after it. This was AI-assisted; I haven’t run the full Maven build yet.