Pourquoi python ne quitte pas ce signal

J’ai un script.py qui lance un processus enfant et fait un peu de travail en boucle. Ce programme existe la plupart du temps correctement lorsqu’il est envoyé à un SIGUSR2 et dans certains cas rares, je ne le comprends pas. Voici l’exemple de travail minimal

 import os, subprocess,signal,sys,time sub = None shutDown = False def handler(signum,frame): global shutDown, sub print("script.py: cached sig: %i " % signum) sys.stdout.flush() if shutDown: print("ignoring signal: %i " % signum) return else: shutDown = True if sub is not None and not sub.poll(): print("script.py: sent signal to doStuff.sh pid: ", sub.pid) sys.stdout.flush() os.kill(sub.pid, signal.SIGTERM) os.waitpid(sub.pid,0) sys.exit(128+signum) signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGUSR2, handler) signal.signal(signal.SIGTERM, handler) for i in range(0,5): print("launching %i" % i) sub = subprocess.Popen(["./doStuff.sh"], stderr = subprocess.STDOUT) sub.wait() print("finished script.py") 

avec le stub doStuff

 function signalHandler() { trap '' SIGINT trap '' SIGTERM sleep 10s # kill ourself to signal calling process we exited on SIGINT trap - SIGINT kill -s SIGINT $$ } trap_with_arg "signalHandler" SIGINT SIGTERM trap '' SIGUSR2 echo "doStuff.sh : pid: $$" for i in {1..100}; do sleep 1s done 

Maintenant, lancez python script.py et envoyez deux fois un SIGINT ( kill -INT -thePID ou appuyez deux fois sur Ctrl+C avec une seconde entre:

 launching 0 doStuff.sh : pid: 7720 ^Cscript.py: cached sig: 2 /** < first time CTRL + C script.py: sent signal to doStuff.sh pid: 7720 /** < doStuff is waiting now ^Cscript.py: cached sig: 2 /** < second time CTRL + C ignoring signal: 2 launching 1 /** < why continuing loop??? so python ... 

Je me demande vraiment pourquoi la boucle continue car il y a un sys.exit à la fin du gestionnaire et je m’attendais à ce que le premier appel à shutDownHandler appelle finalement sys.exit . Quelqu’un voit-il ce qui est défectueux dans ce programme? Avoir du mal à faire fonctionner ça.