Thanks, that explains everything. The code I was using to test was copied from Twisted's daemonizing function: <a href="http://github.com/powdahound/twisted/blob/master/twisted/scripts/_twistd_unix.py#L155-169">http://github.com/powdahound/twisted/blob/master/twisted/scripts/_twistd_unix.py#L155-169</a><div>
<br></div><div>It should probably be updated before more users upgrade to the new kernel and run into this issue. Ticket #823 (<a href="http://twistedmatrix.com/trac/ticket/823">http://twistedmatrix.com/trac/ticket/823</a>) seems very related to this.<div>
<br><div class="gmail_quote">On Thu, Sep 30, 2010 at 8:52 AM, Žiga Seilnacht <span dir="ltr"><<a href="mailto:ziga.seilnacht@gmail.com">ziga.seilnacht@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br>
<div class="im"><br>
Garret Heaton wrote:<br>
> I've simplified this issue down and am able to reproduce it without<br>
> Twisted: <a href="http://gist.github.com/603154" target="_blank">http://gist.github.com/603154</a><br>
><br>
> Still not sure what the cause is, so if anyone has any ideas I'd love to<br>
> hear them. Thanks!<br>
><br>
<br>
</div>The behavior that you are seeing seems to be related to the change in<br>
the 2.6.32 kernel, where they changed the child-runs-first scheduler<br>
parameter to false. Setting that parameter to 1 with:<br>
<br>
$ sudo sysctl -w kernel.sched_child_runs_first=1<br>
<br>
and rebooting the computer restores the behavior that you saw on the<br>
old kernel for me. See <a href="http://lwn.net/Articles/352863/" target="_blank">http://lwn.net/Articles/352863/</a> for more details.<br>
<br>
Parent (which is the controlling process when ran with -t) exiting before<br>
the child starts causes the child to receive SIGHUP signal immediately as<br>
it starts running, before it has time to disassociate itself from the<br>
parent's process group. It seems to me that this might be an actual bug<br>
in twistd, it should block the SIGHUP signal across the fork() calls.<br>
<br>
Your test program has an additional assumption that the child will run<br>
before the parent; the child in your program tries to write to stdout,<br>
i.e. the controlling terminal, which gets closed once the parent exits.<br>
<br>
The modified test program below works for me regardless of the setting<br>
of the kernel.sched_child_runs_first parameter.<br>
<br>
Hope this helps,<br>
Ziga<br>
<br>
<br>
<br>
import os<br>
import signal<br>
<br>
f = open("test_fork.out", "w")<br>
<br>
def daemonize():<br>
# See <a href="http://www.faqs.org/faqs/unix-faq/programmer/faq/" target="_blank">http://www.faqs.org/faqs/unix-faq/programmer/faq/</a> - Section 1.7<br>
print >> f, '--- %s: daemonizing' % os.getpid()<br>
<br>
signal.signal(signal.SIGHUP, signal.SIG_IGN)<br>
if os.fork(): # launch child and...<br>
print >> f, '--- %s: kill parent 1' % os.getpid()<br>
os._exit(0) # kill off parent<br>
print >> f, '--- %s: old sid: %r' % (os.getpid(), os.getsid(os.getpid()))<br>
os.setsid()<br>
print >> f, '--- %s: new sid: %r' % (os.getpid(), os.getsid(os.getpid()))<br>
if os.fork(): # launch child and...<br>
print >> f, '--- %s: kill parent 2' % os.getpid()<br>
os._exit(0) # kill off parent again.<br>
<br>
signal.signal(signal.SIGHUP, signal.SIG_DFL)<br>
print >> f, '--- %s: daemonizing done' % os.getpid()<br>
<br>
if __name__ == "__main__":<br>
print >> f, 'starting as %d' % os.getpid()<br>
daemonize()<br>
print >> f, 'stopping as %s' % os.getpid()<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br></div></div>