8168975: java/rmi/activation/Activatable tests fail due to "Port already in use" in RMID.restart()
authormli
Sun, 20 Nov 2016 17:39:09 -0800
changeset 42171 82cbe399c3fe
parent 42170 0bb91d845f04
child 42172 979e37105f59
8168975: java/rmi/activation/Activatable tests fail due to "Port already in use" in RMID.restart() Reviewed-by: rriggs, chegar, darcy
jdk/test/java/rmi/activation/Activatable/checkActivateRef/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/checkAnnotations/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/checkImplClassLoader/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/checkRegisterInLog/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/createPrivateActivable/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/downloadParameterClass/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/elucidateNoSuchMethod/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/forceLogSnapshot/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/inactiveGroup/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/lookupActivationSystem/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/nestedActivate/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/nonExistentActivatable/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/restartCrashedService/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/restartLatecomer/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/restartService/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/shutdownGracefully/rmid.security.policy
jdk/test/java/rmi/activation/Activatable/unregisterInactive/rmid.security.policy
jdk/test/java/rmi/activation/ActivateFailedException/activateFails/rmid.security.policy
jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/ModifyDescriptor.java
jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/rmid.security.policy
jdk/test/java/rmi/testlibrary/RMID.java
jdk/test/java/rmi/testlibrary/RMIDSelectorProvider.java
--- a/jdk/test/java/rmi/activation/Activatable/checkActivateRef/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/checkActivateRef/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -4,4 +4,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.rmi.server.useDynamicProxies=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/checkAnnotations/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/checkAnnotations/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/checkImplClassLoader/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/checkImplClassLoader/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/checkRegisterInLog/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/checkRegisterInLog/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/createPrivateActivable/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/createPrivateActivable/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/downloadParameterClass/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/downloadParameterClass/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/elucidateNoSuchMethod/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/elucidateNoSuchMethod/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -4,4 +4,6 @@
 
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/forceLogSnapshot/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/forceLogSnapshot/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/inactiveGroup/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/inactiveGroup/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/lookupActivationSystem/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/lookupActivationSystem/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -1,4 +1,6 @@
 grant {
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/nestedActivate/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/nestedActivate/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/nonExistentActivatable/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/nonExistentActivatable/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/restartCrashedService/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/restartCrashedService/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/restartLatecomer/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/restartLatecomer/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/restartService/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/restartService/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/shutdownGracefully/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/shutdownGracefully/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -4,4 +4,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Ddummyname=dummyvalue";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/Activatable/unregisterInactive/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/Activatable/unregisterInactive/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/ActivateFailedException/activateFails/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/ActivateFailedException/activateFails/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,6 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission java.lang.RuntimePermission "selectorProvider";
     permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/ModifyDescriptor.java	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/ModifyDescriptor.java	Sun Nov 20 17:39:09 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,8 +33,9 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
+ *          java.base/sun.nio.ch
  * @build TestLibrary RMID ActivationLibrary
- *     ActivateMe ModifyDescriptor_Stub
+ *     ActivateMe ModifyDescriptor_Stub RMIDSelectorProvider
  * @run main/othervm/policy=security.policy/timeout=240 ModifyDescriptor
  */
 
--- a/jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/rmid.security.policy	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/activation/ActivationSystem/modifyDescriptor/rmid.security.policy	Sun Nov 20 17:39:09 2016 -0800
@@ -3,4 +3,8 @@
     permission com.sun.rmi.rmid.ExecOptionPermission "-Djava.security.policy=*";
     permission com.sun.rmi.rmid.ExecOptionPermission "-Dtest.message=hello";
     permission com.sun.rmi.rmid.ExecOptionPermission "-Dtest.message=hello, again";
+    permission java.lang.RuntimePermission "selectorProvider";
+    permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+    permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
 };
--- a/jdk/test/java/rmi/testlibrary/RMID.java	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/testlibrary/RMID.java	Sun Nov 20 17:39:09 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
 import java.rmi.*;
 import java.rmi.activation.*;
 import java.rmi.registry.*;
+import java.time.LocalTime;
 import java.util.concurrent.TimeoutException;
 
 /**
@@ -44,6 +45,15 @@
     private static final long STARTTIME_MS        = 15_000L;
     private static final long POLLTIME_MS         = 100L;
 
+    // when restart rmid, it may take more time than usual because of
+    // "port in use" by a possible interloper (check JDK-8168975),
+    // so need to set a longer timeout for restart.
+    private static long restartTimeout;
+    // Same reason to inheritedChannel in RMIDSelectorProvider.
+    // Put it here rather than in RMIDSelectorProvider to adjust
+    // both timeout values together.
+    private static long inheritedChannelTimeout;
+
     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
         // "java.rmi.activation.ActivationSystem"
 
@@ -73,7 +83,8 @@
     }
 
     /** make test options and arguments */
-    private static String makeOptions(int port, boolean debugExec) {
+    private static String makeOptions(int port, boolean debugExec,
+                                      boolean enableSelectorProvider) {
 
         String options = " -Dsun.rmi.server.activation.debugExec=" +
             debugExec;
@@ -98,12 +109,24 @@
         // to avoid spurious timeouts on slow machines.
         options += " -Dsun.rmi.activation.execTimeout=60000";
 
-        if (port == 0) {
+        // It's important to set handshakeTimeout to small value, for example
+        // 5 sec (default is 60 sec) to avoid wasting too much time when
+        // calling lookupSystem(port) in restart(), because
+        //   1. If use default value of this option, it will take about 2 minutes
+        //     to finish lookupSystem(port) in 2 loops in restart();
+        //   2. If set this option as 5 sec then lookupSystem(port) will return
+        //     very quickly.
+        options += " -Dsun.rmi.transport.tcp.handshakeTimeout=5000";
+
+        if (port == 0 || enableSelectorProvider) {
             // Ephemeral port, so have the rmid child process create the
             // server socket channel and report its port number, over stdin.
             options += " -classpath " + TestParams.testClassPath;
             options += " --add-exports=java.base/sun.nio.ch=ALL-UNNAMED";
             options += " -Djava.nio.channels.spi.SelectorProvider=RMIDSelectorProvider";
+            options += " -Dtest.java.rmi.testlibrary.RMIDSelectorProvider.port=" + port;
+            options += " -Dtest.java.rmi.testlibrary.RMIDSelectorProvider.timeout="
+                        + inheritedChannelTimeout;
 
             // Disable redirection of System.err to /tmp
             options += " -Dsun.rmi.server.activation.disableErrRedirect=true";
@@ -112,6 +135,10 @@
         return options;
     }
 
+    private static String makeArgs() {
+        return makeArgs(false, 0);
+    }
+
     private static String makeArgs(boolean includePortArg, int port) {
         String propagateManager = null;
 
@@ -183,7 +210,7 @@
                                   boolean debugExec, boolean includePortArg,
                                   int port)
     {
-        String options = makeOptions(port, debugExec);
+        String options = makeOptions(port, debugExec, false);
         String args = makeArgs(includePortArg, port);
         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
                              out, err, port);
@@ -193,14 +220,14 @@
     }
 
     public static RMID createRMIDOnEphemeralPort() {
-        return createRMID(System.out, System.err, true, true, 0);
+        return createRMID(System.out, System.err, true, false, 0);
     }
 
     public static RMID createRMIDOnEphemeralPort(OutputStream out,
                                                  OutputStream err,
                                                  boolean debugExec)
     {
-        return createRMID(out, err, debugExec, true, 0);
+        return createRMID(out, err, debugExec, false, 0);
     }
 
 
@@ -213,6 +240,9 @@
     {
         super(classname, options, args, out, err);
         this.port = port;
+        long waitTime = (long)(240_000 * TestLibrary.getTimeoutFactor());
+        restartTimeout = (long)(waitTime * 0.9);
+        inheritedChannelTimeout = (long)(waitTime * 0.8);
     }
 
     /**
@@ -280,11 +310,11 @@
         // if rmid is already running, then the test will fail with
         // a well recognized exception (port already in use...).
 
-        mesg("Starting rmid on port " + port + ".");
+        mesg("Starting rmid on port " + port + ", at " + LocalTime.now());
         int p = super.startAndGetPort();
         if (p != -1)
             port = p;
-        mesg("Started rmid on port " + port + ".");
+        mesg("Started rmid on port " + port + ", at " + LocalTime.now());
 
         // int slopFactor = 1;
         // try {
@@ -318,6 +348,7 @@
             // The rmid process is alive; check to see whether
             // it responds to a remote call.
 
+            mesg("looking up activation system, at " + LocalTime.now());
             if (lookupSystem(port) != null) {
                 /*
                  * We need to set the java.rmi.activation.port value as the
@@ -328,10 +359,11 @@
                  */
                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
                 mesg("Started successfully after " +
-                    (System.currentTimeMillis() - startTime) + "ms.");
+                    (System.currentTimeMillis() - startTime) + "ms, at " + LocalTime.now());
                 return;
             }
 
+            mesg("after fail to looking up activation system, at " + LocalTime.now());
             if (System.currentTimeMillis() > deadline) {
                 TestLibrary.bomb("Failed to start rmid, giving up after " +
                     (System.currentTimeMillis() - startTime) + "ms.", null);
@@ -347,9 +379,10 @@
      */
     public void restart() throws IOException {
         destroy();
-        options = makeOptions(port, true);
-        args = makeArgs(true, port);
-        start();
+        options = makeOptions(port, true, true);
+        args = makeArgs();
+
+        start(restartTimeout);
     }
 
     /**
--- a/jdk/test/java/rmi/testlibrary/RMIDSelectorProvider.java	Sun Nov 20 07:57:57 2016 -0800
+++ b/jdk/test/java/rmi/testlibrary/RMIDSelectorProvider.java	Sun Nov 20 17:39:09 2016 -0800
@@ -22,6 +22,7 @@
  */
 
 import java.io.*;
+import java.net.BindException;
 import java.net.InetSocketAddress;
 import java.net.ProtocolFamily;
 import java.nio.channels.Channel;
@@ -31,6 +32,7 @@
 import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.AbstractSelector;
 import java.nio.channels.spi.SelectorProvider;
+import java.time.LocalTime;
 import static java.net.StandardSocketOptions.SO_REUSEADDR;
 import static java.net.StandardSocketOptions.SO_REUSEPORT;
 
@@ -98,7 +100,37 @@
                 channel.setOption(SO_REUSEPORT, true);
             }
 
-            channel.bind(new InetSocketAddress(0));
+            // when it comes here, these properties should have been set with
+            // a valid value, but assign a default value anyway.
+            long timeout = Long.getLong(
+                    "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout",
+                    200_000);
+            long deadline = System.currentTimeMillis() + timeout;
+            int port = Integer.getInteger(
+                    "test.java.rmi.testlibrary.RMIDSelectorProvider.port", 0);
+            while (true) {
+                try {
+                    channel.bind(new InetSocketAddress(port));
+                    break;
+                } catch (BindException ex) {
+                    System.out.format("RMIDSelectorProvider: "
+                            + "failed to bind to port %d due to \"%s\", at %s%n",
+                            port, ex.getMessage(), LocalTime.now());
+                }
+                if (System.currentTimeMillis() > deadline) {
+                    System.out.format("RMIDSelectorProvider: "
+                            + "fail to bind to port %d after trying for "
+                            + "%d seconds, exiting rmid process, at %s%n",
+                            port, timeout/1000, LocalTime.now());
+                    channel.close();
+                    // can not start rmid on specific port,
+                    // there is no need to continue run rmid.
+                    System.exit(1);
+                }
+                try {
+                    Thread.sleep(1000);
+                } catch(InterruptedException ignore) { }
+            }
 
             System.out.println(RMID.EPHEMERAL_MSG + channel.socket().getLocalPort());
         }