Thanks, that explains everything. The code I was using to test was copied from Twisted&#39;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">&lt;<a href="mailto:ziga.seilnacht@gmail.com">ziga.seilnacht@gmail.com</a>&gt;</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>
&gt; I&#39;ve simplified this issue down and am able to reproduce it without<br>
&gt; Twisted: <a href="http://gist.github.com/603154" target="_blank">http://gist.github.com/603154</a><br>
&gt;<br>
&gt; Still not sure what the cause is, so if anyone has any ideas I&#39;d love to<br>
&gt; hear them. Thanks!<br>
&gt;<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&#39;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(&quot;test_fork.out&quot;, &quot;w&quot;)<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 &gt;&gt; f, &#39;--- %s: daemonizing&#39; % os.getpid()<br>
<br>
     signal.signal(signal.SIGHUP, signal.SIG_IGN)<br>
     if os.fork(): # launch child and...<br>
         print &gt;&gt; f, &#39;--- %s: kill parent 1&#39; % os.getpid()<br>
         os._exit(0) # kill off parent<br>
     print &gt;&gt; f, &#39;--- %s: old sid: %r&#39; % (os.getpid(), os.getsid(os.getpid()))<br>
     os.setsid()<br>
     print &gt;&gt; f, &#39;--- %s: new sid: %r&#39; % (os.getpid(), os.getsid(os.getpid()))<br>
     if os.fork(): # launch child and...<br>
         print &gt;&gt; f, &#39;--- %s: kill parent 2&#39; % os.getpid()<br>
         os._exit(0) # kill off parent again.<br>
<br>
     signal.signal(signal.SIGHUP, signal.SIG_DFL)<br>
     print &gt;&gt; f, &#39;--- %s: daemonizing done&#39; % os.getpid()<br>
<br>
if __name__ == &quot;__main__&quot;:<br>
     print &gt;&gt; f, &#39;starting as %d&#39; % os.getpid()<br>
     daemonize()<br>
     print &gt;&gt; f, &#39;stopping as %s&#39; % 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>