From 3ee803c2ba32702c3476b517f8df4c4aa5f578d4 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Sun, 9 Sep 2012 18:04:52 +0200 Subject: [PATCH] [entropy.const] implement a debug watchdog thread dumper thread. If ETP_DEBUG_WATCHDOG env variable is set, entropy.const will create a timer thread that prints to stderr the full application thread dump. This is quite useful in case of hard to reproduce deadlocks at the library level. --- lib/entropy/const.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/entropy/const.py b/lib/entropy/const.py index c61df11b9..050bd2f9f 100644 --- a/lib/entropy/const.py +++ b/lib/entropy/const.py @@ -1676,3 +1676,34 @@ def const_get_cpus(): # load config initconfig_entropy_constants(etpSys['rootdir']) + +# Debug Watchdog support. If enabled, a thread dump +# will be pushed to stderr every ETP_DEBUG_WATCHDOG_INTERVAL +# seconds (or 60 seconds if unset). +_debug_watchdog = os.getenv("ETP_DEBUG_WATCHDOG") +if _debug_watchdog is not None: + from threading import Timer + _default_debug_watchdog_interval = 60 + _debug_watchdog_interval = os.getenv( + "ETP_DEBUG_WATCHDOG_INTERVAL", + _default_debug_watchdog_interval) + try: + _debug_watchdog_interval = int(_debug_watchdog_interval) + except (ValueError, TypeError): + _debug_watchdog_interval = _default_debug_watchdog_interval + + const_debug_write( + __name__, + "DebugWatchdogTimer enabled, interval: %d" % ( + _debug_watchdog_interval,)) + + def _dumper(): + dump_signal(None, None) + _setup_timer() + + def _setup_timer(): + _timer = Timer(_debug_watchdog_interval, _dumper) + _timer.name = "DebugWatchdogTimer" + _timer.daemon = True + _timer.start() + _setup_timer()