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 0d8e1a84ddb265f86d2c9185c815a729d27eed70
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Dec 2 12:42:22 2026 +0000

    Add tests that an NIO2 channel is not recycled while it may be in use
    
    A closing NIO2 socket wrapper only returns its channel to the pool, to be
    passed to a different connection, if nothing can still be using it. A thread
    using the channel holds the read or write permit of the wrapper or the lock 
of
    the wrapper. Test that the channel is not recycled while the write permit is
    held or while another thread holds the lock.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 .../tomcat/util/net/TestNio2ChannelRecycle.java    | 115 +++++++++++++++++++++
 1 file changed, 115 insertions(+)

diff --git a/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java 
b/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java
new file mode 100644
index 0000000000..e734d1b665
--- /dev/null
+++ b/test/org/apache/tomcat/util/net/TestNio2ChannelRecycle.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.net;
+
+import java.lang.reflect.Method;
+import java.net.Socket;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.coyote.AbstractProtocol;
+import org.apache.coyote.http11.Http11Nio2Protocol;
+
+/**
+ * An NIO2 channel is returned to the pool when its socket wrapper is closed, 
and then passed to a different connection.
+ * That must only happen if nothing can still be using it. A thread that is 
using the channel holds the read or write
+ * permit of the socket wrapper, or the lock of the socket wrapper.
+ */
+public class TestNio2ChannelRecycle extends TomcatBaseTest {
+
+    private Nio2Endpoint endpoint;
+
+    @Before
+    public void startConnector() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        Assume.assumeTrue("NIO2 only", 
tomcat.getConnector().getProtocolHandler() instanceof Http11Nio2Protocol);
+        tomcat.start();
+
+        Method getEndpoint = 
AbstractProtocol.class.getDeclaredMethod("getEndpoint");
+        getEndpoint.setAccessible(true);
+        endpoint = (Nio2Endpoint) 
getEndpoint.invoke(tomcat.getConnector().getProtocolHandler());
+        Assert.assertNull("The pool should be empty at the start", 
endpoint.getNioChannels().pop());
+    }
+
+
+    @Test
+    public void testNotRecycledWhileWriteInProgress() throws Exception {
+        try (Socket socket = new Socket("localhost", getPort())) {
+            Nio2Endpoint.Nio2SocketWrapper wrapper = awaitWrapper();
+
+            // An operation holds the permit from before it uses the channel 
until its completion handler has finished
+            Assert.assertTrue(wrapper.writePending.tryAcquire());
+            wrapper.close();
+
+            Assert.assertNull("The channel was recycled while a write was in 
progress",
+                    endpoint.getNioChannels().pop());
+        }
+    }
+
+
+    @Test
+    public void testNotRecycledWhileWrapperLockHeld() throws Exception {
+        try (Socket socket = new Socket("localhost", getPort())) {
+            Nio2Endpoint.Nio2SocketWrapper wrapper = awaitWrapper();
+
+            // A thread that is processing the socket holds the lock of the 
socket wrapper
+            Lock lock = wrapper.getLock();
+            CountDownLatch locked = new CountDownLatch(1);
+            CountDownLatch release = new CountDownLatch(1);
+            Thread holder = new Thread(() -> {
+                lock.lock();
+                try {
+                    locked.countDown();
+                    release.await(30, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    // Ignore
+                } finally {
+                    lock.unlock();
+                }
+            });
+            holder.start();
+            try {
+                Assert.assertTrue(locked.await(10, TimeUnit.SECONDS));
+                wrapper.close();
+
+                Assert.assertNull("The channel was recycled while the socket 
wrapper was being processed",
+                        endpoint.getNioChannels().pop());
+            } finally {
+                release.countDown();
+                holder.join();
+            }
+        }
+    }
+
+
+    private Nio2Endpoint.Nio2SocketWrapper awaitWrapper() throws Exception {
+        long end = System.currentTimeMillis() + 10000;
+        while (endpoint.connections.isEmpty() && System.currentTimeMillis() < 
end) {
+            Thread.sleep(20);
+        }
+        Assert.assertEquals("The connection was not accepted", 1, 
endpoint.connections.size());
+        return (Nio2Endpoint.Nio2SocketWrapper) 
endpoint.connections.values().iterator().next();
+    }
+}


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

Reply via email to