Skip to content

fix: close the input stream in withSideEffect - #133

Open
nickita-khylkouski wants to merge 3 commits into
google:masterfrom
nickita-khylkouski:fix/with-side-effect-close
Open

fix: close the input stream in withSideEffect#133
nickita-khylkouski wants to merge 3 commits into
google:masterfrom
nickita-khylkouski:fix/with-side-effect-close

Conversation

@nickita-khylkouski

Copy link
Copy Markdown
Contributor

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.

import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class MoreStreamsCloseTest {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can lose this line. .onClose(stream::close) would throw NPE if stream is null.

@fluentfuture

Copy link
Copy Markdown
Collaborator

Btw, please sign the CLA so I can merge the PR.

Thanks for sending the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants