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


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 66741daae4 Only recycle an NIO2 channel when it is idle
66741daae4 is described below

commit 66741daae4e1840ad853a5405d04cbc73dcc508e
Author: Mark Thomas <[email protected]>
AuthorDate: Mon Nov 30 22:33:26 2026 +0000

    Only recycle an NIO2 channel when it is idle
    
    When a socket wrapper is closed, only reset the channel and return it to the
    cache if no read or write operation is in progress and no other thread holds
    the socket wrapper lock. Otherwise leave the channel to be garbage 
collected.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 java/org/apache/tomcat/util/net/Nio2Endpoint.java | 47 +++++++++++++++++++++--
 webapps/docs/changelog.xml                        |  4 ++
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java 
b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 4aed13b0db..c4f11d0c28 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -992,6 +992,36 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
         }
 
 
+        /**
+         * Is this wrapper's channel idle? Must only be called once the 
wrapper has been switched to the closed
+         * channel.
+         */
+        private boolean isIdle() {
+            boolean readFree = readPending.tryAcquire();
+            boolean writeFree = false;
+            boolean lockFree = false;
+            try {
+                if (readFree) {
+                    writeFree = writePending.tryAcquire();
+                }
+                if (readFree && writeFree) {
+                    lockFree = getLock().tryLock();
+                    if (lockFree) {
+                        getLock().unlock();
+                    }
+                }
+            } finally {
+                if (writeFree) {
+                    writePending.release();
+                }
+                if (readFree) {
+                    readPending.release();
+                }
+            }
+            return readFree && writeFree && lockFree;
+        }
+
+
         @Override
         protected void doClose() {
             if (log.isTraceEnabled()) {
@@ -1009,9 +1039,20 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                     channel.close(true);
                 }
                 if (getEndpoint().running) {
-                    channel.reset(null, null);
-                    if (nioChannels == null || !nioChannels.push(channel)) {
-                        channel.free();
+                    /*
+                     * Only recycle the channel if nothing is using it. 
Operations hold the read or write permit from
+                     * before they use the channel until their completion 
handler (including any TLS handling that
+                     * runs in the callbacks) has finished. A thread 
processing this socket holds the socket wrapper
+                     * lock. If either is held, a thread may still be about to 
use, or be part way through using, the
+                     * channel and must not find it has been reset and passed 
to a different connection. Leave the
+                     * channel to be garbage collected in that case. The 
wrapper already refers to the closed channel
+                     * so no new operation can use it.
+                     */
+                    if (isIdle()) {
+                        channel.reset(null, null);
+                        if (nioChannels == null || !nioChannels.push(channel)) 
{
+                            channel.free();
+                        }
                     }
                 }
             } catch (Throwable e) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 702e9c4200..fb82b34277 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -405,6 +405,10 @@
         window was lost, and the connection could stall waiting for a window
         update the client had no reason to send. (markt)
       </fix>
+      <fix>
+        Only return an NIO2 channel to the cache of channels for re-use once
+        the connection that was using it is idle. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


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

Reply via email to