This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add an explanation what happens with subprocess parent and child processes when signals are sent
Type: Stage:
Components: Documentation Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Ian Macartney, docs@python, krichter, martin.panter
Priority: normal Keywords:

Created on 2015-12-03 03:29 by krichter, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
bug.py Ian Macartney, 2015-12-09 08:54 To see the bug, run it and interrupt it partway through
Messages (5)
msg255802 - (view) Author: Karl Richter (krichter) Date: 2015-12-03 03:29
The [documentation of subprocess](https://docs.python.org/3.6/library/subprocess.html) doesn't contain a substantial statement how signals are handled which are send to the python interpreter. After reading the referenced docs it should be clear

  * whether a signal is passed to both the parent and the child (If yes in which order? What happens if the child process spawns a process which isn't controlled by python?)
  * whether signal handlers are inherited (judging from the `restore_signals` parameter some are overwritten -> what's the purpose of this?). Are changes of a signal handler in the parent reflected in the child?
msg255831 - (view) Author: Karl Richter (krichter) Date: 2015-12-03 18:04
Please also explain how to deal with process replacement in child processes (assuming that http://stackoverflow.com/questions/34059576/how-to-register-a-signal-handler-for-a-subprocess/34065587#34065587 is correct).
msg255845 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-12-03 23:07
These are mainly questions of the OS, not really of Python or the subprocess module. I’m not sure the Python documentation is the right place for this information, unless it gives a misleading impression.

At least in Unix, signals may be sent to the child process only, or to a process group including the parent and the child. Processes can run concurrently, in which case the order is meaningless.

Signal handlers are inherited when os.fork() or similar copies a process, but cannot be inherited when a process is replaced or spawned, because the child is not a copy of the parent.

I think the “restore_signals” flag is an unnecessary and could be deprecated. The purpose of Python ignoring some signals is so that things like writing to a disconnected pipe or socket does not trigger the default signal handling.
msg256147 - (view) Author: Ian Macartney (Ian Macartney) Date: 2015-12-09 08:54
I don't have much experience with what should and shouldn't be in the python docs, however I was recently bitten by a subtlety in signal/subprocess that might be worth documenting.

Anything written to stdout in a signal handler could end up in the stdout of a process returned by Popen, if stdout gets flushed in the signal handler during a critical part of the Popen call. Furthermore, any output buffered before calling Popen could also be flushed by the signal handler, showing up in the child process's stdout. The same goes for stderr.

On 2.7.7 at least, the window where this can happen is between lines 1268-1290 in subprocess.py, where the child process has duplicated stdout, but hasn't yet called execvp.

This is a result of signal inheritance that caught me off guard, and wasn't clear to me by reading the documentation.
msg256153 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-12-09 10:32
I think Karl’s original report was mainly about normal signal handling in the long term in the child, i.e. behaviour after exec() succeeds.

Ian, your problem sounds more like a bug or unfortunate quirk of the window between fork() and exec(). Maybe it is possible to solve it by blocking or resetting signal handlers, or using posix_spawn() (Issue 20104).
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 69973
2015-12-09 10:32:22martin.pantersetmessages: + msg256153
2015-12-09 08:54:06Ian Macartneysetfiles: + bug.py
nosy: + Ian Macartney
messages: + msg256147

2015-12-03 23:07:21martin.pantersetnosy: + martin.panter
messages: + msg255845
2015-12-03 18:04:17krichtersetmessages: + msg255831
2015-12-03 03:29:39krichtercreate