Skip to content

Commit

Permalink
[UNDERTOW-2298] Avoid NPE on concurrent close/timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
baranowb committed Apr 3, 2024
1 parent ddb4aee commit 23d4cd3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.WARN;
import static org.jboss.logging.Logger.Level.TRACE;

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

@LogMessage(level = TRACE)
@Message(id = 15025, value = "IO Operation de-synchronization trace: ")
void ioOperationDesynchronization( @Cause Throwable t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.xnio.channels.StreamSourceChannel;
import io.undertow.connector.ByteBufferPool;
import io.undertow.connector.PooledByteBuffer;
import io.undertow.servlet.UndertowServletLogger;
import io.undertow.servlet.UndertowServletMessages;

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

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

int res = Channels.readBlocking(channel, pooled.getBuffer());
pooled.getBuffer().flip();
if (res == -1) {
setFlags(FLAG_FINISHED);
pooled.close();
pooled = null;
int res = Channels.readBlocking(channel, pooled.getBuffer());
pooled.getBuffer().flip();
if (res == -1) {
setFlags(FLAG_FINISHED);
pooled.close();
pooled = null;
}
} catch(NullPointerException npe) {
//UNDERTOW-2298 - this will happen in small window when read is happening and timeout happens
if(!isFinished()) {
UndertowServletLogger.ROOT_LOGGER.ioOperationDesynchronization(npe);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -932,4 +932,9 @@
<Match>
<Package name="io.undertow.benchmarks"/>
</Match>
<Match>
<Bug pattern="DCN_NULLPOINTER_EXCEPTION"/>
<Class name="io.undertow.servlet.spec.ServletInputStreamImpl"/>
<Method name="readIntoBuffer"/>
</Match>
</FindBugsFilter>

0 comments on commit 23d4cd3

Please sign in to comment.