Merge branch 'stable-3.0' into stable-3.1

* stable-3.0:
  Prevent persistent task listing interruptions on IOExceptions

Change-Id: Ie03efcc94ff8001edee557a6a413233085fab1b6
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java
index 8d4b10a..d15f02d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java
@@ -27,6 +27,7 @@
 import java.nio.file.DirectoryIteratorException;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
@@ -154,13 +155,21 @@
   private List<ReplicateRefUpdate> list(Path tasks) {
     List<ReplicateRefUpdate> results = new ArrayList<>();
     try (DirectoryStream<Path> events = Files.newDirectoryStream(tasks)) {
-      for (Path e : events) {
-        if (Files.isRegularFile(e)) {
-          String json = new String(Files.readAllBytes(e), UTF_8);
-          results.add(GSON.fromJson(json, ReplicateRefUpdate.class));
-        } else if (Files.isDirectory(e)) {
+      for (Path path : events) {
+        if (Files.isRegularFile(path)) {
           try {
-            results.addAll(list(e));
+            String json = new String(Files.readAllBytes(path), UTF_8);
+            results.add(GSON.fromJson(json, ReplicateRefUpdate.class));
+          } catch (NoSuchFileException ex) {
+            logger.atFine().log(
+                "File %s not found while listing waiting tasks (likely in-flight or completed by another node)",
+                path);
+          } catch (IOException e) {
+            logger.atSevere().withCause(e).log("Error when firing pending event %s", path);
+          }
+        } else if (Files.isDirectory(path)) {
+          try {
+            results.addAll(list(path));
           } catch (DirectoryIteratorException d) {
             // iterating over the sub-directories is expected to have dirs disappear
             Nfs.throwIfNotStaleFileHandle(d.getCause());