1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8072466
  27  * @summary Deadlock when initializing MulticastSocket and DatagramSocket
  28  * @library /test/lib
  29  * @run main/othervm MultiDead
  30  */
  31 
  32 import java.net.DatagramSocket;
  33 import java.net.MulticastSocket;
  34 import java.util.concurrent.atomic.AtomicBoolean;
  35 import java.util.concurrent.atomic.AtomicReference;
  36 import java.util.concurrent.CountDownLatch;
  37 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  38 import jdk.test.lib.JDKToolLauncher;
  39 import jdk.test.lib.Utils;
  40 
  41 public class MultiDead {
  42     private static final int THREAD_PAIR_COUNT = 4;
  43     private static final int CHILDREN_COUNT = 20;
  44     // at least 2.5 seconds for a child to complete
  45     private static final long CHILD_TIMEOUT = 2500;
  46     private static final long TIMEOUT =
  47         Utils.adjustTimeout(CHILDREN_COUNT * CHILD_TIMEOUT * 2);
  48 
  49     public static void main(String[] args) throws Throwable {
  50         if (args.length == 0 || args[0].equals("parent")) {
  51             parentProcess();
  52         }
  53 
  54         if (args.length > 0 && args[0].equals("child")) {
  55             childProcess();
  56         }
  57     }
  58 
  59     private static void parentProcess() throws Throwable {
  60         JDKToolLauncher launcher = JDKToolLauncher
  61                 .createUsingTestJDK("java")
  62                 .addToolArg("MultiDead")
  63                 .addToolArg("child");
  64         ProcessBuilder pb = new ProcessBuilder(launcher.getCommand());
  65 
  66         AtomicReference<Process> child = new AtomicReference<>();
  67         AtomicBoolean stopFlag = new AtomicBoolean(false);
  68 
  69         Thread th = new Thread(() -> {
  70             for (int i = 0; i < CHILDREN_COUNT; ++i) {
  71                 System.out.println("child #" + (i + 1) + " of " +
  72                         CHILDREN_COUNT);
  73                 long start = System.nanoTime();
  74                 try {
  75                     child.set(pb.start());
  76                     child.get().waitFor();
  77                     if (stopFlag.get()) {
  78                         break;
  79                     }
  80                 } catch (Exception e) {
  81                     throw new RuntimeException(e);
  82                 }
  83                 if (System.nanoTime() - start >
  84                         MILLISECONDS.toNanos(CHILD_TIMEOUT)) {
  85                     System.err.println("Machine is too slow, " +
  86                             "skipping the test...");
  87                     break;
  88                 }
  89             }
  90         });
  91 
  92         th.start();
  93         th.join(TIMEOUT);
  94 
  95         stopFlag.set(true);
  96         if (th.isAlive()) {
  97             if (child.get() != null) {
  98                 child.get().destroyForcibly();
  99             }
 100             throw new RuntimeException("Failed to complete on time.");
 101         }
 102     }
 103 
 104     private static void childProcess() {
 105         CountDownLatch latch = new CountDownLatch(1);
 106         for (int i = 0; i < THREAD_PAIR_COUNT; ++i) {
 107             new Thread(() -> {
 108                 try {
 109                     latch.await();
 110                     try (MulticastSocket a = new MulticastSocket(6000)) {
 111                     }
 112                 } catch (Exception ignore) {
 113                 }
 114             }).start();
 115 
 116             new Thread(() -> {
 117                 try {
 118                     latch.await();
 119                     try (DatagramSocket b = new DatagramSocket(6000)) {
 120                     }
 121                 } catch (Exception ignore) {
 122                 }
 123             }).start();
 124         }
 125         latch.countDown();
 126     }
 127 }