From 1d87c635043c7cab027ddc346b2228e695dbd5ae Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sat, 27 May 2017 19:02:05 +0200 Subject: [PATCH] Traceback on Ctrl-C - Ctrl-C will print a traceback of all threads instead of complaining --- src/helper_generic.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/helper_generic.py b/src/helper_generic.py index f2a293cd..26d65dcb 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -4,6 +4,7 @@ import sys from binascii import hexlify, unhexlify from multiprocessing import current_process from threading import current_thread, enumerate +import traceback from bmconfigparser import BMConfigParser from debug import logger @@ -29,10 +30,20 @@ def convertIntToString(n): else: return unhexlify('0' + a[2:]) - def convertStringToInt(s): return int(hexlify(s), 16) +def allThreadTraceback(frame): + id2name = dict([(th.ident, th.name) for th in enumerate()]) + code = [] + for threadId, stack in sys._current_frames().items(): + code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId)) + for filename, lineno, name, line in traceback.extract_stack(stack): + code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) + if line: + code.append(" %s" % (line.strip())) + print "\n".join(code) + def signal_handler(signal, frame): logger.error("Got signal %i in %s/%s", signal, current_process().name, current_thread().name) if current_process().name == "RegExParser": @@ -46,6 +57,7 @@ def signal_handler(signal, frame): if BMConfigParser().safeGetBoolean('bitmessagesettings', 'daemon'): shutdown.doCleanShutdown() else: + allThreadTraceback(frame) print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' def isHostInPrivateIPRange(host):