Skip to content

Commit

Permalink
Pick random debug port when the configured one is taken
Browse files Browse the repository at this point in the history
Closes: #33363
  • Loading branch information
geoand committed May 23, 2023
1 parent 4c5e960 commit 32d3518
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,30 @@ protected void prepare() throws Exception {
if (debug != null && debug.equalsIgnoreCase("client")) {
args.add("-agentlib:jdwp=transport=dt_socket,address=" + debugHost + ":" + port + ",server=n,suspend=" + suspend);
} else if (debug == null || !debug.equalsIgnoreCase("false")) {
// make sure the debug port is not used, we don't want to just fail if something else is using it
// we don't check this on restarts, as the previous process is still running
// if the debug port is used, we want to make an effort to pick another one
// if we can't find an open port, we don't fail the process launch, we just don't enable debugging
// Furthermore, we don't check this on restarts, as the previous process is still running
if (debugPortOk == null) {
try (Socket socket = new Socket(getInetAddress(debugHost), port)) {
error("Port " + port + " in use, not starting in debug mode");
debugPortOk = false;
} catch (IOException e) {
debugPortOk = true;
int tries = 0;
while (true) {
boolean isPortUsed;
try (Socket socket = new Socket(getInetAddress(debugHost), port)) {
// we can make a connection, that means the port is in use
isPortUsed = true;
} catch (IOException e) {
// no connection made, so the port is not in use
isPortUsed = false;
}
if (!isPortUsed) {
debugPortOk = true;
break;
}
if (++tries >= 5) {
debugPortOk = false;
break;
} else {
port = getRandomPort();
}
}
}
if (debugPortOk) {
Expand Down

0 comments on commit 32d3518

Please sign in to comment.