From a32c41abe5751bbb828989a7b4365a25928e6238 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Thu, 9 Mar 2017 09:47:57 +1100 Subject: [PATCH] pubsub: fix potential deadlock in startConnections (#1674) If a subscriber connection fails, awaitRunning will throw IllegalStateException. We must make sure that we count down the latch in either case; otherwise startup will deadlock. Also move the call to addListener above startAsync. Otherwise, the listener might not see the connection failing if it fails too quickly. --- .../java/com/google/cloud/pubsub/spi/v1/Subscriber.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java index 8133e4829a68..d71799d937ce 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java @@ -189,7 +189,7 @@ public boolean isRunning() { * } * }, executor); * subscriber.startAsync(); - * + * * // Wait for a stop signal. * done.get(); * subscriber.stopAsync().awaitTerminated(); @@ -466,9 +466,12 @@ private void startConnections( new Runnable() { @Override public void run() { - subscriber.startAsync().awaitRunning(); - subscribersStarting.countDown(); subscriber.addListener(connectionsListener, executor); + try { + subscriber.startAsync().awaitRunning(); + } finally { + subscribersStarting.countDown(); + } } }); }