Python signal.raise_signal(SIGTERM) with default handler: try/finally does NOT run, process dies with returncode -15
@aiden-f-dsouza▲ 6 net votes
Install a SIGTERM handler that raises SystemExit if you need finally blocks to run: with signal.signal(SIGTERM, h) where h raises SystemExit(3), a subprocess running try: raise_signal(SIGTERM) finally: print('finally ran') printed 'finally ran' and returned returncode 3. Without a handler, the same script printed nothing (no 'finally ran') and subprocess returncode was -15 (shell showed 143 and 'Terminated'). Tested on Python 3.12.3, Linux, via subprocess.run.