8181383: com/sun/jdi/OptionTest.java fails intermittently with bind failed: Address already in use

Reviewed-by: sspitsyn, cjplummer
This commit is contained in:
Alex Menkov 2019-02-26 15:29:08 -08:00
parent 57c2735dd0
commit f16ddb2c3d
2 changed files with 25 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, 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,6 @@
* @run driver OptionTest
*/
import java.net.ServerSocket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OptionTest extends Object {
@ -127,18 +125,12 @@ public class OptionTest extends Object {
}
public static void main(String[] args) throws Exception {
// find a free port
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
ss.close();
String address = String.valueOf(port);
String javaExe = System.getProperty("java.home") +
java.io.File.separator + "bin" +
java.io.File.separator + "java";
String targetClass = "HelloWorld";
String baseOptions = "transport=dt_socket" +
",address=" + address +
",address=0" +
",server=y" +
",suspend=n";

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, 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
@ -30,7 +30,6 @@
* @build VMConnection RunToExit Exit0
* @run driver RunToExit
*/
import java.net.ServerSocket;
import com.sun.jdi.Bootstrap;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.event.*;
@ -41,6 +40,8 @@ import java.util.Map;
import java.util.List;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import jdk.test.lib.process.ProcessTools;
@ -50,6 +51,9 @@ public class RunToExit {
static volatile int error_seen = 0;
static volatile boolean ready = false;
/* port the debuggee is listening on */
private static String address;
/*
* Find a connector by name
*/
@ -66,12 +70,11 @@ public class RunToExit {
}
/*
* Launch a server debuggee with the given address
* Launch a server debuggee, detect debuggee listening port
*/
private static Process launch(String address, String class_name) throws Exception {
private static Process launch(String class_name) throws Exception {
String args[] = new String[]{
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
+ address,
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0",
class_name
};
args = VMConnection.insertDebuggeeVMOptions(args);
@ -92,8 +95,17 @@ public class RunToExit {
return p;
}
/* warm-up predicate for debuggee */
private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
private static boolean isTransportListening(String line) {
return line.startsWith("Listening for transport dt_socket");
Matcher m = listenRegexp.matcher(line);
if (!m.matches()) {
return false;
}
// address is 2nd group
address = m.group(2);
return true;
}
private static void checkForError(String line) {
@ -103,28 +115,21 @@ public class RunToExit {
}
/*
* - pick a TCP port
* - Launch a server debuggee: server=y,suspend=y,address=${port}
* - Launch a server debuggee: server=y,suspend=y,address=0
* - detect the port debuggee is listening on
* - run it to VM death
* - verify we saw no error
*/
public static void main(String args[]) throws Exception {
// find a free port
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
ss.close();
String address = String.valueOf(port);
// launch the server debuggee
Process process = launch(address, "Exit0");
Process process = launch("Exit0");
// attach to server debuggee and resume it so it can exit
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
Map conn_args = conn.defaultArguments();
Connector.IntegerArgument port_arg =
(Connector.IntegerArgument)conn_args.get("port");
port_arg.setValue(port);
port_arg.setValue(address);
System.out.println("Connection arguments: " + conn_args);