1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Thread and ThreadGroup that reraise exceptions on the main thread."""
6 # pylint: disable=W0212
13 from pylib
.utils
import watchdog_timer
16 class TimeoutError(Exception):
17 """Module-specific timeout exception."""
21 def LogThreadStack(thread
):
22 """Log the stack for the given thread.
25 thread: a threading.Thread instance.
27 stack
= sys
._current
_frames
()[thread
.ident
]
28 logging
.critical('*' * 80)
29 logging
.critical('Stack dump for thread %r', thread
.name
)
30 logging
.critical('*' * 80)
31 for filename
, lineno
, name
, line
in traceback
.extract_stack(stack
):
32 logging
.critical('File: "%s", line %d, in %s', filename
, lineno
, name
)
34 logging
.critical(' %s', line
.strip())
35 logging
.critical('*' * 80)
38 class ReraiserThread(threading
.Thread
):
39 """Thread class that can reraise exceptions."""
41 def __init__(self
, func
, args
=None, kwargs
=None, name
=None):
45 func: callable to call on a new thread.
46 args: list of positional arguments for callable, defaults to empty.
47 kwargs: dictionary of keyword arguments for callable, defaults to empty.
48 name: thread name, defaults to Thread-N.
50 super(ReraiserThread
, self
).__init
__(name
=name
)
62 def ReraiseIfException(self
):
63 """Reraise exception if an exception was raised in the thread."""
65 raise self
._exc
_info
[0], self
._exc
_info
[1], self
._exc
_info
[2]
67 def GetReturnValue(self
):
68 """Reraise exception if present, otherwise get the return value."""
69 self
.ReraiseIfException()
74 """Overrides Thread.run() to add support for reraising exceptions."""
76 self
._ret
= self
._func
(*self
._args
, **self
._kwargs
)
77 except: # pylint: disable=W0702
78 self
._exc
_info
= sys
.exc_info()
81 class ReraiserThreadGroup(object):
82 """A group of ReraiserThread objects."""
84 def __init__(self
, threads
=None):
85 """Initialize thread group.
88 threads: a list of ReraiserThread objects; defaults to empty.
92 self
._threads
= threads
94 def Add(self
, thread
):
95 """Add a thread to the group.
98 thread: a ReraiserThread object.
100 self
._threads
.append(thread
)
103 """Start all threads."""
104 for thread
in self
._threads
:
107 def _JoinAll(self
, watcher
=None):
108 """Join all threads without stack dumps.
110 Reraises exceptions raised by the child threads and supports breaking
111 immediately on exceptions raised on the main thread.
114 watcher: Watchdog object providing timeout, by default waits forever.
117 watcher
= watchdog_timer
.WatchdogTimer(None)
118 alive_threads
= self
._threads
[:]
120 for thread
in alive_threads
[:]:
121 if watcher
.IsTimedOut():
122 raise TimeoutError('Timed out waiting for %d of %d threads.' %
123 (len(alive_threads
), len(self
._threads
)))
124 # Allow the main thread to periodically check for interrupts.
126 if not thread
.isAlive():
127 alive_threads
.remove(thread
)
128 # All threads are allowed to complete before reraising exceptions.
129 for thread
in self
._threads
:
130 thread
.ReraiseIfException()
132 def JoinAll(self
, watcher
=None):
135 Reraises exceptions raised by the child threads and supports breaking
136 immediately on exceptions raised on the main thread. Unfinished threads'
137 stacks will be logged on watchdog timeout.
140 watcher: Watchdog object providing timeout, by default waits forever.
143 self
._JoinAll
(watcher
)
145 for thread
in (t
for t
in self
._threads
if t
.isAlive()):
146 LogThreadStack(thread
)
149 def GetAllReturnValues(self
, watcher
=None):
150 """Get all return values, joining all threads if necessary.
153 watcher: same as in |JoinAll|. Only used if threads are alive.
155 if any([t
.isAlive() for t
in self
._threads
]):
156 self
.JoinAll(watcher
)
157 return [t
.GetReturnValue() for t
in self
._threads
]