Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align ip version preference for the wrapped emulator #1052

Merged
merged 4 commits into from Jan 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,10 +25,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
Expand Down Expand Up @@ -98,10 +101,24 @@ public synchronized void start() throws IOException, TimeoutException, Interrupt
}
this.port = getAvailablePort();

// Try to align the localhost address across java & golang emulator
// This should fix issues on systems that default to ipv4 but the jvm is started with
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
// -Djava.net.preferIPv6Addresses=true
Optional<String> localhostAddress = Optional.empty();
try {
localhostAddress = Optional.of(InetAddress.getByName(null).getHostAddress());
} catch (UnknownHostException e) {
}

// Workaround https://bugs.openjdk.java.net/browse/JDK-8068370
for (int attemptsLeft = 3; process == null; attemptsLeft--) {
try {
process = Runtime.getRuntime().exec(String.format("%s -port %d", executable, port));
String cmd = executable.toString();
if (localhostAddress.isPresent()) {
cmd += String.format(" -host [%s]", localhostAddress.get());
}
cmd += String.format(" -port %d", port);
process = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
if (attemptsLeft > 0) {
Thread.sleep(1000);
Expand Down Expand Up @@ -242,6 +259,13 @@ private static String getBundledResourcePath() {

/** Gets a random open port number. */
private static int getAvailablePort() {
try {
InetAddress moo = InetAddress.getByName(null);
System.out.println(moo);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like some cleanup needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well thats just embarrassing 😳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be good now

} catch (UnknownHostException e) {
e.printStackTrace();
}

try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
} catch (IOException e) {
Expand Down