Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

fix(logging): catch and log exceptions in Watchdog runnable to ensure it never stops running #1036

Merged
merged 4 commits into from Jun 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions gax/src/main/java/com/google/api/gax/rpc/Watchdog.java
Expand Up @@ -40,6 +40,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.GuardedBy;
import org.threeten.bp.Duration;
Expand All @@ -61,6 +63,8 @@
*/
@InternalApi
public class Watchdog implements Runnable, BackgroundResource {
private static final Logger LOG = Logger.getLogger(Watchdog.class.getName());

// Dummy value to convert the ConcurrentHashMap into a Set
private static Object PRESENT = new Object();
private final ConcurrentHashMap<WatchdogStream, Object> openStreams = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -111,6 +115,14 @@ public <ResponseT> ResponseObserver<ResponseT> watch(

@Override
public void run() {
try {
runUnsafe();
} catch (Throwable t) {
LOG.log(Level.SEVERE, "Caught throwable in periodic Watchdog run. Continuing.", t);
}
}

private void runUnsafe() {
Iterator<Entry<WatchdogStream, Object>> it = openStreams.entrySet().iterator();

while (it.hasNext()) {
Expand Down