25

I'm using python to manage some simulations. I build the parameters and run the program using:

pipe = open('/dev/null', 'w')
pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe)

My code handles different signal. Ctrl+C will stop the simulation, ask if I want to save, and exit gracefully. I have other signal handlers (to force data output for example).

What I want is to send a signal (SIGINT, Ctrl+C) to my python script which will ask the user which signal he wants to send to the program.

The only thing preventing the code to work is that it seems that whatever I do, Ctrl+C will be "forwarded" to the subprocess: the code will catch it to and exit:

try:
  <wait for available slots>
except KeyboardInterrupt:
  print "KeyboardInterrupt catched! All simulations are paused. Please choose the signal to send:"
  print "  0: SIGCONT (Continue simulation)"
  print "  1: SIGINT  (Exit and save)"
  [...]
  answer = raw_input()
  pid.send_signal(signal.SIGCONT)
  if   (answer == "0"):
    print "    --> Continuing simulation..."
  elif (answer == "1"):
    print "    --> Exit and save."
    pid.send_signal(signal.SIGINT)
    [...]

So whatever I do, the program is receiving the SIGINT that I only want my python script to see. How can I do that???

I also tried:

signal.signal(signal.SIGINT, signal.SIG_IGN)
pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe)
signal.signal(signal.SIGINT, signal.SIG_DFL)

to run the program but this gives the same result: the program catches the SIGINT.

Thanx!

big_gie
  • 2,829
  • 3
  • 31
  • 45
  • Lots of good answers here: https://stackoverflow.com/questions/13593223/making-sure-a-python-script-with-subprocesses-dies-on-sigint – totaam Feb 15 '20 at 09:55

5 Answers5

20

Combining some of other answers that will do the trick - no signal sent to main app will be forwarded to the subprocess.

import os
from subprocess import Popen

def preexec(): # Don't forward signals.
    os.setpgrp()

Popen('whatever', preexec_fn = preexec)
Marek Sapota
  • 20,103
  • 3
  • 34
  • 47
  • 14
    Please consider using [subprocess32][1] instead of subprocess if you're using Python 2.x and **do not use `preexec_fn`** anymore. It isn't safe. Use the new Popen **start_new_session=True** parameter instead. [1]: http://code.google.com/p/python-subprocess32/ – gps Sep 18 '12 at 06:18
  • 5
    Beside safety argument, the above code works because normally when you press Ctrl-C the SIGINT is sent to the process group. By default all subprocesses are in the same process group with parent process. By calling `setpgrp()` you put your child process to a new process group so it won't get the signals from parent. – Cenk Alti Nov 15 '14 at 12:00
  • 5
    In terms of style, it's a bit more Pythonic to say: `Popen('whatever', preexec_fn=os.setpgrp)`. There's no need to define preexec(). – Scott May 29 '15 at 21:33
  • 1
    Why don't just `signal.signal(signal.SIGINT, signal.SIG_IGN)` in preexec? – omikron Feb 29 '16 at 14:11
8

This can indeed be done using ctypes. I wouldn't really recommend this solution, but I was interested enough to cook something up, so I thought I would share it.

parent.py

#!/usr/bin/python

from ctypes import *
import signal
import subprocess
import sys
import time

# Get the size of the array used to
# represent the signal mask
SIGSET_NWORDS = 1024 / (8 * sizeof(c_ulong))

# Define the sigset_t structure
class SIGSET(Structure):
    _fields_ = [
        ('val', c_ulong * SIGSET_NWORDS)
    ]

# Create a new sigset_t to mask out SIGINT
sigs = (c_ulong * SIGSET_NWORDS)()
sigs[0] = 2 ** (signal.SIGINT - 1)
mask = SIGSET(sigs)

libc = CDLL('libc.so.6')

def handle(sig, _):
    if sig == signal.SIGINT:
        print("SIGINT from parent!")

def disable_sig():
    '''Mask the SIGINT in the child process'''
    SIG_BLOCK = 0
    libc.sigprocmask(SIG_BLOCK, pointer(mask), 0)

# Set up the parent's signal handler
signal.signal(signal.SIGINT, handle)

# Call the child process
pid = subprocess.Popen("./child.py", stdout=sys.stdout, stderr=sys.stdin, preexec_fn=disable_sig)

while (1):
    time.sleep(1)

child.py

#!/usr/bin/python
import time
import signal

def handle(sig, _):
    if sig == signal.SIGINT:
        print("SIGINT from child!")

signal.signal(signal.SIGINT, handle)
while (1):
    time.sleep(1)

Note that this makes a bunch of assumptions about various libc structures and as such, is probably quite fragile. When running, you won't see the message "SIGINT from child!" printed. However, if you comment out the call to sigprocmask, then you will. Seems to do the job :)

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • Thanx for your suggestion. I think it's too complicated for the goal I have though. Basically, I just want to pause the parent script, ask the user something and send a signal to all child processes. Maybe another keyboard input could pause the parent script, like ctrl+x? – big_gie Sep 26 '10 at 16:02
  • Yeah, as I said, I wouldn't really recommend the solution. You can use any key combination you want to pause the parent if you listen for keyboard events in a separate thread. – Michael Mior Sep 26 '10 at 22:10
4

POSIX says that a program run with execvp (which is what subprocess.Popen uses) should inherit the signal mask of the calling process.

I could be wrong, but I don't think calling signal modifies the mask. You want sigprocmask, which python does not directly expose.

It would be a hack, but you could try setting it via a direct call to libc via ctypes. I'd definitely be interested in seeing a better answer on this strategy.

The alternative strategy would be to poll stdin for user input as part of your main loop. ("Press Q to quit/pause" -- something like that.) This sidesteps the issue of handling signals.

bstpierre
  • 30,042
  • 15
  • 70
  • 103
2

I resolved this problem by creating a helper app that I call instead of creating the child directly. This helper changes its parent group and then spawn the real child process.

import os
import sys

from time import sleep
from subprocess import Popen

POLL_INTERVAL=2

# dettach from parent group (no more inherited signals!)
os.setpgrp()

app = Popen(sys.argv[1:])
while app.poll() is None:
    sleep(POLL_INTERVAL)

exit(app.returncode)

I call this helper in the parent, passing the real child and its parameters as arguments:

Popen(["helper", "child", "arg1", ...])

I have to do this because my child app is not under my control, if it were I could have added the setpgrp there and bypassed the helper altogether.

Marcelo Santos
  • 1,851
  • 1
  • 12
  • 14
1

The function:

os.setpgrp()

Works well only if Popen is being called right afterwards. If you are trying to prevent signals from being propagated to the subprocesses of an arbitrary package, then the package may override this before creating subprocesses causing signals to be propagated anyways. This is the case when, for example, trying to prevent signal propagation into web browser processes spawned from the package Selenium.

This function also removes the ability to easily communicate between the separated processes without something like sockets.

For my purposes, this seemed like overkill. Instead of worrying about signals propagating, I used the custom signal SIGUSR1. Many Python packages ignore SIGUSR1, so even if it is sent to all subprocesses, it will usually be ignored

It can be sent to a process in bash on Ubuntu using

kill -10 <pid>

It can be recognized in your code via

signal.signal(signal.SIGUSR1, callback_function)

The available signal numbers on Ubuntu can be found at /usr/include/asm/signal.h.

jbass
  • 116
  • 1
  • 8