Skip to content

Release readBuffer when close BufferedReadChannel & BufferedChannel#4030

Open
lifepuzzlefun wants to merge 5 commits intoapache:masterfrom
lifepuzzlefun:fix_read_buffer_leak
Open

Release readBuffer when close BufferedReadChannel & BufferedChannel#4030
lifepuzzlefun wants to merge 5 commits intoapache:masterfrom
lifepuzzlefun:fix_read_buffer_leak

Conversation

@lifepuzzlefun
Copy link
Contributor

@lifepuzzlefun lifepuzzlefun commented Jul 15, 2023

Motivation

Release readBuffer when close BufferedReadChannel & BufferedChannel

BufferedChannel is subclass of BufferedReadChannel, which will create a readBuffer to cache read content.

When close BufferedChannel, the readBuffer should be released to avoid memory leak.

Copy link
Contributor

@hangc0276 hangc0276 left a comment

Choose a reason for hiding this comment

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

Would you please provide more context about this change?

Copy link
Contributor

@hangc0276 hangc0276 left a comment

Choose a reason for hiding this comment

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

I support this change. Would you please add a test to cover this change? Especially cover the readBuffer leak case.

}

readBufferStartPosition = Long.MIN_VALUE;
readBuffer.release();
Copy link
Contributor

Choose a reason for hiding this comment

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

We'd better use ReferenceCountUtil.release(readBuffer); instead of readBuffer.release();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for review, fixed.

Comment on lines +105 to +106
super.close();
writeBufferStartPosition.set(0);
Copy link
Member

Choose a reason for hiding this comment

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

Should we close and reset the position after setting the closed to true?

// We set the position of the write buffer of this buffered channel to Long.MAX_VALUE
// so that there are no overlaps with the write buffer while reading
fc = new BufferedReadChannel(newFc, conf.getReadBufferBytes());

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change


// BufferedReadChannel is not response for fileChannel close.

closed = true;
Copy link
Member

Choose a reason for hiding this comment

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

Move the closed to the first step?

Copy link
Contributor

@dlg99 dlg99 left a comment

Choose a reason for hiding this comment

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

In general LGTM, but needs a test and more context on scenarios when this is a problem

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants