Skip to content

Commit 23d4cd3

Browse files
committed
[UNDERTOW-2298] Avoid NPE on concurrent close/timeout
1 parent ddb4aee commit 23d4cd3

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

servlet/src/main/java/io/undertow/servlet/UndertowServletLogger.java

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import static org.jboss.logging.Logger.Level.ERROR;
3434
import static org.jboss.logging.Logger.Level.WARN;
35+
import static org.jboss.logging.Logger.Level.TRACE;
3536

3637
/**
3738
* log messages start at 15000
@@ -140,4 +141,7 @@ public interface UndertowServletLogger extends BasicLogger {
140141
@Message(id = 15024, value = "Servlet %s init() method in web application %s threw exception")
141142
void failedToLoad(String servletName, String appName, @Cause Throwable t);
142143

144+
@LogMessage(level = TRACE)
145+
@Message(id = 15025, value = "IO Operation de-synchronization trace: ")
146+
void ioOperationDesynchronization( @Cause Throwable t);
143147
}

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.xnio.channels.StreamSourceChannel;
3636
import io.undertow.connector.ByteBufferPool;
3737
import io.undertow.connector.PooledByteBuffer;
38+
import io.undertow.servlet.UndertowServletLogger;
3839
import io.undertow.servlet.UndertowServletMessages;
3940

4041
/**
@@ -196,14 +197,21 @@ public int read(final byte[] b, final int off, final int len) throws IOException
196197

197198
private void readIntoBuffer() throws IOException {
198199
if (pooled == null && !anyAreSet(state, FLAG_FINISHED)) {
199-
pooled = bufferPool.allocate();
200+
try {
201+
pooled = bufferPool.allocate();
200202

201-
int res = Channels.readBlocking(channel, pooled.getBuffer());
202-
pooled.getBuffer().flip();
203-
if (res == -1) {
204-
setFlags(FLAG_FINISHED);
205-
pooled.close();
206-
pooled = null;
203+
int res = Channels.readBlocking(channel, pooled.getBuffer());
204+
pooled.getBuffer().flip();
205+
if (res == -1) {
206+
setFlags(FLAG_FINISHED);
207+
pooled.close();
208+
pooled = null;
209+
}
210+
} catch(NullPointerException npe) {
211+
//UNDERTOW-2298 - this will happen in small window when read is happening and timeout happens
212+
if(!isFinished()) {
213+
UndertowServletLogger.ROOT_LOGGER.ioOperationDesynchronization(npe);
214+
}
207215
}
208216
}
209217
}

spotbugs-exclude.xml

+5
Original file line numberDiff line numberDiff line change
@@ -932,4 +932,9 @@
932932
<Match>
933933
<Package name="io.undertow.benchmarks"/>
934934
</Match>
935+
<Match>
936+
<Bug pattern="DCN_NULLPOINTER_EXCEPTION"/>
937+
<Class name="io.undertow.servlet.spec.ServletInputStreamImpl"/>
938+
<Method name="readIntoBuffer"/>
939+
</Match>
935940
</FindBugsFilter>

0 commit comments

Comments
 (0)