From 8ae418d33320135a4eea3c864065aa6868497eeb Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sun, 21 Oct 2018 17:21:56 -0400 Subject: cmdloop now checks to see if it is running in the main thread before attempting to register a signal handler for SIGINT --- cmd2/cmd2.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'cmd2/cmd2.py') diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index e08db8c5..b9e6aa99 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3588,10 +3588,12 @@ a..b, a:b, a:, ..b items by indices (inclusive) if callargs: self.cmdqueue.extend(callargs) - # Register a SIGINT signal handler for Ctrl+C - import signal - original_sigint_handler = signal.getsignal(signal.SIGINT) - signal.signal(signal.SIGINT, self.sigint_handler) + # Only the main thread is allowed to set a new signal handler in Python, so only attempt if that is the case + if threading.current_thread() is threading.main_thread(): + # Register a SIGINT signal handler for Ctrl+C + import signal + original_sigint_handler = signal.getsignal(signal.SIGINT) + signal.signal(signal.SIGINT, self.sigint_handler) # Grab terminal lock before the prompt has been drawn by readline self.terminal_lock.acquire() @@ -3625,8 +3627,9 @@ a..b, a:b, a:, ..b items by indices (inclusive) # This will also zero the lock count in case cmdloop() is called again self.terminal_lock.release() - # Restore the original signal handler - signal.signal(signal.SIGINT, original_sigint_handler) + if threading.current_thread() is threading.main_thread(): + # Restore the original signal handler + signal.signal(signal.SIGINT, original_sigint_handler) if self.exit_code is not None: sys.exit(self.exit_code) -- cgit v1.2.1