Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-41190

MessageBus - Sending a synchronous message without responseDestination to a SynchronousDestination always times out (deadlock)

    Details

      Description

      Steps to reproduce (see unit test below):

      1. Register a synchronous destination and a simple listener that sends a reply
      2. Create a new message (do not set responseDestination)
      3. MessageBusUtil.sendSynchronousMessage will always wait 10 seconds, even if the listener executes fast

      The problem was introduced by the fix in LPS-28253 (commit bfade339c9663e53078b117a47202442fc44222b), where "liferay/message_bus/default_response" was changed from ParallelDestination to SynchronousDestination.

      The problem is not with the default_response destination itself, but with SynchronousMessageListener.

      SynchronousMessageListener enters a deadlock when the response destination is synchronous:

      1. line 64: _messageBus.sendMessage(destinationName, _message); //executes on the current thread and calls the receive method
      2. line 52: notify() //inside receive(), notify() is called, but no thread is waiting yet, so this has no effect; then the method returns execution to line 66
      3. line 66: wait(_timeout); //the thread blocks waiting for a notify() that will never come

      SynchronousMessageListener should check if the wait is necessary (either check if the destination is async, or if _result is null; in case of a sync destination the result will be not null)

      The following test always fails with a timeout.

      MessageBusTestLPS-41190.java
      package com.liferay.portal.security.pacl.test.messaging;
      
      import com.liferay.portal.kernel.messaging.Message;
      import com.liferay.portal.kernel.messaging.MessageBusException;
      import com.liferay.portal.kernel.messaging.MessageBusUtil;
      import com.liferay.portal.kernel.messaging.MessageListener;
      import com.liferay.portal.kernel.messaging.MessageListenerException;
      import com.liferay.portal.kernel.messaging.SynchronousDestination;
      import com.liferay.portal.kernel.test.ExecutionTestListeners;
      import com.liferay.portal.security.pacl.PACLExecutionTestListener;
      import com.liferay.portal.security.pacl.PACLIntegrationJUnitTestRunner;
      
      import org.junit.After;
      import org.junit.Assert;
      import org.junit.Before;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      
      @ExecutionTestListeners(listeners = {PACLExecutionTestListener.class})
      @RunWith(PACLIntegrationJUnitTestRunner.class)
      public class MessageBusTestLPS-41190 {
      
          private static final String DESTINATION = "test_synchronous_destination";
      
          @Before
          public void setup() {
              SynchronousDestination destination = new SynchronousDestination();
              destination.setName(DESTINATION);
              MessageBusUtil.addDestination(destination);
              MessageBusUtil.registerMessageListener(DESTINATION, new MessageListener() {
                  public void receive(Message message) throws MessageListenerException {
                      Message responseMessage = MessageBusUtil.createResponseMessage(message);
                      responseMessage.setPayload("true");
                      MessageBusUtil.sendMessage(responseMessage.getDestinationName(), responseMessage);
                  }
              });
          }
      
          @After
          public void cleanup() {
              MessageBusUtil.removeDestination(DESTINATION);
          }
      
          @Test(timeout = 100)
          public void testSynchronousDestination() throws MessageBusException {
              Message message = new Message();
              Object response = MessageBusUtil.sendSynchronousMessage(DESTINATION, message, 200);
              Assert.assertEquals("true", response);
          }
      
      }
      
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              support-lep@liferay.com SE Support
              Reporter:
              danmana Dan Manastireanu
              Participants of an Issue:
              Recent user:
              Esther Sanz
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 24 weeks, 5 days ago

                  Packages

                  Version Package
                  7.0.0 CE GA1
                  7.0.0 DXP GA1