Repository: maven-surefire
Updated Branches:
  refs/heads/master e7bb6fc8b -> eed317207


[SUREFIRE-1357] PING scheduler kills JVM in debug mode


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/eed31720
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/eed31720
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/eed31720

Branch: refs/heads/master
Commit: eed317207695e3174d2f283390dd0386fc09f3e4
Parents: e7bb6fc
Author: Tibor17 <tibo...@lycos.com>
Authored: Fri Apr 7 22:38:45 2017 +0200
Committer: Tibor17 <tibo...@lycos.com>
Committed: Fri Apr 7 22:38:45 2017 +0200

----------------------------------------------------------------------
 .../booterclient/output/ForkClient.java         | 27 +++++++++-----
 .../maven/surefire/booter/ForkedBooter.java     | 38 +++++++++++++++-----
 2 files changed, 47 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/eed31720/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
----------------------------------------------------------------------
diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
index 583460f..7aaa0f1 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
@@ -77,6 +77,7 @@ import static 
org.apache.maven.surefire.util.internal.StringUtils.unescapeString
 public class ForkClient
      implements StreamConsumer
 {
+    private static final String PRINTABLE_JVM_NATIVE_STREAM = "Listening for 
transport dt_socket at address:";
     private static final long START_TIME_ZERO = 0L;
     private static final long START_TIME_NEGATIVE_TIMEOUT = -1L;
 
@@ -296,16 +297,24 @@ public class ForkClient
 
     private void logStreamWarning( Throwable e, String event )
     {
-        final String msg = "Corrupted stdin stream in forked JVM " + 
forkNumber + ".";
-        final InPluginProcessDumpSingleton util = 
InPluginProcessDumpSingleton.getSingleton();
-        final File dump =
-                e == null ? util.dumpText( msg + " Stream '" + event + "'.", 
defaultReporterFactory, forkNumber )
-                        : util.dumpException( e, msg + " Stream '" + event + 
"'.", defaultReporterFactory, forkNumber );
-
-        if ( !printedErrorStream )
+        if ( event == null || !event.contains( PRINTABLE_JVM_NATIVE_STREAM ) )
+        {
+            final String msg = "Corrupted stdin stream in forked JVM " + 
forkNumber + ".";
+            final InPluginProcessDumpSingleton util = 
InPluginProcessDumpSingleton.getSingleton();
+            final File dump =
+                    e == null
+                    ? util.dumpText( msg + " Stream '" + event + "'.", 
defaultReporterFactory, forkNumber )
+                    : util.dumpException( e, msg + " Stream '" + event + "'.", 
defaultReporterFactory, forkNumber );
+
+            if ( !printedErrorStream )
+            {
+                printedErrorStream = true;
+                log.warning( msg + " See the dump file " + 
dump.getAbsolutePath() );
+            }
+        }
+        else
         {
-            printedErrorStream = true;
-            log.warning( msg + " See the dump file " + dump.getAbsolutePath() 
);
+            log.info( event );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/eed31720/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
----------------------------------------------------------------------
diff --git 
a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
index fef21d1..a011594 100644
--- 
a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
+++ 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
@@ -32,9 +32,10 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.lang.management.ManagementFactory;
 import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.ThreadFactory;
@@ -72,7 +73,6 @@ public final class ForkedBooter
     private static final long DEFAULT_SYSTEM_EXIT_TIMEOUT_IN_SECONDS = 30;
     private static final long PING_TIMEOUT_IN_SECONDS = 20;
     private static final long ONE_SECOND_IN_MILLIS = 1000;
-    private static final ScheduledExecutorService JVM_PING = 
createPingScheduler();
 
     private static volatile ScheduledThreadPoolExecutor jvmTerminator;
     private static volatile long systemExitTimeoutInSeconds = 
DEFAULT_SYSTEM_EXIT_TIMEOUT_IN_SECONDS;
@@ -86,7 +86,7 @@ public final class ForkedBooter
     public static void main( String... args )
     {
         final CommandReader reader = startupMasterProcessReader();
-        final ScheduledFuture<?> pingScheduler = listenToShutdownCommands( 
reader );
+        final ExecutorService pingScheduler = isDebugging() ? null : 
listenToShutdownCommands( reader );
         final PrintStream originalOut = out;
         try
         {
@@ -156,7 +156,7 @@ public final class ForkedBooter
                 encode( stringBuilder, stackTraceWriter, false );
                 encodeAndWriteToOutput( ( (char) BOOTERCODE_ERROR ) + ",0," + 
stringBuilder + "\n", originalOut );
             }
-            acknowledgedExit( reader, originalOut );
+            acknowledgedExit( reader, originalOut, pingScheduler );
         }
         catch ( Throwable t )
         {
@@ -164,12 +164,17 @@ public final class ForkedBooter
             // Just throwing does getMessage() and a local trace - we want to 
call printStackTrace for a full trace
             // noinspection UseOfSystemOutOrSystemErr
             t.printStackTrace( err );
+            cancelPingScheduler( pingScheduler );
             // noinspection ProhibitedExceptionThrown,CallToSystemExit
             exit( 1 );
         }
-        finally
+    }
+
+    private static void cancelPingScheduler( ExecutorService pingScheduler )
+    {
+        if ( pingScheduler != null )
         {
-            pingScheduler.cancel( true );
+            pingScheduler.shutdown();
         }
     }
 
@@ -178,13 +183,15 @@ public final class ForkedBooter
         return getReader();
     }
 
-    private static ScheduledFuture<?> listenToShutdownCommands( CommandReader 
reader )
+    private static ExecutorService listenToShutdownCommands( CommandReader 
reader )
     {
         reader.addShutdownListener( createExitHandler() );
         AtomicBoolean pingDone = new AtomicBoolean( true );
         reader.addNoopListener( createPingHandler( pingDone ) );
         Runnable pingJob = createPingJob( pingDone );
-        return JVM_PING.scheduleAtFixedRate( pingJob, 0, 
PING_TIMEOUT_IN_SECONDS, SECONDS );
+        ScheduledExecutorService pingScheduler = createPingScheduler();
+        pingScheduler.scheduleAtFixedRate( pingJob, 0, 
PING_TIMEOUT_IN_SECONDS, SECONDS );
+        return pingScheduler;
     }
 
     private static CommandListener createPingHandler( final AtomicBoolean 
pingDone )
@@ -255,7 +262,7 @@ public final class ForkedBooter
         System.exit( returnCode );
     }
 
-    private static void acknowledgedExit( CommandReader reader, PrintStream 
originalOut )
+    private static void acknowledgedExit( CommandReader reader, PrintStream 
originalOut, ExecutorService pingScheduler )
     {
         final Semaphore barrier = new Semaphore( 0 );
         reader.addByeAckListener( new CommandListener()
@@ -271,6 +278,7 @@ public final class ForkedBooter
         launchLastDitchDaemonShutdownThread( 0 );
         long timeoutMillis = max( systemExitTimeoutInSeconds * 
ONE_SECOND_IN_MILLIS, ONE_SECOND_IN_MILLIS );
         acquireOnePermit( barrier, timeoutMillis );
+        cancelPingScheduler( pingScheduler );
         System.exit( 0 );
     }
 
@@ -397,4 +405,16 @@ public final class ForkedBooter
         File surefirePropertiesFile = new File( tmpDir, propFileName );
         return surefirePropertiesFile.exists() ? new FileInputStream( 
surefirePropertiesFile ) : null;
     }
+
+    private static boolean isDebugging()
+    {
+        for ( String argument : 
ManagementFactory.getRuntimeMXBean().getInputArguments() )
+        {
+            if ( "-Xdebug".equals( argument ) || argument.startsWith( 
"-agentlib:jdwp" ) )
+            {
+                return true;
+            }
+        }
+        return false;
+    }
 }

Reply via email to