@@ -61,7 +61,7 @@ public class Runner implements MessageConsumer {
6161 protected RunnerListener listener ;
6262
6363 // Running remote VM
64- protected VirtualMachine vm ;
64+ protected volatile VirtualMachine vm ;
6565 protected boolean vmReturnedError ;
6666
6767 // Thread transferring remote error stream to our error stream
@@ -78,6 +78,9 @@ public class Runner implements MessageConsumer {
7878 protected PrintStream sketchErr ;
7979 protected PrintStream sketchOut ;
8080
81+ protected volatile boolean cancelled ;
82+ protected final Object cancelLock = new Object [0 ];
83+
8184
8285 public Runner (JavaBuild build , RunnerListener listener ) throws SketchException {
8386 this .listener = listener ;
@@ -218,6 +221,13 @@ public boolean launchVirtualMachine(boolean present, String[] args) {
218221
219222 commandArgs .append (vmParams );
220223 commandArgs .append (sketchParams );
224+
225+ // Opportunistically quit if the launch was cancelled,
226+ // the next chance to cancel will be after connecting to the VM
227+ if (cancelled ) {
228+ return false ;
229+ }
230+
221231 launchJava (commandArgs .array ());
222232
223233 AttachingConnector connector = (AttachingConnector )
@@ -249,7 +259,15 @@ public boolean launchVirtualMachine(boolean present, String[] args) {
249259 while (true ) {
250260 try {
251261 Messages .log (getClass ().getName () + " attempting to attach to VM" );
252- vm = connector .attach (arguments );
262+ synchronized (cancelLock ) {
263+ vm = connector .attach (arguments );
264+ if (cancelled && vm != null ) {
265+ // cancelled and connected to the VM, handle closing now
266+ Messages .log (getClass ().getName () + " aborting, launch cancelled" );
267+ close ();
268+ return false ;
269+ }
270+ }
253271// vm = connector.attach(arguments);
254272 if (vm != null ) {
255273 Messages .log (getClass ().getName () + " attached to the VM" );
@@ -523,29 +541,28 @@ protected void generateTrace() {
523541 //vm.setDebugTraceMode(debugTraceMode);
524542// vm.setDebugTraceMode(VirtualMachine.TRACE_ALL);
525543// vm.setDebugTraceMode(VirtualMachine.TRACE_NONE); // formerly, seems to have no effect
526-
527- // Calling this seems to set something internally to make the
528- // Eclipse JDI wake up. Without it, an ObjectCollectedException
529- // is thrown on excReq.enable(). No idea why this works,
530- // but at least exception handling has returned. (Suspect that it may
531- // block until all or at least some threads are available, meaning
532- // that the app has launched and we have legit objects to talk to).
533- vm .allThreads ();
534- // The bug may not have been noticed because the test suite waits for
535- // a thread to be available, and queries it by calling allThreads().
536- // See org.eclipse.debug.jdi.tests.AbstractJDITest for the example.
537-
538- EventRequestManager mgr = vm .eventRequestManager ();
539- // get only the uncaught exceptions
540- ExceptionRequest excReq = mgr .createExceptionRequest (null , false , true );
541- // System.out.println(excReq);
542- // this version reports all exceptions, caught or uncaught
543- // ExceptionRequest excReq = mgr.createExceptionRequest(null, true, true);
544- // suspend so we can step
545- excReq .setSuspendPolicy (EventRequest .SUSPEND_ALL );
546- // excReq.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
547- // excReq.setSuspendPolicy(EventRequest.SUSPEND_NONE); // another option?
548- excReq .enable ();
544+ try {
545+ // Calling this seems to set something internally to make the
546+ // Eclipse JDI wake up. Without it, an ObjectCollectedException
547+ // is thrown on excReq.enable(). No idea why this works,
548+ // but at least exception handling has returned. (Suspect that it may
549+ // block until all or at least some threads are available, meaning
550+ // that the app has launched and we have legit objects to talk to).
551+ vm .allThreads ();
552+ // The bug may not have been noticed because the test suite waits for
553+ // a thread to be available, and queries it by calling allThreads().
554+ // See org.eclipse.debug.jdi.tests.AbstractJDITest for the example.
555+
556+ EventRequestManager mgr = vm .eventRequestManager ();
557+ // get only the uncaught exceptions
558+ ExceptionRequest excReq = mgr .createExceptionRequest (null , false , true );
559+ // this version reports all exceptions, caught or uncaught
560+ // suspend so we can step
561+ excReq .setSuspendPolicy (EventRequest .SUSPEND_ALL );
562+ excReq .enable ();
563+ } catch (VMDisconnectedException ignore ) {
564+ return ;
565+ }
549566
550567 Thread eventThread = new Thread () {
551568 public void run () {
@@ -851,19 +868,22 @@ protected SketchException findException(String message, ObjectReference or, Thre
851868
852869
853870 public void close () {
854- // TODO make sure stop() has already been called to exit the sketch
871+ synchronized (cancelLock ) {
872+ cancelled = true ;
873+
874+ // TODO make sure stop() has already been called to exit the sketch
855875
856- // TODO actually kill off the vm here
857- if (vm != null ) {
858- try {
859- vm .exit (0 );
876+ // TODO actually kill off the vm here
877+ if (vm != null ) {
878+ try {
879+ vm .exit (0 );
860880
861- } catch (com .sun .jdi .VMDisconnectedException vmde ) {
862- // if the vm has disconnected on its own, ignore message
863- //System.out.println("harmless disconnect " + vmde.getMessage());
864- // TODO shouldn't need to do this, need to do more cleanup
881+ } catch (com .sun .jdi .VMDisconnectedException vmde ) {
882+ // if the vm has disconnected on its own, ignore message
883+ //System.out.println("harmless disconnect " + vmde.getMessage());
884+ // TODO shouldn't need to do this, need to do more cleanup
885+ }
865886 }
866- vm = null ;
867887 }
868888 }
869889
@@ -884,7 +904,9 @@ synchronized public void message(String s) {
884904 if (editor != null ) {
885905// editor.internalCloseRunner(); // [091124]
886906// editor.handleStop(); // prior to 0192
887- editor .internalCloseRunner (); // 0192
907+ java .awt .EventQueue .invokeLater (() -> {
908+ editor .internalCloseRunner (); // 0192
909+ });
888910 }
889911 return ;
890912 }
0 commit comments