Follow-up to c2d6278. NPE->ClosedChannelException for closed socket · apache/tomcat@7046644 · GitHub
Skip to content

Commit

Permalink
Follow-up to c2d6278. NPE->ClosedChannelException for closed socket
Browse files Browse the repository at this point in the history
If an attempt is made to use a closed socket, throw a
ClosedChannelException rather than a NullPointerException
  • Loading branch information
markt-asf committed May 29, 2019
1 parent b510524 commit 7046644
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions java/org/apache/tomcat/util/net/NioEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.ByteBuffer;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.CompletionHandler;
import java.nio.channels.FileChannel;
import java.nio.channels.NetworkChannel;
Expand All @@ -52,6 +53,7 @@
import org.apache.tomcat.util.collections.SynchronizedQueue;
import org.apache.tomcat.util.collections.SynchronizedStack;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.NioChannel.ClosedNioChannel;
import org.apache.tomcat.util.net.jsse.JSSESupport;

/**
Expand Down Expand Up @@ -1240,6 +1242,10 @@ private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {

@Override
protected void doWrite(boolean block, ByteBuffer from) throws IOException {
NioChannel socket = getSocket();
if (socket instanceof ClosedNioChannel) {
throw new ClosedChannelException();
}
if (block) {
long writeTimeout = getWriteTimeout();
Selector selector = null;
Expand All @@ -1249,11 +1255,11 @@ protected void doWrite(boolean block, ByteBuffer from) throws IOException {
// Ignore
}
try {
pool.write(from, getSocket(), selector, writeTimeout);
pool.write(from, socket, selector, writeTimeout);
if (block) {
// Make sure we are flushed
do {
if (getSocket().flush(true, selector, writeTimeout)) {
if (socket.flush(true, selector, writeTimeout)) {
break;
}
} while (true);
Expand All @@ -1268,7 +1274,7 @@ protected void doWrite(boolean block, ByteBuffer from) throws IOException {
// registered for write once as both container and user code can trigger
// write registration.
} else {
if (getSocket().write(from) == -1) {
if (socket.write(from) == -1) {
throw new EOFException();
}
}
Expand Down

0 comments on commit 7046644

Please sign in to comment.