[Twisted-Python] ReconnectingClientFactory.stopTrying
RedHat 9.0 Twisted 1.0.4 I have two servers, A and B, connected over a network which may, or may not be working well. Server A wants to send a bunch of data over to Server B. I'm using ReconnectingClientFactory to get A connected to B even in the presence of failures. Once Server A dumps the data on B, it should disconnect and go back to doing it's (unrelated) work, and close the connection on B. ReconnectingClientFactory has "stopTrying" which I call to prevent future connections from being made. Then I close the current connection. Unfortunately that tells the ReconnectingClientFactory that it should reconnect, even though it should have stopped trying. Shortly thereafter I get a stack trace. Reversing the order... loseConnection, stopTrying, doesn't work either. Test code below will give a stack trace after a few seconds of running. My (present) workaround is to close the connection, and tell the ReconnectingClientFactory to stopTrying with: reactor.callLater(0.1, self.factory.stopTrying) It works, but the 0.1 is a kludge to allow connection clean-up to finish, but timed to go off before the Connector starts a new attempt. Yuck. -Eric # from twisted.internet import reactor from twisted.internet.protocol import Factory, ReconnectingClientFactory from twisted.protocols.basic import Int16StringReceiver import pickle class In(Int16StringReceiver): def __init__(self): self.msgs = {} def stringReceived(self, msg): n, msg = pickle.loads(msg) self.msgs[n] = msg self.sendString(pickle.dumps(n)) def connectionLost(self, reason): print self.msgs.values() class Out(Int16StringReceiver): def __init__(self): self.msgs = {} def connectionMade(self): for i in range(10): self.msgs[i] = 'X' * i for i in self.msgs.keys(): self.sendString(pickle.dumps( (i, self.msgs[i]))) def stringReceived(self, msg): n = pickle.loads(msg) del self.msgs[n] if not self.msgs: self.transport.loseConnection() self.factory.stopTrying() f = Factory() f.protocol = In c = ReconnectingClientFactory() c.protocol = Out PORT=9000 reactor.connectTCP('localhost', PORT, c) reactor.listenTCP(PORT, f) reactor.run()
On Fri, May 02, 2003 at 07:06:58PM -0400, Eric C. Newton wrote:
RedHat 9.0 Twisted 1.0.4
Thanks for including this info.
[snip]
My (present) workaround is to close the connection, and tell the ReconnectingClientFactory to stopTrying with:
reactor.callLater(0.1, self.factory.stopTrying)
It works, but the 0.1 is a kludge to allow connection clean-up to finish, but timed to go off before the Connector starts a new attempt. Yuck.
Fixed in CVS. I also turned your demonstration into a test case and added it as well, as I don't expect you'll have any issue with the legalities (Grant glyph a non-exclusive copyright to the code - see doc/legal/ for the details). Please confirm this, so I don't have to back out test_factories.py and write a new one :) And thanks for the bug report! Jp -- #!/bin/bash ( LIST=(~/.sigs/*.sig) cat ${LIST[$(($RANDOM % ${#LIST[*]}))]} echo -- $'\n' `uptime | sed -e 's/.*m//'` ) > ~/.signature -- up 1 day, 4:38, 5 users, load average: 0.09, 0.06, 0.01
participants (2)
-
Eric C. Newton
-
Jp Calderone