Skip to content
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

[UNDERTOW-2344] Take into account that close could run concurrently with a read operation #1549

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,19 @@ public int read(final byte[] b, final int off, final int len) throws IOException
return copied;
}

private void readIntoBuffer() throws IOException {
private PooledByteBuffer readIntoBuffer() throws IOException {
if (pooled == null && !anyAreSet(state, FLAG_FINISHED)) {
pooled = bufferPool.allocate();

final PooledByteBuffer buffer = pooled = bufferPool.allocate();
int res = Channels.readBlocking(channel, pooled.getBuffer());
pooled.getBuffer().flip();
if (res == -1) {
setFlags(FLAG_FINISHED);
pooled.close();
pooled = null;
}
return buffer;
}
return null;
}

private void readIntoBufferNonBlocking() throws IOException {
Expand Down Expand Up @@ -263,7 +264,12 @@ public void close() throws IOException {
setFlags(FLAG_CLOSED);
try {
while (allAreClear(state, FLAG_FINISHED)) {
readIntoBuffer();
// read is always supposed to run from the same thread, but
// close is a different story... specially in tests
PooledByteBuffer buffer = readIntoBuffer();
if (buffer != null) {
buffer.close();
}
if (pooled != null) {
pooled.close();
pooled = null;
Expand Down
Loading