Skip to content

Commit 9ca3cb1

Browse files
authored
Merge pull request #1549 from fl4via/UNDERTOW-2344
[UNDERTOW-2344] Take into account that close could run concurrently with a read operation
2 parents 6ed8ce8 + 050f2ec commit 9ca3cb1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

servlet/src/main/java/io/undertow/servlet/spec/ServletInputStreamImpl.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,19 @@ public int read(final byte[] b, final int off, final int len) throws IOException
194194
return copied;
195195
}
196196

197-
private void readIntoBuffer() throws IOException {
197+
private PooledByteBuffer readIntoBuffer() throws IOException {
198198
if (pooled == null && !anyAreSet(state, FLAG_FINISHED)) {
199-
pooled = bufferPool.allocate();
200-
199+
final PooledByteBuffer buffer = pooled = bufferPool.allocate();
201200
int res = Channels.readBlocking(channel, pooled.getBuffer());
202201
pooled.getBuffer().flip();
203202
if (res == -1) {
204203
setFlags(FLAG_FINISHED);
205204
pooled.close();
206205
pooled = null;
207206
}
207+
return buffer;
208208
}
209+
return null;
209210
}
210211

211212
private void readIntoBufferNonBlocking() throws IOException {
@@ -263,7 +264,12 @@ public void close() throws IOException {
263264
setFlags(FLAG_CLOSED);
264265
try {
265266
while (allAreClear(state, FLAG_FINISHED)) {
266-
readIntoBuffer();
267+
// read is always supposed to run from the same thread, but
268+
// close is a different story... specially in tests
269+
PooledByteBuffer buffer = readIntoBuffer();
270+
if (buffer != null) {
271+
buffer.close();
272+
}
267273
if (pooled != null) {
268274
pooled.close();
269275
pooled = null;

0 commit comments

Comments
 (0)