diff --git a/tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorMessageTransportTest.java b/tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorMessageTransportTest.java index 02aee5d34325..7df5b1302d17 100644 --- a/tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorMessageTransportTest.java +++ b/tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorMessageTransportTest.java @@ -134,9 +134,11 @@ public void inspectorReconnectTest() throws IOException, InterruptedException { } // We will not get the last 4 messages as we do not do initial suspension on re-connect int expectedNumMessages = 2 * MESSAGES.length - 4; - while (session.messages.size() < expectedNumMessages) { - // The reply messages are sent asynchronously. We need to wait for them. - Thread.sleep(100); + synchronized (session.messages) { + while (session.messages.size() < expectedNumMessages) { + // The reply messages are sent asynchronously. We need to wait for them. + session.messages.wait(); + } } Assert.assertEquals(session.messages.toString(), expectedNumMessages, session.messages.size()); @@ -226,7 +228,10 @@ private static final class BasicRemote { void sendText(String text) throws IOException { if (!text.startsWith("{\"method\":\"Debugger.scriptParsed\"")) { - messages.add("toClient(" + text + ")"); + synchronized (messages) { + messages.add("toClient(" + text + ")"); + messages.notifyAll(); + } } if (text.startsWith("{\"method\":\"Debugger.paused\"")) { handler.onMessage("{\"id\":100,\"method\":\"Debugger.resume\"}"); diff --git a/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/instrument/InspectorInstrument.java b/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/instrument/InspectorInstrument.java index cf55795a4a5e..20569042feef 100644 --- a/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/instrument/InspectorInstrument.java +++ b/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/instrument/InspectorInstrument.java @@ -420,10 +420,10 @@ private static final class Server { info.println(" " + address); info.flush(); } else { + restartServerEndpointOnClose(hostAndPort, env, wsuri, executionContext, connectionWatcher, iss, interceptor); interceptor.opened(serverEndpoint); wss = interceptor; wsURL = wsuri.toString(); - restartServerEndpointOnClose(hostAndPort, env, wsuri, executionContext, connectionWatcher, iss, interceptor); } } if (debugBreak || waitAttached) { diff --git a/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java b/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java index 93cad99f7dfa..1bd54d12d080 100644 --- a/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java +++ b/tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java @@ -62,7 +62,7 @@ public final class InspectServerSession implements MessageEndpoint { final InspectorExecutionContext context; private volatile MessageEndpoint messageEndpoint; private volatile JSONMessageListener jsonMessageListener; - private CommandProcessThread processThread; + private volatile CommandProcessThread processThread; private Runnable onClose; private InspectServerSession(RuntimeDomain runtime, DebuggerDomain debugger, ProfilerDomain profiler, @@ -86,15 +86,19 @@ public void onClose(Runnable onCloseTask) { @Override public void sendClose() { - runtime.disable(); - debugger.disable(); - profiler.disable(); - context.reset(); - messageEndpoint = null; - processThread.dispose(); - processThread = null; - if (onClose != null) { - onClose.run(); + Runnable onCloseRunnable = null; + synchronized (this) { + runtime.disable(); + debugger.disable(); + profiler.disable(); + context.reset(); + messageEndpoint = null; + processThread.dispose(); + processThread = null; + onCloseRunnable = onClose; + } + if (onCloseRunnable != null) { + onCloseRunnable.run(); } } @@ -103,7 +107,7 @@ public DebuggerDomain getDebugger() { return debugger; } - public void setMessageListener(MessageEndpoint messageListener) { + public synchronized void setMessageListener(MessageEndpoint messageListener) { this.messageEndpoint = messageListener; if (messageListener != null && processThread == null) { EventHandler eh = new EventHandlerImpl(); @@ -115,7 +119,7 @@ public void setMessageListener(MessageEndpoint messageListener) { } } - public void setJSONMessageListener(JSONMessageListener messageListener) { + public synchronized void setJSONMessageListener(JSONMessageListener messageListener) { this.jsonMessageListener = messageListener; if (messageListener != null && processThread == null) { EventHandler eh = new EventHandlerImpl(); @@ -139,14 +143,20 @@ public void sendText(String message) { } return; } - processThread.push(cmd); + CommandProcessThread pt = processThread; + if (pt != null) { + pt.push(cmd); + } } public void sendCommand(Command cmd) { if (context.isSynchronous()) { sendCommandSync(cmd); } else { - processThread.push(cmd); + CommandProcessThread pt = processThread; + if (pt != null) { + pt.push(cmd); + } } }