6791551: ExclusiveBind.java has a race condition

Reviewed-by: sla, sjiang
This commit is contained in:
Jaroslav Bachorik 2014-02-10 14:01:16 +01:00
parent 6720161012
commit 1cf765b9e8

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,57 +25,28 @@
* @bug 4531526 * @bug 4531526
* @summary Test that more than one debuggee cannot bind to same port * @summary Test that more than one debuggee cannot bind to same port
* at the same time. * at the same time.
* @library /lib/testlibrary
* *
* @build jdk.testlibrary.ProcessTools jdk.testlibrary.JDKToolLauncher jdk.testlibrary.Utils
* @build VMConnection ExclusiveBind HelloWorld * @build VMConnection ExclusiveBind HelloWorld
* @run main ExclusiveBind * @run main ExclusiveBind
*/ */
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.net.ServerSocket; import java.net.ServerSocket;
import com.sun.jdi.Bootstrap; import com.sun.jdi.Bootstrap;
import com.sun.jdi.VirtualMachine; import com.sun.jdi.VirtualMachine;
import com.sun.jdi.connect.Connector; import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.AttachingConnector; import com.sun.jdi.connect.AttachingConnector;
import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import jdk.testlibrary.ProcessTools;
import jdk.testlibrary.Utils;
public class ExclusiveBind { public class ExclusiveBind {
/*
* Helper class to direct process output to the parent
* System.out
*/
static class IOHandler implements Runnable {
InputStream in;
IOHandler(InputStream in) {
this.in = in;
}
static void handle(InputStream in) {
IOHandler handler = new IOHandler(in);
Thread thr = new Thread(handler);
thr.setDaemon(true);
thr.start();
}
public void run() {
try {
byte b[] = new byte[100];
for (;;) {
int n = in.read(b);
if (n < 0) return;
for (int i=0; i<n; i++) {
System.out.print((char)b[i]);
}
}
} catch (IOException ioe) { }
}
}
/* /*
* Find a connector by name * Find a connector by name
*/ */
@ -95,25 +66,23 @@ public class ExclusiveBind {
* Launch (in server mode) a debuggee with the given address and * Launch (in server mode) a debuggee with the given address and
* suspend mode. * suspend mode.
*/ */
private static Process launch(String address, boolean suspend, String class_name) throws IOException { private static ProcessBuilder prepareLauncher(String address, boolean suspend, String class_name) throws Exception {
String exe = System.getProperty("java.home") + File.separator + "bin" + List<String> args = new ArrayList<>();
File.separator + "java"; for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) {
String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() + args.add(dbgOption);
" -agentlib:jdwp=transport=dt_socket,server=y,suspend=";
if (suspend) {
cmd += "y";
} else {
cmd += "n";
} }
cmd += ",address=" + address + " " + class_name; String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=";
if (suspend) {
lib += "y";
} else {
lib += "n";
}
lib += ",address=" + address;
System.out.println("Starting: " + cmd); args.add(lib);
args.add(class_name);
Process p = Runtime.getRuntime().exec(cmd); return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
IOHandler.handle(p.getInputStream());
IOHandler.handle(p.getErrorStream());
return p;
} }
/* /*
@ -132,16 +101,21 @@ public class ExclusiveBind {
String address = String.valueOf(port); String address = String.valueOf(port);
// launch the first debuggee // launch the first debuggee
Process process1 = launch(address, true, "HelloWorld"); ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
// start the debuggee and wait for the "ready" message
// give first debuggee time to suspend Process p = ProcessTools.startProcess(
Thread.currentThread().sleep(5000); "process1",
process1,
line -> line.equals("Listening for transport dt_socket at address: " + address),
Math.round(5000 * Utils.TIMEOUT_FACTOR),
TimeUnit.MILLISECONDS
);
// launch a second debuggee with the same address // launch a second debuggee with the same address
Process process2 = launch(address, false, "HelloWorld"); ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
// get exit status from second debuggee // get exit status from second debuggee
int exitCode = process2.waitFor(); int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
// clean-up - attach to first debuggee and resume it // clean-up - attach to first debuggee and resume it
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");