Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalStateException: Trying to send a message on a shutdown sender #38

Open
juherr opened this issue Oct 28, 2016 · 13 comments

Comments

@juherr
Copy link
Member

juherr commented Oct 28, 2016

java.lang.NullPointerException at java.io.Writer.(Unknown Source) at 
java.io.OutputStreamWriter.(Unknown Source) at 
org.testng.remote.strprotocol.JsonMessageSender.sendMessage(JsonMessageSender.java:37) at 
org.testng.remote.strprotocol.MessageHub.sendMessage(MessageHub.java:43) at 
org.testng.remote.strprotocol.RemoteTestListener1.onTestSuccess(RemoteTestListener1.java:88) at 
org.testng.internal.Invoker.runTestListeners(Invoker.java:1695) at 
org.testng.internal.Invoker.runTestListeners(Invoker.java:1675) at 
org.testng.internal.Invoker.invokeMethod(Invoker.java:700) at 
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820) at 
org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:75) at 
org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:14) at 
java.util.concurrent.FutureTask.run(Unknown Source) at 
java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at 
java.util.concurrent.FutureTask.run(Unknown Source) at 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at 
java.lang.Thread.run(Unknown Source) java.lang.IllegalStateException: Trying to send a message on a shutdown sender at 
org.testng.remote.strprotocol.JsonMessageSender.sendMessage(JsonMessageSender.java:31) at 
org.testng.remote.strprotocol.MessageHub.sendMessage(MessageHub.java:43) at 
org.testng.remote.strprotocol.RemoteTestListener1.onTestStart(RemoteTestListener1.java:49) at 
org.testng.internal.Invoker.runTestListeners(Invoker.java:1700) at 
org.testng.internal.Invoker.runTestListeners(Invoker.java:1675) at 
org.testng.internal.Invoker.invokeMethod(Invoker.java:619) at 
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820) at 
org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:75) at 
org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:14) at 
java.util.concurrent.FutureTask.run(Unknown Source) at 
java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at 
java.util.concurrent.FutureTask.run(Unknown Source) at 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at 
java.lang.Thread.run(Unknown Source)

From http://stackoverflow.com/q/40296350/4234729

@juherr
Copy link
Member Author

juherr commented Oct 31, 2016

Ping @missedone

@missedone
Copy link
Contributor

I tried once before, but can't reproduce, will try a more complicate case later.

@juherr
Copy link
Member Author

juherr commented Oct 31, 2016

Ok 👍

I've just seen the Trying to send a message on a shutdown sender message and it could be a clue too.
As I understand it, the server is closed due to an exception (check stackoverflow thread) and the client is failing.

@missedone missedone changed the title NPE java.lang.IllegalStateException: Trying to send a message on a shutdown sender Nov 19, 2016
@missedone
Copy link
Contributor

missedone commented Nov 19, 2016

Still can't reproduce the issue with the sample on SO.
however, looks like the sender was shutdown due to exception in running testng, in the case reported, testng-team/testng#1221 ConcurrentModifiedException in TextReporter.

  public void run() {
    IMessageSender sender = m_serPort != null
        ? new SerializedMessageSender(m_host, m_serPort)
        : new StringMessageSender(m_host, m_port);
    final MessageHub msh = new MessageHub(sender);
    msh.setDebug(isDebug());
    try {
      msh.connect();
      // We couldn't do this until now in debug mode since the .xml file didn't exist yet.
      // Now that we have connected with the Eclipse client, we know that it created the .xml
      // file so we can proceed with the initialization
      initializeSuitesAndJarFile();

      List<XmlSuite> suites = Lists.newArrayList();
      calculateAllSuites(m_suites, suites);
      if(suites.size() > 0) {

        int testCount= 0;

        for(int i= 0; i < suites.size(); i++) {
          testCount+= (suites.get(i)).getTests().size();
        }

        GenericMessage gm= new GenericMessage(MessageHelper.GENERIC_SUITE_COUNT);
        gm.setSuiteCount(suites.size());
        gm.setTestCount(testCount);
        msh.sendMessage(gm);

        addListener(new RemoteSuiteListener(msh));
        setTestRunnerFactory(new DelegatingTestRunnerFactory(buildTestRunnerFactory(), msh));

        super.run();
      }
      else {
        System.err.println("No test suite found. Nothing to run");
      }
    }
    catch(Throwable cause) {
      cause.printStackTrace(System.err);
    }
    finally {
// Nick: shutdown here
      msh.shutDown();
      if (! m_debug && ! m_dontExit) {
        System.exit(0);
      }
    }
  }

in JsonMessageSender, it check the status:

  public void sendMessage(IMessage message) throws Exception {
    if (m_outStream == null) {
// Nick: I think that's fine here
// or, probably, we can just print a warning message, and return quietly.
      throw new IllegalStateException("Trying to send a message on a shutdown sender");
    }

    synchronized (m_outStream) {
      p("Sending message " + message);

      ...
    }
  }

@juherr
Copy link
Member Author

juherr commented Nov 19, 2016

Ok for the IllegalStateException which seems to be expected.
But what about the NullPointerException? Is it possible to have m_outStream = null at

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(m_outStream, "UTF-8"));
?

@missedone
Copy link
Contributor

missedone commented Nov 19, 2016

well, I think, yes, there is a time window that synchronized get the copy of non-null reference of m_outStream, but sadly 'm_outStream' could be set to null by other thread once enter the synchronized block.
we can make it stronger by double check if m_outStream is null, for example:

    synchronized (m_outStream) {
      p("Sending message " + message);
// Nick: double check here
if (m_outStream != null) {
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(m_outStream, "UTF-8"));
      JsonWriter writer = new JsonWriter(bw);
      writeMessage(writer, message);
      bw.newLine();
      bw.flush();

      waitForAck();
}
    }

@juherr
Copy link
Member Author

juherr commented Nov 21, 2016

Ok.And what about?

synchronized (m_outStream) {      
  if (m_outStream == null) {
    throw new IllegalStateException("Trying to send a message on a shutdown sender");
  }
  p("Sending message " + message);
  ...

Do you see any drawbacks?

@missedone
Copy link
Contributor

m_outStream could be null, synchronized on null object will cause NPE.

@juherr
Copy link
Member Author

juherr commented Nov 21, 2016

oh! right!

@missedone
Copy link
Contributor

I think we can just leave it for now (keep the ticket open), see if this will happen again with the upstream fix merged.

@juherr
Copy link
Member Author

juherr commented Nov 21, 2016

👍

@laurafchen
Copy link

Hi there, I'm still this exception, can you help out?
java.lang.IllegalStateException: Trying to send a message on a shutdown sender
at org.testng.remote.strprotocol.JsonMessageSender.sendMessage(JsonMessageSender.java:31)
at org.testng.remote.strprotocol.MessageHub.sendMessage(MessageHub.java:43)
at org.testng.remote.strprotocol.RemoteTestListener1.onTestSuccess(RemoteTestListener1.java:88)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1899)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:778)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

@juherr
Copy link
Member Author

juherr commented Mar 7, 2018

@laurafchen do you have any project to share that shows the issue? Could you share your Eclipse version and the version if its plugins ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants