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 e5f2374ddfa64617cf961bda2dd0fdf8a1bb6fa0
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Dec 2 17:52:02 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 075ab490fa..d8fa5ac663 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -519,6 +519,12 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
 
         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;
@@ -656,6 +662,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                         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.
@@ -1018,7 +1025,9 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
             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 {
@@ -1035,7 +1044,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 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 e734d1b665..32f4d46dc4 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 56f82fee87..d3af87a339 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -477,6 +477,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