The problem appears to be that the work of either stopThreads()
or sys.exit()
is not reentrant, and the second SIGTERM causes one or both of those to be called in the middle of itself.
Now, you can't prevent the SIGTERM from being delivered multiple times, but you can rework your logic to accommodate it. The usual (and usually correct) advice is to refactor your signal handler to simply set a flag, and check for that flag in your main loop:
def termHandler(self, *args):
global flag_exit
global exit_code
flag_exit = True
exit_code = 2
... in the main loop ...
while not flag_exit:
do_work()
self.stopThreads()
sys.exit(exit_code)