Index: /trunk/FACT++/scripts/takeRun.js
===================================================================
--- /trunk/FACT++/scripts/takeRun.js	(revision 17925)
+++ /trunk/FACT++/scripts/takeRun.js	(revision 17926)
@@ -173,4 +173,28 @@
     //}
 
+    // ================================================================
+    //  Function for Critical voltage
+    // ================================================================
+
+    // INSTALL a watchdog... send FAD_CONTROL/CLOSE_OPEN_FILES
+    // could send MCP/RESET as well but would result in a timeout
+    var callback = dim.onchange['FEEDBACK'];
+    dim.onchange['FEEDBACK'] = function(state)
+    {
+        if (callback)
+            callback.call(this, state);
+
+        if ((state.name=="Critical" || state.name=="OnStandby") &&
+            (this.last!="Critical"  && this.last!="OnStandby"))
+        {
+            console.out("Feedback state changed from "+this.last+" to "+state.name+" [takeRun.js]");
+
+            // Includes FAD_CONTROL/CLOSE_ALL_OPEN_FILES
+            dim.send("MCP/STOP");
+        }
+
+        this.last=state.name;
+    }
+
     // Here we could check and handle fad losses
 
@@ -241,5 +265,22 @@
     }
 
+    // This is to check if we have missed the event. This can happen as
+    // a race condition when the MCP/STOP is sent by the event handler
+    // but the run was not yet fully configured.
+    var statefb = dim.state("FEEDBACK").name;
+    if (statefb=="Critical" || statefb=="OnStandby")
+    {
+        console.out("Run started by FEEDBACK in state "+statefb);
+        dim.send("MCP/STOP"); // Includes FAD_CONTROL/CLOSE_ALL_OPEN_FILES
+
+        dim.onchange['FEEDBACK'] = callback;
+
+        return true;
+    }
+
     dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
+
+    // REMOVE watchdog
+    dim.onchange['FEEDBACK'] = callback;
 
     if (incomplete)
