Skip to content

Commit d02fd57

Browse files
pongadrybosome
authored andcommitted
pubsub: fix potential deadlock in startConnections (googleapis#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.
1 parent 827e519 commit d02fd57

File tree

1 file changed

+6
-3
lines changed
  • google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1

1 file changed

+6
-3
lines changed

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public boolean isRunning() {
189189
* }
190190
* }, executor);
191191
* subscriber.startAsync();
192-
*
192+
*
193193
* // Wait for a stop signal.
194194
* done.get();
195195
* subscriber.stopAsync().awaitTerminated();
@@ -466,9 +466,12 @@ private void startConnections(
466466
new Runnable() {
467467
@Override
468468
public void run() {
469-
subscriber.startAsync().awaitRunning();
470-
subscribersStarting.countDown();
471469
subscriber.addListener(connectionsListener, executor);
470+
try {
471+
subscriber.startAsync().awaitRunning();
472+
} finally {
473+
subscribersStarting.countDown();
474+
}
472475
}
473476
});
474477
}

0 commit comments

Comments
 (0)