[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Avoid twisted complains about missing pidfile during shutdown



commit 5414f5227973587daf67440501cfc71e454f1d65
Author: Leonid Evdokimov <leon@xxxxxxxxxxxx>
Date:   Wed Oct 12 18:18:20 2016 +0300

    Avoid twisted complains about missing pidfile during shutdown
    
    Twisted wants to clean it's pidfiles itself, so `ooniprobe-agent` should
    remove the pidfile if and only if it's stale.
---
 ooni/scripts/ooniprobe_agent.py | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/ooni/scripts/ooniprobe_agent.py b/ooni/scripts/ooniprobe_agent.py
index e670d86..d7f4750 100644
--- a/ooni/scripts/ooniprobe_agent.py
+++ b/ooni/scripts/ooniprobe_agent.py
@@ -121,6 +121,17 @@ def get_running_pidfile():
             return pidfile
     raise NotRunning
 
+def is_stale_pidfile(pidfile):
+    try:
+        with open(pidfile) as fd:
+            pid = int(fd.read())
+    except Exception:
+        return False # that's either garbage in the pid-file or a race
+    return not is_process_running(pid)
+
+def get_stale_pidfiles():
+    return [f for f in [config.system_pid_path, config.user_pid_path] if is_stale_pidfile(f)]
+
 def status_agent():
     try:
         get_running_pidfile()
@@ -130,7 +141,7 @@ def status_agent():
         print("ooniprobe-agent is NOT running")
         return 1
 
-def stop_agent():
+def do_stop_agent():
     # This function is borrowed from tahoe
     try:
         pidfile = get_running_pidfile()
@@ -144,12 +155,7 @@ def stop_agent():
         os.kill(pid, signal.SIGTERM)
     except OSError as ose:
         if ose.errno == errno.ESRCH:
-            print("No process was running. Cleaning up.")
-            # the process didn't exist, so wipe the pid file
-            try:
-                os.remove(pidfile)
-            except EnvironmentError as exc:
-                print("Failed to delete the pidfile {0}".format(exc))
+            print("No process was running.") # it's just a race
             return 2
         elif ose.errno == errno.EPERM:
             # The process is owned by root. We assume it's running
@@ -157,10 +163,7 @@ def stop_agent():
             return 3
         else:
             raise
-    try:
-        os.remove(pidfile)
-    except EnvironmentError:
-        pass
+    # the process wants to clean it's own pidfile itself
     start = time.time()
     time.sleep(0.1)
     wait = 40
@@ -197,6 +200,16 @@ def stop_agent():
     # we define rc=1 to mean "I think something is still running, sorry"
     return 1
 
+def stop_agent():
+    retval = do_stop_agent()
+    for pidfile in get_stale_pidfiles():
+        try:
+            os.remove(pidfile)
+            print("Cleaned up stale pidfile {0}".format(pidfile))
+        except EnvironmentError:
+            print("Failed to delete the pidfile {0}: {1}".format(pidfile, exc))
+    return retval
+
 def run():
     options = AgentOptions()
     options.parseOptions()



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits