[Python-bugs-list] PyOS_AfterFork (PR#392)

jon@dstc.edu.au jon@dstc.edu.au
Thu, 6 Jul 2000 00:32:47 -0400 (EDT)


Full_Name: Jonathan Giddy
Version: 1.5.2 - 2.0b1
OS: Solaris and Linux
Submission from: duck.its.monash.edu.au (130.194.11.183)


In posixmodule.c:posix_fork, the function PyOS_AfterFork is called for
both the parent and the child, despite the docs stating that it should
be called in the new (child) process.

This causes problems in the parent since the forking thread becomes the 
main thread according to the signal module.

e.g. the following program fails with the frustrating message:
"ValueError: signal only works in main thread"

import os, signal, threading

def foo():
    pid = os.fork()
    if pid:
        os.wait(pid, 0)
    else:
        os._exit(0)

def handler(signo, frame):
    pass

def main():
    t = threading.Thread(target=foo)
    t.start()
    t.join()  # optional
    signal.signal(signal.SIGTERM, handler)

main()