You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2018/09/14 14:06:06 UTC

[tika] branch branch_1x updated: TIKA-2725 -- add synchronization to avoid potential NPE in watcher thread

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch branch_1x
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/branch_1x by this push:
     new 5211fc7  TIKA-2725 -- add synchronization to avoid potential NPE in watcher thread
5211fc7 is described below

commit 5211fc79e24672bf1418f6016c9bcc891e438049
Author: TALLISON <ta...@apache.org>
AuthorDate: Fri Sep 14 10:05:51 2018 -0400

    TIKA-2725 -- add synchronization to avoid potential NPE in watcher thread
---
 .../org/apache/tika/server/TikaServerWatchDog.java | 31 +++++++++++++++++-----
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/tika-server/src/main/java/org/apache/tika/server/TikaServerWatchDog.java b/tika-server/src/main/java/org/apache/tika/server/TikaServerWatchDog.java
index 67007f2..d9b3c54 100644
--- a/tika-server/src/main/java/org/apache/tika/server/TikaServerWatchDog.java
+++ b/tika-server/src/main/java/org/apache/tika/server/TikaServerWatchDog.java
@@ -31,7 +31,16 @@ import java.util.concurrent.TimeUnit;
 
 public class TikaServerWatchDog {
 
+    private enum CHILD_STATUS {
+        INITIALIZING,
+        RUNNING,
+        SHUTTING_DOWN
+    }
+
     private static final Logger LOG = LoggerFactory.getLogger(TikaServerWatchDog.class);
+
+    private Object[] childStatusLock = new Object[0];
+    private volatile CHILD_STATUS childStatus = CHILD_STATUS.INITIALIZING;
     private volatile Instant lastPing = null;
     private ChildProcess childProcess = null;
     int restarts = 0;
@@ -46,12 +55,10 @@ public class TikaServerWatchDog {
             public void run() {
                 while (true) {
                     long tmpLastPing = -1L;
-                    try {
-                        //TODO: clean this up with synchronization/locking
-                        //to avoid potential NPE
-                        tmpLastPing = lastPing.toEpochMilli();
-                    } catch (NullPointerException e) {
-
+                    synchronized (childStatusLock) {
+                        if (childStatus == CHILD_STATUS.RUNNING) {
+                            tmpLastPing = lastPing.toEpochMilli();
+                        }
                     }
                     if (tmpLastPing > 0) {
                         long elapsed = Duration.between(Instant.ofEpochMilli(tmpLastPing), Instant.now()).toMillis();
@@ -78,27 +85,36 @@ public class TikaServerWatchDog {
         pingTimer.start();
         try {
             childProcess = new ChildProcess(args);
-
+            setChildStatus(CHILD_STATUS.RUNNING);
             while (true) {
 
                 if (!childProcess.ping()) {
+                    setChildStatus(CHILD_STATUS.INITIALIZING);
                     lastPing = null;
                     childProcess.close();
                     LOG.info("About to restart the child process");
                     childProcess = new ChildProcess(args);
                     LOG.info("Successfully restarted child process -- {} restarts so far)", ++restarts);
+                    setChildStatus(CHILD_STATUS.RUNNING);
                 }
                 Thread.sleep(serverTimeouts.getPingPulseMillis());
             }
         } catch (InterruptedException e) {
             //interrupted...shutting down
         } finally {
+            setChildStatus(CHILD_STATUS.SHUTTING_DOWN);
             if (childProcess != null) {
                 childProcess.close();
             }
         }
     }
 
+    private void setChildStatus(CHILD_STATUS status) {
+        synchronized (childStatusLock) {
+            childStatus = status;
+        }
+    }
+
     private static List<String> extractArgs(String[] args) {
         List<String> argList = new ArrayList<>();
         for (int i = 0; i < args.length; i++) {
@@ -139,6 +155,7 @@ public class TikaServerWatchDog {
                 throw new IOException("bad status from child process: "+
                         ServerStatus.STATUS.lookup(status));
             }
+            lastPing = Instant.now();
         }
 
         public boolean ping() {