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

guido@beopen.com guido@beopen.com
Thu, 6 Jul 2000 01:52:32 -0400 (EDT)


> 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()

Yech!  You're right.  The code I wrote to call PyOS_AfterFork()
incorrectly assumed only the main thread would fork.

Fred: your fix is almost right -- there's a second call to
PyOS_AfterFork() in posixmodule.c (in forkpty()) that needs similar
treatment.

--Guido van Rossum (home page: http://dinsdale.python.org/~guido/)