Skip to content

Commit

Permalink
fix: align ip version preference for the wrapped emulator (#1052)
Browse files Browse the repository at this point in the history
* fix: align ip version preference for the wrapped  emulator

When the  emulator wrapper is started with -Djava.net.preferIPv6Addresses=true on a machine that defaults to ipv4,
the golang emulator and the java client will pick different ip stacks and not be able to connect. This change will
use java to resolve the localhost's ip address and push it down to the golang emulator

* remove debug

* whitespace
  • Loading branch information
igorbernstein2 committed Jan 5, 2022
1 parent 1ee5773 commit 9dc93c5
Showing 1 changed file with 18 additions and 1 deletion.
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
// -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

0 comments on commit 9dc93c5

Please sign in to comment.