From 1a0b3a0db9f9eab72ddfdd7dbaf6e4801a6126fa Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 5 Jan 2024 02:13:38 -0800 Subject: [PATCH] Retry binding to ipv6 localhost Fixes https://github.com/bazelbuild/bazel/issues/20743 PiperOrigin-RevId: 595935153 Change-Id: I0409552aa92f3886c5abf3bd3ce50d67594dab7e --- .bazelrc | 3 +-- .../build/lib/server/GrpcServerImpl.java | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.bazelrc b/.bazelrc index cec53955f10c50..ca34565a1bd64e 100644 --- a/.bazelrc +++ b/.bazelrc @@ -90,8 +90,7 @@ test:ci-macos --test_env=TEST_INSTALL_BASE=/Users/buildkite/bazeltest/install_ba test:ci-macos --test_env=REPOSITORY_CACHE=/Users/buildkite/bazeltest/repo_cache test:ci-macos --test_env=REMOTE_NETWORK_ADDRESS=bazel.build:80 test:ci-macos --sandbox_writable_path=/Users/buildkite/bazeltest -# TODO(pcloudy): Set this to false after fixing https://github.com/bazelbuild/bazel/issues/20743 -test:ci-macos --sandbox_default_allow_network=true +test:ci-macos --sandbox_default_allow_network=false ## For Windows common:ci-windows --config=ci-common diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java index df3a56b418cde6..bf4a4a36b7f339 100644 --- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java +++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java @@ -46,6 +46,7 @@ import com.google.devtools.build.lib.util.DetailedExitCode; import com.google.devtools.build.lib.util.ExitCode; import com.google.devtools.build.lib.util.InterruptedFailureDetails; +import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.util.io.CommandExtensionReporter; import com.google.devtools.build.lib.util.io.OutErr; @@ -407,6 +408,26 @@ public void interrupt() { commandManager.interruptInflightCommands(); } + private Server bindIpv6WithRetries(InetSocketAddress address, int maxRetries) throws IOException { + Server server = null; + for (int attempt = 1; attempt <= maxRetries; attempt++) { + try { + server = + NettyServerBuilder.forAddress(address) + .addService(this) + .directExecutor() + .build() + .start(); + break; + } catch (IOException e) { + if (attempt == maxRetries) { + throw e; + } + } + } + return server; + } + @Override public void serve() throws AbruptExitException { Preconditions.checkState(!serving); @@ -422,8 +443,10 @@ public void serve() throws AbruptExitException { if (Epoll.isAvailable() && !Socket.isIPv6Preferred()) { throw new IOException("ipv6 is not preferred on the system."); } - server = - NettyServerBuilder.forAddress(address).addService(this).directExecutor().build().start(); + // For some strange reasons, Bazel server sometimes fails to bind to IPv6 localhost when + // running in macOS sandbox-exec with internet blocked. Retrying seems to help. + // See https://github.com/bazelbuild/bazel/issues/20743 + server = bindIpv6WithRetries(address, OS.getCurrent() == OS.DARWIN ? 3 : 1); } catch (IOException ipv6Exception) { address = new InetSocketAddress("127.0.0.1", port); try {