From b48b4db3fb3577ed21da8f05e3c0428a991e3eed Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 26 Mar 2015 07:35:07 -0700 Subject: [PATCH] Fix synchronization in client auth The synchronization was simply using the wrong object, so no synchronization was actually occurring. In addition, reference of cached outside of synchronized block could permit using a partially constructed cached object, as the reference may be set before instantiation completes. --- .../src/main/java/io/grpc/auth/ClientAuthInterceptor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java index 17c3aaec59..3351aa7155 100644 --- a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java +++ b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java @@ -74,7 +74,8 @@ public class ClientAuthInterceptor implements ClientInterceptor { @Override public void start(Listener responseListener, Metadata.Headers headers) { try { - synchronized (this) { + Metadata.Headers cachedSaved; + synchronized (ClientAuthInterceptor.this) { // TODO(lryan): This is icky but the current auth library stores the same // metadata map until the next refresh cycle. This will be fixed once // https://github.com/google/google-auth-library-java/issues/3 @@ -83,8 +84,9 @@ public class ClientAuthInterceptor implements ClientInterceptor { lastMetadata = credentials.getRequestMetadata(); cached = toHeaders(lastMetadata); } + cachedSaved = cached; } - headers.merge(cached); + headers.merge(cachedSaved); super.start(responseListener, headers); } catch (IOException ioe) { responseListener.onClose(Status.fromThrowable(ioe), new Metadata.Trailers()); @@ -105,4 +107,4 @@ public class ClientAuthInterceptor implements ClientInterceptor { } return headers; } -} \ No newline at end of file +}