This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit a8e42ac80c698276fa953ae635266ccde4050415 Author: Mark Thomas <[email protected]> AuthorDate: Sat Nov 28 08:19:25 2026 +0000 Improve the handling of operations on an NIO2 socket that is being closed Switch the socket wrapper to the closed channel before the real channel is closed, reset and returned to the cache. Fail asynchronous operations on a reset channel via the completion handler rather than with a NullPointerException, so that the write permit is always released. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- java/org/apache/tomcat/util/net/Nio2Channel.java | 28 +++++++++++++++++++---- java/org/apache/tomcat/util/net/Nio2Endpoint.java | 19 +++++++++------ webapps/docs/changelog.xml | 4 ++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/java/org/apache/tomcat/util/net/Nio2Channel.java b/java/org/apache/tomcat/util/net/Nio2Channel.java index 3d799f9cfc..8af460d1c7 100644 --- a/java/org/apache/tomcat/util/net/Nio2Channel.java +++ b/java/org/apache/tomcat/util/net/Nio2Channel.java @@ -171,12 +171,22 @@ public class Nio2Channel implements AsynchronousByteChannel { public <A> void read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler) { - sc.read(dst, timeout, unit, attachment, handler); + AsynchronousSocketChannel channel = sc; + if (channel == null) { + handler.failed(new ClosedChannelException(), attachment); + return; + } + channel.read(dst, timeout, unit, attachment, handler); } public <A> void read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler) { - sc.read(dsts, offset, length, timeout, unit, attachment, handler); + AsynchronousSocketChannel channel = sc; + if (channel == null) { + handler.failed(new ClosedChannelException(), attachment); + return; + } + channel.read(dsts, offset, length, timeout, unit, attachment, handler); } @Override @@ -191,12 +201,22 @@ public class Nio2Channel implements AsynchronousByteChannel { public <A> void write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler) { - sc.write(src, timeout, unit, attachment, handler); + AsynchronousSocketChannel channel = sc; + if (channel == null) { + handler.failed(new ClosedChannelException(), attachment); + return; + } + channel.write(src, timeout, unit, attachment, handler); } public <A> void write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler) { - sc.write(srcs, offset, length, timeout, unit, attachment, handler); + AsynchronousSocketChannel channel = sc; + if (channel == null) { + handler.failed(new ClosedChannelException(), attachment); + return; + } + channel.write(srcs, offset, length, timeout, unit, attachment, handler); } private static final Future<Boolean> DONE = new Future<Boolean>() { diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java b/java/org/apache/tomcat/util/net/Nio2Endpoint.java index ba3157052d..0b4bf3dc75 100644 --- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java +++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java @@ -1011,15 +1011,21 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel,AsynchronousS if (log.isTraceEnabled()) { log.trace("Calling [" + getEndpoint() + "].closeSocket([" + this + "])"); } + /* + * Switch to the closed channel first so that any operation started from this point on fails rather than + * using a channel that is about to be closed, reset and returned to the cache. + */ + Nio2Channel channel = getSocket(); + reset(Nio2Channel.CLOSED_NIO2_CHANNEL); try { - getEndpoint().connections.remove(getSocket().getIOChannel()); - if (getSocket().isOpen()) { - getSocket().close(true); + getEndpoint().connections.remove(channel.getIOChannel()); + if (channel.isOpen()) { + channel.close(true); } if (getEndpoint().running) { - getSocket().reset(null, null); - if (nioChannels == null || !nioChannels.push(getSocket())) { - getSocket().free(); + channel.reset(null, null); + if (nioChannels == null || !nioChannels.push(channel)) { + channel.free(); } } } catch (Throwable e) { @@ -1030,7 +1036,6 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel,AsynchronousS } finally { socketBufferHandler = SocketBufferHandler.EMPTY; nonBlockingWriteBuffer.clear(); - reset(Nio2Channel.CLOSED_NIO2_CHANNEL); } try { SendfileData data = getSendfileData(); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index c40a865fef..6379a110ee 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -225,6 +225,10 @@ </subsection> <subsection name="Coyote"> <changelog> + <fix> + Improve the handling of operations on an NIO2 socket that is being + closed. (markt) + </fix> <fix> <bug>70287</bug>: Restore the keep-alive timeout after a sendfile write. Patch provided by Artur Shellunts. (markt) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
