Skip to content

Commit e5a28f4

Browse files
authored
pubsub: stop Subscriber asynchronously (#3144)
It is hard to do this completely async, but we can use a background thread so it doesn't block user code. Fixes #3065. Fixes #3134.
1 parent 8d499fa commit e5a28f4

File tree

1 file changed

+18
-11
lines changed
  • google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ protected void doStart() {
296296
@Override
297297
public void run() {
298298
try {
299-
startStreamingConnections();
299+
startStreamingConnections();
300300
notifyStarted();
301301
} catch (Throwable t) {
302302
notifyFailed(t);
@@ -308,16 +308,23 @@ public void run() {
308308

309309
@Override
310310
protected void doStop() {
311-
// stop connection is no-op if connections haven't been started.
312-
stopAllStreamingConnections();
313-
try {
314-
for (AutoCloseable closeable : closeables) {
315-
closeable.close();
316-
}
317-
notifyStopped();
318-
} catch (Exception e) {
319-
notifyFailed(e);
320-
}
311+
new Thread(
312+
new Runnable() {
313+
@Override
314+
public void run() {
315+
try {
316+
// stop connection is no-op if connections haven't been started.
317+
stopAllStreamingConnections();
318+
for (AutoCloseable closeable : closeables) {
319+
closeable.close();
320+
}
321+
notifyStopped();
322+
} catch (Exception e) {
323+
notifyFailed(e);
324+
}
325+
}
326+
})
327+
.start();
321328
}
322329

323330
private void startStreamingConnections() throws IOException {

0 commit comments

Comments
 (0)