[Twisted-Python] The Trial of the DirtyReactorError

Matthew Glubb matt at zgroupplc.com
Fri Apr 13 05:15:28 EDT 2007

Hash: SHA1

Hi All,

Following on from my DirtyReactorError errors whilst running unit  
tests under trial I opted, as glyph suggested, to call listenTCP  
inside the ServerFactory in order to keep track of the port and shut  
it down cleanly.

On 12 Apr 2007, at 19:56, glyph at divmod.com wrote:

> If they are "always shut down", what event currently shuts them  
> down? Have your test trigger that event.  If they're one-shot, then  
> perhaps the method that calls listenTCP should be on the factory  
> itself, making it even easier to keep track of the Port instance it  
> is associated with.

This causes me a problem. When a client connection is lost, I want to  
shutdown the server. The only way that I can see to do this is by the  
connectionLost event calling a method in my ServerFactory that shuts  
down the listening port:

class Echo(basic.LineOnlyReceiver):

     def connectionLost(self, reason):

class EchoServerFactory(protocol.ServerFactory):
     protocol = Echo
     port = None

     def __init__(self, port):
         self.port = reactor.listenTCP(port, self)

     def shutdown(self, reason):

*However*, this results in the following error:

twisted.trial.util.PendingTimedCallsError: pendingTimedCalls still  
pending (consider setting twisted.internet.base.DelayedCall.debug =  
True): <DelayedCall 24706704 [-0.00161504745483s] called=0  
cancelled=0 Port.connectionLost(<twisted.python.failure.Failure  
<class 'twisted.internet.error.ConnectionDone'>>)

Obviously, tcp.Port.stopListening() results in the  
Echo.connectionLost event being triggered, which in turn calls  
EchoServerFactory.shutdown(), which triggers an additional  
Echo.connectionLost event. It seems that tcp.Port.connected is not  
being updated quickly enough to prevent the additional pendingTimedCalls

My question therefore is how is it possible to cleanly shut down a  
server when a client connection is lost?

Apologies if I am being a complete idiot about this. I am still a  
relatively twisted newbie ;)



m a t t h e w   g l u b b

Z Group PLC

Tel: +44 (0) 8700 111 173
Fax: +44 (0) 8707 051 393
Txt: +44 (0) 7800 140 877
Web: <http://www.zgroupplc.com/>

This  email  and  any  files  transmitted  with it are  confidential and
intended solely for the use of the individual or entity to whom they are
addressed.  The opinions  expressed in this mail are those of the author
and do not necessarily  represent the views of the company.  If you have
received this email in error please notify <service at zgroupplc.com>

Version: GnuPG v1.4.1 (Darwin)


More information about the Twisted-Python mailing list