[Twisted-Python] [Twisted] #5272: In the core doc, do not call reactor.stop directly in processEnded

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon Sep 26 09:27:39 EDT 2011

On 12:13 pm, a.libran at gmail.com wrote:
>>  The process ''starting'' is different from `ProcessProtocol` methods 
>>  called.  `ProcessProtocol.processEnded` is ''not'' called until the
>>  reactor is running.
>On my own VMs, the processEnded gets called
>[root at master1 play]# cat fun.py
>from twisted.internet import reactor
>from twisted.internet import reactor, protocol
>class OneRun(protocol.ProcessProtocol):
>    def processEnded(self, reason):
>        print "Process ended\n"
>reactor.spawnProcess(OneRun(), executable="echo", args=["echo",
>"hello"], childFDs={0:0, 1:1, 2:2})
>[root at master1 play]# python fun.py
>Process ended
>[root at master1 play]#
>But once again, on a different VM, I only see "hello" and processEnded
>isn't called.
>Both VMs run Python 2.7.1 and Twisted 11.0.0
>What is going on?

Thanks for this follow-up.  I noticed that in this snippet, there's no 
`reactor.run()´ call at all.  This is a better example, since it shows 
`processEnded´ being called even though there's no chance of the reactor 
ever being running.  Investigating this, I see that there is an early- 
exit case in process support (in `twisted/internet/process.py´, line 65) 
which may call `processEnded´ even when the reactor is not running. 
Inserting a sleep(1) before the `os.waitpid´ call in that function makes 
it much more likely to trigger this case, since it gives the child 
plenty of time to exit.

This strikes me as a bug.  Can you file a new ticket for this?

Thanks again,
>David Kao
>On Mon, Sep 26, 2011 at 8:03 PM, Twisted <trac at twistedmatrix.com> 
>>#5272: In the core doc, do not call reactor.stop directly in 
>>    Reporter:  dkdog   |           Owner:
>>        Type:  defect  |          Status:  closed
>>    Priority:  normal  |       Milestone:
>>   Component:  core    |      Resolution:  worksforme
>>    Keywords:          |          Branch:
>>Branch_author:          |   Launchpad_bug:
>>Changes (by exarkun):
>> * status:  reopened => closed
>> * resolution:  => worksforme
>> > I just found out that reactor.spawnProcess could spawn the 
>> right away **before** reactor.run is executed.
>> This doesn't seem related to this ticket.
>> > I get the impression that most stuff don't get run until the 
>> event loop is up and running with reactor.run()
>> That's true.  Most stuff.
>> > In this case, who's answering all the ProcessProtocol's methods 
>> processEnded then, without an event loop?
>> The process ''starting'' is different from `ProcessProtocol` methods 
>> called.  `ProcessProtocol.processEnded` is ''not'' called until the
>> reactor is running.
>> Further discussion should be moved to the mailing list.  Thanks.
>>Ticket URL: <http://twistedmatrix.com/trac/ticket/5272#comment:8>
>>Twisted <http://twistedmatrix.com/trac/>
>>Engine of your Internet
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com

More information about the Twisted-Python mailing list