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
The following commit(s) were added to refs/heads/11.0.x by this push:
new 0a3af3d7a5 Only recycle an NIO2 channel when it is idle
0a3af3d7a5 is described below
commit 0a3af3d7a5f8e46cecb9cfeb82b0cea7def7e5a5
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 9e98f49055..07c14f2b44 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -995,6 +995,36 @@ public class Nio2Endpoint extends
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
}
+ /**
+ * 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()) {
@@ -1012,9 +1042,20 @@ public class Nio2Endpoint extends
AbstractNetworkChannelEndpoint<Nio2Channel,Asy
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 64f022dbaf..4168d4b825 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]