Caffe2 - Python API
A deep learning, cross platform ML framework
timeout_guard.py
1 
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 import contextlib
9 import threading
10 import os
11 import time
12 import signal
13 import logging
14 
15 
16 '''
17 Sometimes CUDA devices can get stuck, 'deadlock'. In this case it is often
18 better just the kill the process automatically. Use this guard to set a
19 maximum timespan for a python call, such as RunNet(). If it does not complete
20 in time, process is killed.
21 
22 Example usage:
23  with timeout_guard.CompleteInTimeOrDie(10.0):
24  core.RunNet(...)
25 '''
26 
27 
28 class WatcherThread(threading.Thread):
29 
30  def __init__(self, timeout_secs):
31  threading.Thread.__init__(self)
32  self.timeout_secs = timeout_secs
33  self.completed = False
34  self.condition = threading.Condition()
35  self.daemon = True
36  self.caller_thread = threading.current_thread()
37 
38  def run(self):
39  started = time.time()
40  self.condition.acquire()
41  while time.time() - started < self.timeout_secs and not self.completed:
42  self.condition.wait(self.timeout_secs - (time.time() - started))
43  self.condition.release()
44  if not self.completed:
45  log = logging.getLogger("timeout_guard")
46  log.error("Call did not finish in time. Timeout:{}s PID: {}".format(
47  self.timeout_secs,
48  os.getpid(),
49  ))
50 
51  # First try dying cleanly, but in 10 secs, exit properly
52  def forcequit():
53  time.sleep(10.0)
54  log.info("Prepared output, dumping threads. ")
55  print("Caller thread was: {}".format(self.caller_thread))
56  print("-----After force------")
57  import sys
58  import traceback
59  code = []
60  for threadId, stack in sys._current_frames().items():
61  if threadId == self.caller_thread.ident:
62  code.append("\n# ThreadID: %s" % threadId)
63  for filename, lineno, name, line in traceback.extract_stack(stack):
64  code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
65  if line:
66  code.append(" %s" % (line.strip()))
67 
68  print("\n".join(code))
69  log.error("Process did not terminate cleanly in 10 s, forcing")
70  os._exit(1)
71 
72  forcet = threading.Thread(target=forcequit, args=())
73  forcet.daemon = True
74  forcet.start()
75  print("Caller thread was: {}".format(self.caller_thread))
76  print("-----Before forcing------")
77  import sys
78  import traceback
79  code = []
80  for threadId, stack in sys._current_frames().items():
81  code.append("\n# ThreadID: %s" % threadId)
82  for filename, lineno, name, line in traceback.extract_stack(stack):
83  code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
84  if line:
85  code.append(" %s" % (line.strip()))
86 
87  print("\n".join(code))
88  os.kill(os.getpid(), signal.SIGINT)
89 
90 
91 @contextlib.contextmanager
92 def CompleteInTimeOrDie(timeout_secs):
93  watcher = WatcherThread(timeout_secs)
94  watcher.start()
95  yield
96  watcher.completed = True
97  watcher.condition.acquire()
98  watcher.condition.notify()
99  watcher.condition.release()
100 
101 
102 def EuthanizeIfNecessary(timeout_secs=120):
103  '''
104  Call this if you have problem with process getting stuck at shutdown.
105  It will kill the process if it does not terminate in timeout_secs.
106  '''
107  watcher = WatcherThread(timeout_secs)
108  watcher.start()
def EuthanizeIfNecessary(timeout_secs=120)