From 95a44e059233e188081dd0f679f058b35efc2e1c Mon Sep 17 00:00:00 2001 From: Larry Safran <107004254+larry-safran@users.noreply.github.com> Date: Thu, 2 Mar 2023 23:28:37 +0000 Subject: [PATCH] Move retrieval of elapsed time in handleRpcStreamClosed to make TSan happy (#9923) * In `handleRpcStreamClosed()`, move retry handling to before the call to `xdsResponseHandler.handleStreamClosed()` so that TSan doesn't report a race condition that is completely meaningless. fixes #9920 --- .../java/io/grpc/xds/AbstractXdsClient.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/xds/src/main/java/io/grpc/xds/AbstractXdsClient.java b/xds/src/main/java/io/grpc/xds/AbstractXdsClient.java index b538461692..57e0575890 100644 --- a/xds/src/main/java/io/grpc/xds/AbstractXdsClient.java +++ b/xds/src/main/java/io/grpc/xds/AbstractXdsClient.java @@ -330,6 +330,17 @@ final class AbstractXdsClient { return; } + if (responseReceived || retryBackoffPolicy == null) { + // Reset the backoff sequence if had received a response, or backoff sequence + // has never been initialized. + retryBackoffPolicy = backoffPolicyProvider.get(); + } + // Need this here to avoid tsan race condition in XdsClientImplTestBase.sendToNonexistentHost + long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); + long delayNanos = Math.max(0, retryBackoffPolicy.nextBackoffNanos() - elapsed); + rpcRetryTimer = syncContext.schedule( + new RpcRetryTask(), delayNanos, TimeUnit.NANOSECONDS, timeService); + checkArgument(!error.isOk(), "unexpected OK status"); String errorMsg = error.getDescription() != null && error.getDescription().equals(CLOSED_BY_SERVER) @@ -341,17 +352,7 @@ final class AbstractXdsClient { xdsResponseHandler.handleStreamClosed(error); cleanUp(); - if (responseReceived || retryBackoffPolicy == null) { - // Reset the backoff sequence if had received a response, or backoff sequence - // has never been initialized. - retryBackoffPolicy = backoffPolicyProvider.get(); - } - long delayNanos = Math.max( - 0, - retryBackoffPolicy.nextBackoffNanos() - stopwatch.elapsed(TimeUnit.NANOSECONDS)); logger.log(XdsLogLevel.INFO, "Retry ADS stream in {0} ns", delayNanos); - rpcRetryTimer = syncContext.schedule( - new RpcRetryTask(), delayNanos, TimeUnit.NANOSECONDS, timeService); } private void close(Exception error) {