Skip to content

Commit 74a7aa1

Browse files
committed
pr comment
1 parent 4c988ec commit 74a7aa1

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

  • google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ public void close() {
243243
throw new IllegalStateException(e);
244244
}
245245

246+
// When started, connections submit tasks to the executor.
247+
// These tasks must finish before the connections can declare themselves running.
248+
// If we have a single-thread executor and call startPollingConnections from the
249+
// same executor, it will deadlock: the thread will be stuck waiting for connections
250+
// to start but cannot start the connections.
251+
// For this reason, we spawn a dedicated thread. Starting subscriber should be rare.
246252
new Thread(new Runnable() {
247253
@Override
248254
public void run() {
@@ -395,6 +401,8 @@ private void startConnections(
395401
List<? extends ApiService> connections, final ApiService.Listener connectionsListener) {
396402
for (ApiService subscriber : connections) {
397403
subscriber.addListener(connectionsListener, executor);
404+
// Starting each connection submits a blocking task to the executor.
405+
// We start connections one at a time to avoid swamping executor with blocking tasks.
398406
subscriber.startAsync().awaitRunning();
399407
}
400408
}

0 commit comments

Comments
 (0)