classification
Title: Pending signals are inherited by child processes
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: gdb, gregory.p.smith, pitrou, python-dev
Priority: normal Keywords: needs review, patch

Created on 2010-08-06 14:43 by gdb, last changed 2012-11-11 04:39 by gregory.p.smith. This issue is now closed.

Files
File name Uploaded Description Edit
signals.patch gdb, 2010-08-06 14:43 Clear pending signals from child process after fork()
issue9535-clear_signals_afterfork-gps01.diff gregory.p.smith, 2011-01-04 03:46 review
Messages (7)
msg113104 - (view) Author: Greg Brockman (gdb) Date: 2010-08-06 14:43
Upon os.fork(), pending signals are inherited by the child process.  This can be demonstrated by pressing C-c in the middle of the
following program:

"""
import os, sys, time, threading
def do_fork():
    while True:
        if not os.fork():
            print 'hello from child'
            sys.exit(0)
        time.sleep(0.5)
t = threading.Thread(target=do_fork)
t.start()
t.join()
"""
Right after os.fork(), each child will raise a KeyboardInterrupt exception.

This behavior is different from the semantics of POSIX fork(), where child processes do not inherit their parents' pending signals.

Attached is a first stab at a patch to fix this issue.  Please let me know what you think!
msg125245 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-03 21:26
Shouldn't the clearing of signals be done in PyOS_AfterFork() directly?
Gregory, what do you think about the semantics?
msg125246 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2011-01-03 21:40
Doing this makes sense, i'm taking to closer look to see where it should be done.
msg125290 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2011-01-04 03:46
Yes I'd do it in PyOS_AfterFork.  Simpler patch based on gdb's attached.

This could also use a test but as it involves signals and race conditions, making a deterministic test for this is difficult as it.

If we added testing hooks to the python interpreter internals to allow test code to do things like set the signal tripped flags directly it's be much easier.
msg125305 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-04 11:11
> Yes I'd do it in PyOS_AfterFork.  Simpler patch based on gdb's
> attached.

Private functions shouldn't use the PyXX_CamelCase() convention :)
Otherwise, looks good.
msg175315 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-11 04:34
New changeset ba45976c2801 by Gregory P. Smith in branch '3.2':
Fixes issue #9535: Fix pending signals that have been received but not
http://hg.python.org/cpython/rev/ba45976c2801

New changeset c7d6cd3c27b9 by Gregory P. Smith in branch '3.3':
Fixes issue #9535: Fix pending signals that have been received but not
http://hg.python.org/cpython/rev/c7d6cd3c27b9

New changeset 41280973921a by Gregory P. Smith in branch 'default':
Fixes issue #9535: Fix pending signals that have been received but not
http://hg.python.org/cpython/rev/41280973921a
msg175316 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-11 04:38
New changeset 9b711422c98f by Gregory P. Smith in branch '2.7':
Fixes issue #9535: Fix pending signals that have been received but not yet
http://hg.python.org/cpython/rev/9b711422c98f
History
Date User Action Args
2012-11-11 04:39:09gregory.p.smithsetstatus: open -> closed
resolution: fixed
2012-11-11 04:38:29python-devsetmessages: + msg175316
2012-11-11 04:34:49python-devsetnosy: + python-dev
messages: + msg175315
2011-01-04 11:11:21pitrousetmessages: + msg125305
2011-01-04 03:46:39gregory.p.smithsetfiles: + issue9535-clear_signals_afterfork-gps01.diff

messages: + msg125290
keywords: + needs review
2011-01-03 21:40:04gregory.p.smithsetassignee: gregory.p.smith
messages: + msg125246
2011-01-03 21:26:17pitrousetnosy: + pitrou
messages: + msg125245
2010-09-03 23:45:39pitrousetnosy: + gregory.p.smith
stage: patch review

components: + Interpreter Core, - Extension Modules
versions: + Python 3.1, Python 3.2, - Python 2.6, Python 2.5
2010-08-06 14:43:13gdbcreate