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

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

commit 5c660f019e850b17b6047bdfd5eb296acc8e1d77
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Dec 2 17:41:01 2026 +0000

    Re-use the NIO2 channel of a connection that is closed after a read failed
    
    A closing NIO2 socket wrapper only returns its channel to the pool if 
nothing
    can still be using it, which includes the read permit being free. When a 
read
    fails the completion handler does not release the read permit because the
    socket wrapper is about to be closed, so the permit was held for good and 
the
    channel was never re-used. For HTTP/1.1 connections that end with a failed 
read
    that was nearly every connection in a test with many resets. A read that has
    failed has finished with the channel so treat it as finished when checking
    whether the channel is idle.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 java/org/apache/tomcat/util/net/Nio2Endpoint.java  | 13 +++++++++--
 .../tomcat/util/net/TestNio2ChannelRecycle.java    | 25 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  7 ++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java 
b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index df00d01d8c..c0462b7af7 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -511,6 +511,12 @@ public class Nio2Endpoint extends 
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
 
         private volatile SendfileData sendfileData = null;
 
+        /*
+         * Set when a read fails. The read permit is not released in that 
case, as the socket wrapper is about to be
+         * closed, but nothing uses the channel for the read once it has 
failed.
+         */
+        private volatile boolean readFailed = false;
+
         private final CompletionHandler<Integer,ByteBuffer> 
readCompletionHandler;
         private boolean readInterest = false; // Guarded by 
readCompletionHandler
         private boolean readNotify = false;
@@ -645,6 +651,7 @@ public class Nio2Endpoint extends 
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
                         ioe = new IOException(exc);
                     }
                     setError(ioe);
+                    readFailed = true;
                     if (exc instanceof AsynchronousCloseException) {
                         // Release here since there will be no
                         // notify/dispatch to do the release.
@@ -1007,7 +1014,9 @@ public class Nio2Endpoint extends 
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
             if (sendfileData != null) {
                 return false;
             }
-            boolean readFree = readPending.tryAcquire();
+            boolean readAcquired = readPending.tryAcquire();
+            // A read that has failed has finished with the channel although 
its permit is never released
+            boolean readFree = readAcquired || readFailed;
             boolean writeFree = false;
             boolean lockFree = false;
             try {
@@ -1024,7 +1033,7 @@ public class Nio2Endpoint extends 
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
                 if (writeFree) {
                     writePending.release();
                 }
-                if (readFree) {
+                if (readAcquired) {
                     readPending.release();
                 }
             }
diff --git a/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java 
b/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java
index f706d57341..d9f263f6a6 100644
--- a/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java
+++ b/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java
@@ -104,6 +104,31 @@ public class TestNio2ChannelRecycle extends TomcatBaseTest 
{
     }
 
 
+    /*
+     * A read that fails does not release the read permit as the socket 
wrapper is about to be closed. The read has
+     * finished with the channel so, once the wrapper has been closed, the 
channel can be re-used.
+     */
+    @Test
+    public void testRecycledAfterReadFailure() throws Exception {
+        try (Socket socket = new Socket("localhost", getPort())) {
+            awaitWrapper();
+            // Reset the connection when it is closed rather than closing it 
cleanly. The read that is waiting for the
+            // request fails.
+            socket.setSoLinger(true, 0);
+        }
+
+        long end = System.currentTimeMillis() + 10000;
+        Nio2Channel channel = null;
+        while (channel == null && System.currentTimeMillis() < end) {
+            channel = endpoint.getNioChannels().pop();
+            if (channel == null) {
+                Thread.sleep(50);
+            }
+        }
+        Assert.assertNotNull("The channel was not recycled after the read 
failed", channel);
+    }
+
+
     private Nio2Endpoint.Nio2SocketWrapper awaitWrapper() throws Exception {
         long end = System.currentTimeMillis() + 10000;
         while (endpoint.connections.isEmpty() && System.currentTimeMillis() < 
end) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 71983c978d..326a080402 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -476,6 +476,13 @@
         if the connection is closed while an asynchronous sendfile operation on
         it has not finished. (markt)
       </fix>
+      <fix>
+        Return the channel of an NIO2 connection to the pool for re-use when
+        the connection is closed after a read has failed, for example because
+        the connection was reset. Previously, the channel was not re-used. This
+        restores the re-use of channels for HTTP/1.1 connections that end in
+        this way. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to