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

remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 9535eb3  Optimized selector handling for Java 11
9535eb3 is described below

commit 9535eb304d5b359c8e23ae87eb6b4612e9bb608d
Author: remm <r...@apache.org>
AuthorDate: Thu May 27 10:55:55 2021 +0200

    Optimized selector handling for Java 11
    
    Hopefully well tested with Tomcat 10.
---
 java/org/apache/tomcat/util/net/NioEndpoint.java | 42 +++++++++++++++---------
 webapps/docs/changelog.xml                       |  3 ++
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java 
b/java/org/apache/tomcat/util/net/NioEndpoint.java
index e8b017b..234747c 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -739,23 +739,28 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
         }
 
         public void cancelledKey(SelectionKey sk, 
SocketWrapperBase<NioChannel> socketWrapper) {
-            try {
-                // If is important to cancel the key first, otherwise a 
deadlock may occur between the
-                // poller select and the socket channel close which would 
cancel the key
-                if (sk != null) {
-                    sk.attach(null);
-                    if (sk.isValid()) {
-                        sk.cancel();
+            if (JreCompat.isJre11Available() && socketWrapper != null) {
+                socketWrapper.close();
+            } else {
+                try {
+                    // If is important to cancel the key first, otherwise a 
deadlock may occur between the
+                    // poller select and the socket channel close which would 
cancel the key
+                    // This workaround is not needed on Java 11+
+                    if (sk != null) {
+                        sk.attach(null);
+                        if (sk.isValid()) {
+                            sk.cancel();
+                        }
+                    }
+                } catch (Throwable e) {
+                    ExceptionUtils.handleThrowable(e);
+                    if (log.isDebugEnabled()) {
+                        
log.error(sm.getString("endpoint.debug.channelCloseFail"), e);
+                    }
+                } finally {
+                    if (socketWrapper != null) {
+                        socketWrapper.close();
                     }
-                }
-            } catch (Throwable e) {
-                ExceptionUtils.handleThrowable(e);
-                if (log.isDebugEnabled()) {
-                    log.error(sm.getString("endpoint.debug.channelCloseFail"), 
e);
-                }
-            } finally {
-                if (socketWrapper != null) {
-                    socketWrapper.close();
                 }
             }
         }
@@ -1746,6 +1751,11 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
         }
 
         private SelectionKey getSelectionKey() {
+            // Shortcut for Java 11 onwards
+            if (JreCompat.isJre11Available()) {
+                return null;
+            }
+
             SocketChannel socketChannel = 
socketWrapper.getSocket().getIOChannel();
             if (socketChannel == null) {
                 return null;
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 91731c9..6a2f068 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -174,6 +174,9 @@
         Add back simplification of NIO block read and write, now better
         validated in Tomcat 10. (remm)
       </update>
+      <fix>
+        Optimize NIO selector handling for Java 11. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to