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

Ensure NetworkTime never throw exceptions inside onClose #12422

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:flogger_checked_in",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/grpc:grpc-jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import build.bazel.remote.execution.v2.ExecutionGrpc;
import com.google.common.base.Stopwatch;
import com.google.common.flogger.GoogleLogger;
import com.google.devtools.build.lib.concurrent.ThreadSafety;
import io.grpc.CallOptions;
import io.grpc.Channel;
Expand All @@ -31,6 +32,8 @@
/** Reentrant wall clock stopwatch and grpc interceptor for network waits. */
@ThreadSafety.ThreadSafe
public class NetworkTime {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

public static final Context.Key<NetworkTime> CONTEXT_KEY = Context.key("remote-network-time");

private final Stopwatch wallTime = Stopwatch.createUnstarted();
Expand Down Expand Up @@ -87,9 +90,20 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(
responseListener) {

/**
* This method must not throw any exceptions. Doing so will cause the wrapped call to
* silently hang indefinitely: https://github.com/grpc/grpc-java/pull/6107
*/
@Override
public void onClose(Status status, Metadata trailers) {
networkTime.stop();
// There is a risk that networkTime.stop() would throw a IllegalStateException: if
// networkTime.outstanding is overflowed, wallTime.stop() will be called even it's
// already stopped.
try {
networkTime.stop();
} catch (RuntimeException e) {
logger.atWarning().withCause(e).log("Failed to stop networkTime");
}
super.onClose(status, trailers);
}
},
Expand Down