This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 05645a1d234f7ec4e18f6dd6d3a515e75fdb5d4c
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 75f8d57587..e226a1f1e4 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<>() {
diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java 
b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 73f2aa2bb6..4aed13b0db 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -997,15 +997,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) {
@@ -1016,7 +1022,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 dd900d7f1a..95ef2bad88 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]

Reply via email to