[Swift-commit] r5187 - SwiftApps/SwiftR/Swift/exec

tga at ci.uchicago.edu tga at ci.uchicago.edu
Wed Sep 28 13:32:43 CDT 2011


Author: tga
Date: 2011-09-28 13:32:43 -0500 (Wed, 28 Sep 2011)
New Revision: 5187

Modified:
   SwiftApps/SwiftR/Swift/exec/EvalRBatchPersistent.sh
Log:
Properly clean up timer processes


Modified: SwiftApps/SwiftR/Swift/exec/EvalRBatchPersistent.sh
===================================================================
--- SwiftApps/SwiftR/Swift/exec/EvalRBatchPersistent.sh	2011-09-28 18:06:07 UTC (rev 5186)
+++ SwiftApps/SwiftR/Swift/exec/EvalRBatchPersistent.sh	2011-09-28 18:32:43 UTC (rev 5187)
@@ -109,11 +109,18 @@
 function start_idletimer {
   # After the specified number of seconds
   # kill the specified R process
+
+  # Cancel old timeout
+  if [ -f $SLOTDIR/idletimer_pid ] ; then
+    term_timeout $(cat $SLOTDIR/idletimer_pid)
+  fi
+
   local timeout=$1
   local rpid=$2
   local idletimer_id=$$$(date '+%s%N') # timestamp in ns plus pid should be unique
   echo "$idletimer_id" > $SLOTDIR/idletimer
   (
+    trap "exit 0" 1 # timeout cancelled with SIGHUP
     sleep ${timeout}s
     
     # if mutex has been acquired, know another process active
@@ -122,12 +129,14 @@
         if [ "$new_idletimer_id" = "$idletimer_id" ]; then
           echo killing idle R process $rpid
           kill $rpid
-          rm -f $SLOTDIR/idletimer $SLOTDIR/fromR.fifo $SLOTDIR/toR.fifo
+          rm -f $SLOTDIR/idletimer $SLOTDIR/idletimer_pid \
+                $SLOTDIR/fromR.fifo $SLOTDIR/toR.fifo
           touch $SLOTDIR/timedout
         fi
         rmdir $SLOTDIR/mutex
     fi
   ) &> /dev/null &
+  echo $! > $SLOTDIR/idletimer_pid
 }
 
 # Ensure that the dir for this slot exists. 
@@ -162,6 +171,11 @@
 IDLE_TIMEOUT=120
 timeout_pid=
 
+function term_timeout {
+    local pid=$1
+    local others=`ps -o pid --no-headers --ppid $pid`
+    kill -1 "$pid" $others &> /dev/null
+}
 
 function start_timeout {
     local ppid=$$
@@ -182,7 +196,8 @@
     #DEBUG
     trap "" SIGHUP
     if [ ! -z "$timeout_pid" ]; then
-        kill -1 $timeout_pid
+        # kill timeout and sleep process 
+        term_timeout $timeout_pid &
         timeout_pid=
     fi
 }




More information about the Swift-commit mailing list