You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2021/11/26 13:06:27 UTC

[tomcat] branch 10.0.x updated: Refactor to reduce JVM crashes on shutdown when sendfile is used

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

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


The following commit(s) were added to refs/heads/10.0.x by this push:
     new f319783  Refactor to reduce JVM crashes on shutdown when sendfile is used
f319783 is described below

commit f3197839bbacf44572b63951b3f238fdcdd39841
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri Nov 26 11:50:19 2021 +0000

    Refactor to reduce JVM crashes on shutdown when sendfile is used
---
 java/org/apache/tomcat/util/net/AprEndpoint.java | 29 ++++++++++++++++++++----
 webapps/docs/changelog.xml                       |  4 ++++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/AprEndpoint.java b/java/org/apache/tomcat/util/net/AprEndpoint.java
index 04651a3..e0deb55 100644
--- a/java/org/apache/tomcat/util/net/AprEndpoint.java
+++ b/java/org/apache/tomcat/util/net/AprEndpoint.java
@@ -558,6 +558,10 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
             // Stop the Poller calling select
             poller.stop();
 
+            if (getUseSendfile()) {
+                sendfile.stop();
+            }
+
             // Wait for the acceptor to shutdown
             if (acceptor.getState() != AcceptorState.ENDED && !getBindOnInit()) {
                 log.warn(sm.getString("endpoint.warn.unlockAcceptorFailed", acceptor.getThreadName()));
@@ -582,8 +586,6 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
 
             if (getUseSendfile()) {
                 try {
-                    sendfile.stop();
-
                     // Wait for the sendfile thread to exit, otherwise parallel
                     // destruction of sockets which are still in the poller can cause
                     // problems.
@@ -600,12 +602,9 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
                     if (sendfile.sendfileThread.isAlive()) {
                         log.warn(sm.getString("endpoint.sendfileThreadStop"));
                     }
-
-                    sendfile.destroy();
                 } catch (Exception e) {
                     // Ignore
                 }
-                sendfile = null;
             }
 
             // Close the SocketWrapper for each open connection - this should
@@ -630,6 +629,15 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
                 Socket.shutdown(socket.longValue(), Socket.APR_SHUTDOWN_READWRITE);
             }
 
+            if (getUseSendfile()) {
+                try {
+                    sendfile.destroy();
+                } catch (Exception e) {
+                    // Ignore
+                }
+                sendfile = null;
+            }
+
             try {
                 poller.destroy();
             } catch (Exception e) {
@@ -1810,6 +1818,15 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
          *              otherwise
          */
         public SendfileState add(SendfileData data) {
+
+            SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(data.socket));
+
+            // Use the blocking status write lock as a proxy for a lock on
+            // writing to the socket. Don't want it to close in another thread
+            // while this thread is writing as that could trigger a JVM crash.
+            WriteLock wl = ((AprSocketWrapper) socketWrapper).getBlockingStatusWriteLock();
+            wl.lock();
+
             // Initialize fd from data given
             try {
                 data.fdpool = Socket.pool(data.socket);
@@ -1846,6 +1863,8 @@ public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallB
             } catch (Exception e) {
                 log.warn(sm.getString("endpoint.sendfile.error"), e);
                 return SendfileState.ERROR;
+            } finally {
+                wl.unlock();
             }
             // Add socket to the list. Newly added sockets will wait
             // at most for pollTime before being polled
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index f6e5983..4deeb0f 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -158,6 +158,10 @@
         <bug>65677</bug>: Improve exception handling for errors during HTTP/1.1
         reads with NIO2. (markt)
       </fix>
+      <fix>
+        Refactor APR/native connector shutdown to remove a potential source of
+        JVM crashes on shutdown when sendfile is used. (markt)
+      </fix>
     </changelog>
   </subsection>
 </section>

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