3 Replies Latest reply on Jun 10, 2019 7:34 AM by Paul Ferraro

    Multiple threads after response.sendredirect

    ziad saade Newbie

      Dear All

       

      If I have the below code flow:

       

      method1

      {

           method2();

           stm1;

           stm2;

      }

       

      method2()

      {

           method3();

           return;

      }

       

      method3()

      {

           response.sendredirect("index.html");

      }

       

      When executing method1() the below flow should be executed (Targeted Behavior Executed in Tomcat and other Application Servers):

      1-Execute method2()

      2-Execute method3()

      3-Execute Stm1

      4-Execute Stm2

      5-Execute response.sendredirect("index.html");

       

      However Wildfly is executing as following:

      1-Execute method2()

      2-Execute method3()

      3-Execute response.sendredirect("index.html") and in parallel execute stm1 and stm2

       

      the main issue is that before executing response.sendredirect("index.html") in method3 (); all the statements in method1() after the call of method2() should be executed (Stm1, Stm2 and others if available).

       

      Kindly advice if there's any configuration in Wildfly to Apply the first flow one thread processing.

       

      PS: Attached a code template project that can be debugged in both Application Servers Tomcat and Wildfly to check the different behavior.

       

      Best Regards

        • 1. Re: Multiple threads after response.sendredirect
          Paul Ferraro Master

          I've looked at your reproducer and have run it.  Your expectations are simply incorrect.

           

          Here is the code flow in your reproducer:

           

          1. /Redirection
            1. doGet(...)
              1. doPost(...)
                1. method2(...)
                  1. method3(...)
                    1. HttpResponse.sendRedirect("RedirectionTarget")
                2. System.out.println("statemen1")
                3. System.out.println("statemen2")

           

          In this flow, your sendRedirect(...) happens before the System.out calls.  Thus the System.out from RedirectionTarget will happen in parallel with the the System.out calls in your RedirectionSource.doPost(...) method.

          • 2. Re: Multiple threads after response.sendredirect
            ziad saade Newbie

            Dear Paul

             

            Be advised that my expectation are correct when running the provided project under Apache Tomcat, WebLogic and WebSphere.

             

            Kindly try to run it under tomcat and check the output. is it possible that there's a Wildfly configuration to be applied in order to meet the expectation?

             

            Thanks and Best Regards

            • 3. Re: Multiple threads after response.sendredirect
              Paul Ferraro Master

              I refer you to section 5.5 of the servlet specification:

               

              "[sendRedirect(...)] will have the side effect of committing the response, if it has not already been committed, and terminating it. No further output to the client should be made by the servlet after these methods are called. If data is written to the response after these methods are called, the data is ignored."

               

              Therefore, following the sendRedirect(...) invocation, the client that initiated the request will receive a 302 response, sending another request to the redirection target.  This second auto-initiated request might be processed by the same thread (if the previous thread has already completed), or it might happen in a second thread.  This behavior is can vary between servlet container implementations. If there is logic that should execute before the redirected request is processed, then you should execute this logic before you call sendRedirect(...).