Can someone explain what is going on here at a low level? Why does launching a shell (bash, ksh) as a child subprocess from Python3, Rake, Ruby or Make, cause the terminal to behave like the parent process does not receive the SIGINT generated by a ctrl-c? The parent certainly does interrupt when the subprocess is not a shell, so what is really going on? I'm seeing that the child and parent are both part of the same process group throughout execution.
How does the child make the parent immune to SIGINT? If this is accomplished with channel rerouting or some other fancy trickery, please explain how this is done with an example. I'd like to fix what I'm seeing as inconsistent behavior between EDA tools that have CLI's; some act as Bash does in a subproc, others will be left as orphans.
Test code:
This code will first launch bash as a subprocess that can't be interrupted with ctrl-c, despite being wrapped by Python3; you will have to exit to return back to the parent. The code will then launch child.py which has an interrupt handler; when you ctrl-c inside the child, you will see that both parent.py raise and child.py interrupt at the same time, leaving child.py in orphaned state while its SIGINT handler spews to stdout; the exit status is that of parent; the child's status is lost.
parent.py:
#!/usr/bin/env python3
import os, sys
import subprocess
import signal
import time
PID = os.getpid()
PGID = os.getpgid(PID)
PROC_LABEL = f'PARENT: pid={PID}, pgid={PGID}'
print(f"{PROC_LABEL}: spawning bash...")
proc = subprocess.Popen(['bash'])
ret = proc.wait()
print(f"{PROC_LABEL}: child exit status:", proc.returncode)
print(f"{PROC_LABEL}: spawning ./child.py...")
proc = subprocess.Popen(['./child.py'])
proc.wait()
print(f"{PROC_LABEL}: child exit status:", proc.returncode)
sys.exit(0)
child.py
#!/usr/bin/env python3
import os, sys
import signal
import time
PID = os.getpid()
PGID = os.getpgid(PID)
PROC_LABEL = f"CHILD : pid={PID}; pgid={PGID}"
def intr_handler(sig, frame):
print(f'\n{PROC_LABEL}: Trapped: {signal.Signals(sig).name}')
for idx in range(3,0,-1):
print(f"{PROC_LABEL}: sleeping for {idx} seconds")
time.sleep(1)
print(f"{PROC_LABEL}: bye")
sys.exit(100)
signal.signal(signal.SIGINT, intr_handler)
ret = input(f"{PROC_LABEL}: type something: ")
print("input:", ret)
sys.exit(0)
execution:
$ ./parent.py
PARENT: pid=3121412, pgid=3121412: spawning bash...
bash> ^C
bash> exit 0
exit
PARENT: pid=3121412, pgid=3121412: child exit status: 0
PARENT: pid=3121412, pgid=3121412: spawning ./child.py...
CHILD : pid=3121728; pgid=3121412: type something: ^C
CHILD : pid=3121728; pgid=3121412: Trapped: SIGINT
CHILD : pid=3121728; pgid=3121412: sleeping for 3 seconds
Traceback (most recent call last):
File "./parent.py", line 18, in <module>
proc.wait()
File "/m/tools/lang/python/pyenv/versions/3.7.6/lib/python3.7/subprocess.py", line 1019, in wait
return self._wait(timeout=timeout)
File "/m/tools/lang/python/pyenv/versions/3.7.6/lib/python3.7/subprocess.py", line 1653, in _wait
(pid, sts) = self._try_wait(0)
File "/m/tools/lang/python/pyenv/versions/3.7.6/lib/python3.7/subprocess.py", line 1611, in _try_wait
(pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
$ CHILD : pid=3121728; pgid=3121412: sleeping for 2 seconds
CHILD : pid=3121728; pgid=3121412: sleeping for 1 seconds
CHILD : pid=3121728; pgid=3121412: bye
echo $?
1