Author: krosenvold
Date: Mon Aug  6 19:47:31 2012
New Revision: 1369951

URL: http://svn.apache.org/viewvc?rev=1369951&view=rev
Log:
[SUREFIRE-897] System.exit() in ForkedBooter might hang due to swing/windows 
bug.

Patch by Ralf Stuckert, applied with some modifications

Modified:
    
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java

Modified: 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java?rev=1369951&r1=1369950&r2=1369951&view=diff
==============================================================================
--- 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
 (original)
+++ 
maven/surefire/trunk/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
 Mon Aug  6 19:47:31 2012
@@ -77,7 +77,7 @@ public class ForkedBooter
             originalOut.println( "Z,0,BYE!" );
             originalOut.flush();
             // noinspection CallToSystemExit
-            System.exit( 0 );
+            exit( 0 );
         }
         catch ( Throwable t )
         {
@@ -85,10 +85,19 @@ public class ForkedBooter
             // noinspection UseOfSystemOutOrSystemErr
             t.printStackTrace( System.err );
             // noinspection ProhibitedExceptionThrown,CallToSystemExit
-            System.exit( 1 );
+            exit( 1 );
         }
     }
 
+    private final static long SYSTEM_EXIT_TIMEOUT = 30 * 1000;
+
+    private static void exit( final int returnCode )
+    {
+        launchLastDitchDaemonShutdownThread( returnCode );
+        System.exit( returnCode );
+    }
+
+
     public static RunResult runSuitesInProcess( Object testSet, ClassLoader 
testsClassLoader,
                                                 StartupConfiguration 
startupConfiguration,
                                                 ProviderConfiguration 
providerConfiguration )
@@ -112,4 +121,25 @@ public class ForkedBooter
         final PrintStream originalSystemOut = 
providerConfiguration.getReporterConfiguration().getOriginalSystemOut();
         return surefireReflector.createForkingReporterFactory( trimStackTrace, 
originalSystemOut );
     }
+
+    private static void launchLastDitchDaemonShutdownThread( final int 
returnCode )
+    {
+        Thread lastExit = new Thread( new Runnable()
+        {
+            public void run()
+            {
+                try
+                {
+                    Thread.sleep( SYSTEM_EXIT_TIMEOUT );
+                    Runtime.getRuntime().halt( returnCode );
+                }
+                catch ( InterruptedException ignore )
+                {
+                }
+            }
+        } );
+        lastExit.setDaemon( true );
+        lastExit.start();
+    }
+
 }


Reply via email to