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 2016/09/05 14:17:59 UTC

svn commit: r1759274 - in /tomcat/trunk: java/org/apache/coyote/AsyncStateMachine.java webapps/docs/changelog.xml

Author: markt
Date: Mon Sep  5 14:17:59 2016
New Revision: 1759274

URL: http://svn.apache.org/viewvc?rev=1759274&view=rev
Log:
Refactor the code that implements the requirement that a call to complete() or dispatch() made from a non-container thread before the container initiated thread that called startAsync() completes must be delayed until the container initiated thread has completed. Rather than implementing this by blocking the non-container thread, extend the internal state machine to track this. This removes the possibility that blocking the non-container thread could trigger a deadlock.

Modified:
    tomcat/trunk/java/org/apache/coyote/AsyncStateMachine.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/coyote/AsyncStateMachine.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/AsyncStateMachine.java?rev=1759274&r1=1759273&r2=1759274&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/AsyncStateMachine.java [UTF-8] (original)
+++ tomcat/trunk/java/org/apache/coyote/AsyncStateMachine.java [UTF-8] Mon Sep  5 14:17:59 2016
@@ -29,28 +29,44 @@ import org.apache.tomcat.util.security.P
  *
  * <pre>
  * The internal states that are used are:
- * DISPATCHED    - Standard request. Not in Async mode.
- * STARTING      - ServletRequest.startAsync() has been called but the
- *                 request in which that call was made has not finished
- *                 processing.
- * STARTED       - ServletRequest.startAsync() has been called and the
- *                 request in which that call was made has finished
- *                 processing.
- * READ_WRITE_OP - Performing an asynchronous read or write.
- * MUST_COMPLETE - complete() has been called before the request in which
- *                 ServletRequest.startAsync() has finished. As soon as that
- *                 request finishes, the complete() will be processed.
- * COMPLETING    - The call to complete() was made once the request was in
- *                 the STARTED state. May or may not be triggered by a
- *                 container thread - depends if start(Runnable) was used
- * TIMING_OUT    - The async request has timed out and is waiting for a call
- *                 to complete(). If that isn't made, the error state will
- *                 entered.
- * MUST_DISPATCH - dispatch() has been called before the request in which
- *                 ServletRequest.startAsync() has finished. As soon as that
- *                 request finishes, the dispatch() will be processed.
- * DISPATCHING   - The dispatch is being processed.
- * ERROR         - Something went wrong.
+ * DISPATCHED       - Standard request. Not in Async mode.
+ * STARTING         - ServletRequest.startAsync() has been called but the
+ *                    request in which that call was made has not finished
+ *                    processing.
+ * STARTED          - ServletRequest.startAsync() has been called and the
+ *                    request in which that call was made has finished
+ *                    processing.
+ * READ_WRITE_OP    - Performing an asynchronous read or write.
+ * MUST_COMPLETE    - ServletRequest.startAsync() followed by complete() have
+ *                    been called during a single Servlet.service() method. The
+ *                    complete() will be processed as soon as the request
+ *                    finishes.
+ * COMPLETE_PENDING - ServletRequest.startAsync() has been called and before the
+ *                    request in which that call was had finished processing,
+ *                    complete() was called for a non-container thread. The
+ *                    complete() will be processed as soon as the request
+ *                    finishes. This is different to MUST_COMPLETE because of
+ *                    differences required to avoid race conditions during error
+ *                    handling.
+ * COMPLETING       - The call to complete() was made once the request was in
+ *                    the STARTED state. May or may not be triggered by a
+ *                    container thread - depends if start(Runnable) was used.
+ * TIMING_OUT       - The async request has timed out and is waiting for a call
+ *                    to complete(). If that isn't made, the error state will
+ *                    entered.
+ * MUST_DISPATCH    - ServletRequest.startAsync() followed by dispatch() have
+ *                    been called during a single Servlet.service() method. The
+ *                    dispatch() will be processed as soon as the request
+ *                    finishes.
+ * DISPATCH_PENDING - ServletRequest.startAsync() has been called and before the
+ *                    request in which that call was had finished processing,
+ *                    dispatch() was called for a non-container thread. The
+ *                    dispatch() will be processed as soon as the request
+ *                    finishes. This is different to MUST_DISPATCH because of
+ *                    differences required to avoid race conditions during error
+ *                    handling.
+ * DISPATCHING      - The dispatch is being processed.
+ * ERROR            - Something went wrong.
  *
  * |-----------------�------|
  * |                       \|/
@@ -63,29 +79,33 @@ import org.apache.tomcat.util.security.P
  * |   |    |                |          |--|timeout()   |                                         |
  * |   |    |     post()     |          | \|/           |     post()                              |
  * |   |    |    |---------- | --�DISPATCHED�---------- | --------------COMPLETING�-----|         |
- * |   |    |    |           |   /|\/|\ |               |                 | /|\         |         |
- * |   |    |    |    |---�- | ---|  |  |               |                 |--|          |         |
- * |   |    ^    ^    |      |       |  |startAsync()   |               timeout()       |         |
- * |   |    |    |    |   |-- \ -----|  |               |                               |         |
- * |   |    |    |    |   |    \        |               |                               |         |
- * |   |    |    |    |   |     \       |               |                               |         |
- * |   |    |    |    |   ^      \      |               |                               |         |
- * |  \|/   |    |    |   |       \    \|/   post()     |                               |         |
+ * |   |    |    |           |   /|\/|\ |               |                | /|\ /|\      |         |
+ * |   |    |    |    |---�- | ---|  |  |startAsync()   |       timeout()|--|   |       |         |
+ * |   |    ^    ^    |      |       |  |               |                       |       |         |
+ * |   |    |    |    |   |-- \ -----|  |   complete()  |                       |post() |         |
+ * |   |    |    |    |   |    \        |     /--�----- | ---COMPLETE_PENDING-�-|       |         |
+ * |   |    |    |    |   |     \       |    /          |                               |         |
+ * |   |    |    |    |   ^      \      |   /           |                               |         |
+ * |  \|/   |    |    |   |       \    \|/ /   post()   |                               |         |
  * | MUST_COMPLETE-�- | - | --�----STARTING--�--------- | -------------|                ^         |
- * |  /|\    /|\      |   |  complete()  |              |              |     complete() |         |
- * |   |      |       |   |              |              |    post()    |     /----------|         |
- * |   |      |       ^   |              |dispatch()    |    |-----|   |    /                     |
- * |   |      |       |   |              |              |    |     |   |   /                      |
- * |   |      |       |   |             \|/             |    |    \|/ \|/ /       post()          |
+ * |  /|\    /|\      |   |  complete()  | \            |              |     complete() |         |
+ * |   |      |       |   |              |  \           |    post()    |     /----------|         |
+ * |   |      |       ^   |    dispatch()|   \          |    |-----|   |    /                     |
+ * |   |      |       |   |              |    \         |    |     |   |   /                      |
+ * |   |      |       |   |             \|/    \        |    |    \|/ \|/ /       post()          |
  * |   |      |       |   |--�--MUST_DISPATCH-----�-----|    |--�--STARTED�---------�---------|   |
- * |   |      |       | dispatched() /|\   |                      / |   |                     |   |
- * |   |      |       |               |    |post()               /  |   |                     ^   |
- * ^   |      ^       |               |    |                    /   |   |asyncOperation()     |   |
- * |   |      |       ^               |    |                   /    |   |                     |   |
- * |   |      |       |               |    |    |-------------/     |   |�-READ_WRITE_OP--�---|   |
- * |   |      |       |               |    |    |    dispatch()     |            |  |  |          |
- * |   |      |       |post()         |    |    |          timeout()|            |  |  |   error()|
- * |   |      |       |dispatched()   |   \|/  \|/                  |  dispatch()|  |  |-�--------|
+ * |   |      |       | dispatched() /|\   |     \                / |   |                     |   |
+ * |   |      |       |               |    |      \              /  |   |                     |   |
+ * |   |      |       |               |    |       \            /   |   |                     |   |
+ * |   |      |       |               |    |post()  \           |   |   |                     ^   |
+ * ^   |      ^       |               |    |       \|/          |   |   |asyncOperation()     |   |
+ * |   |      |       ^               |    |  DISPATCH_PENDING  |   |   |                     |   |
+ * |   |      |       |               |    |  |post()           |   |   |                     |   |
+ * |   |      |       |               |    |  |      |----------|   |   |�-READ_WRITE_OP--�---|   |
+ * |   |      |       |               |    |  |      |  dispatch()  |            |  |  |          |
+ * |   |      |       |               |    |  |      |              |            |  |  |          |
+ * |   |      |       |post()         |    |  |      |     timeout()|            |  |  |   error()|
+ * |   |      |       |dispatched()   |   \|/\|/    \|/             |  dispatch()|  |  |-�--------|
  * |   |      |       |---�---------- | ---DISPATCHING�-----�------ | ------�----|  |
  * |   |      |                       |     |    ^                  |               |
  * |   |      |                       |     |----|                  |               |
@@ -111,30 +131,30 @@ public class AsyncStateMachine {
     private static final StringManager sm = StringManager.getManager(AsyncStateMachine.class);
 
     private static enum AsyncState {
-        DISPATCHED   (false, false, false, false, false),
-        STARTING     (true,  true,  false, false, true),
-        STARTED      (true,  true,  false, false, false),
-        MUST_COMPLETE(true,  true,  true,  false, false),
-        COMPLETING   (true,  false, true,  false, false),
-        TIMING_OUT   (true,  true,  false, false, false),
-        MUST_DISPATCH(true,  true,  false, true,  false),
-        DISPATCHING  (true,  false, false, true,  false),
-        READ_WRITE_OP(true,  true,  false, false, true),
-        ERROR        (true,  true,  false, false, false);
+        DISPATCHED      (false, false, false, false),
+        STARTING        (true,  true,  false, false),
+        STARTED         (true,  true,  false, false),
+        MUST_COMPLETE   (true,  true,  true,  false),
+        COMPLETE_PENDING(true,  true,  false, false),
+        COMPLETING      (true,  false, true,  false),
+        TIMING_OUT      (true,  true,  false, false),
+        MUST_DISPATCH   (true,  true,  false, true),
+        DISPATCH_PENDING(true,  true,  false, false),
+        DISPATCHING     (true,  false, false, true),
+        READ_WRITE_OP   (true,  true,  false, false),
+        ERROR           (true,  true,  false, false);
 
         private final boolean isAsync;
         private final boolean isStarted;
         private final boolean isCompleting;
         private final boolean isDispatching;
-        private final boolean pauseNonContainerThread;
 
         private AsyncState(boolean isAsync, boolean isStarted, boolean isCompleting,
-                boolean isDispatching, boolean pauseNonContainerThread) {
+                boolean isDispatching) {
             this.isAsync = isAsync;
             this.isStarted = isStarted;
             this.isCompleting = isCompleting;
             this.isDispatching = isDispatching;
-            this.pauseNonContainerThread = pauseNonContainerThread;
         }
 
         public boolean isAsync() {
@@ -152,10 +172,6 @@ public class AsyncStateMachine {
         public boolean isCompleting() {
             return isCompleting;
         }
-
-        public boolean getPauseNonContainerThread() {
-            return pauseNonContainerThread;
-        }
     }
 
 
@@ -234,14 +250,13 @@ public class AsyncStateMachine {
      * complete() or dispatch().
      */
     public synchronized SocketState asyncPostProcess() {
-
-        // Unpause any non-container threads that may be waiting for this
-        // container thread to complete this method. Note because of the syncs
-        // those non-container threads won't start back up until until this
-        // method exits.
-        notifyAll();
-
-        if (state == AsyncState.STARTING || state == AsyncState.READ_WRITE_OP) {
+        if (state == AsyncState.COMPLETE_PENDING) {
+            doComplete();
+            return SocketState.ASYNC_END;
+        } else if (state == AsyncState.DISPATCH_PENDING) {
+            doDispatch();
+            return SocketState.ASYNC_END;
+        } else  if (state == AsyncState.STARTING || state == AsyncState.READ_WRITE_OP) {
             state = AsyncState.STARTED;
             return SocketState.LONG;
         } else if (state == AsyncState.MUST_COMPLETE || state == AsyncState.COMPLETING) {
@@ -267,13 +282,22 @@ public class AsyncStateMachine {
 
 
     public synchronized boolean asyncComplete() {
-        pauseNonContainerThread();
+        if (!ContainerThreadMarker.isContainerThread() && state == AsyncState.STARTING) {
+            state = AsyncState.COMPLETE_PENDING;
+            return false;
+        } else {
+            return doComplete();
+        }
+    }
+
+
+    private synchronized boolean doComplete() {
         clearNonBlockingListeners();
         boolean doComplete = false;
         if (state == AsyncState.STARTING || state == AsyncState.TIMING_OUT ||
                 state == AsyncState.ERROR || state == AsyncState.READ_WRITE_OP) {
             state = AsyncState.MUST_COMPLETE;
-        } else if (state == AsyncState.STARTED) {
+        } else if (state == AsyncState.STARTED || state == AsyncState.COMPLETE_PENDING) {
             state = AsyncState.COMPLETING;
             doComplete = true;
         } else {
@@ -304,7 +328,16 @@ public class AsyncStateMachine {
 
 
     public synchronized boolean asyncDispatch() {
-        pauseNonContainerThread();
+        if (!ContainerThreadMarker.isContainerThread() && state == AsyncState.STARTING) {
+            state = AsyncState.DISPATCH_PENDING;
+            return false;
+        } else {
+            return doDispatch();
+        }
+    }
+
+
+    private synchronized boolean doDispatch() {
         boolean doDispatch = false;
         if (state == AsyncState.STARTING ||
                 state == AsyncState.TIMING_OUT ||
@@ -312,7 +345,7 @@ public class AsyncStateMachine {
             // In these three cases processing is on a container thread so no
             // need to transfer processing to a new container thread
             state = AsyncState.MUST_DISPATCH;
-        } else if (state == AsyncState.STARTED) {
+        } else if (state == AsyncState.STARTED || state == AsyncState.DISPATCH_PENDING) {
             state = AsyncState.DISPATCHING;
             // A dispatch is always required.
             // If on a non-container thread, need to get back onto a container
@@ -427,24 +460,4 @@ public class AsyncStateMachine {
         processor.getRequest().listener = null;
         processor.getRequest().getResponse().listener = null;
     }
-
-
-    /*
-     * startAsync() has been called but the container thread where this was
-     * called has not completed processing. To avoid various race conditions -
-     * including several related to error page handling - pause this
-     * non-container thread until the container thread has finished processing.
-     * The non-container thread will be paused until the container thread
-     * completes asyncPostProcess().
-     */
-    private synchronized void pauseNonContainerThread() {
-        while (!ContainerThreadMarker.isContainerThread() &&
-                state.getPauseNonContainerThread()) {
-            try {
-                wait();
-            } catch (InterruptedException e) {
-                // TODO Log this?
-            }
-        }
-    }
 }

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1759274&r1=1759273&r2=1759274&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Sep  5 14:17:59 2016
@@ -54,6 +54,20 @@
       </add>
     </changelog>
   </subsection>
+  <subsection name="Coyote">
+    <changelog>
+      <add>
+        Refactor the code that implements the requirement that a call to
+        <code>complete()</code> or <code>dispatch()</code> made from a
+        non-container thread before the container initiated thread that called
+        <code>startAsync()</code> completes must be delayed until the container
+        initiated thread has completed. Rather than implementing this by
+        blocking the non-container thread, extend the internal state machine to
+        track this. This removes the possibility that blocking the non-container
+        thread could trigger a deadlock. (markt)  
+      </add>
+    </changelog>
+  </subsection>
   <subsection name="Other">
     <changelog>
       <fix>



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