[Twisted-Python] Twisted client memory leak

Diego Woitasen diego at woitasen.com.ar
Tue Jan 22 06:06:44 MST 2013


I have an Twisted client app that makes hundreds of connections per
minute. I discover that I have a memory leak un my app and I'm almost
sure that is related to the ClientFactory() derived class that is
never deleted.

I reproduce the problem with a modification of Echo client example
from Twisted documentation:

    from twisted.internet.protocol import Protocol, ClientFactory
    from twisted.internet import reactor
    from twisted.internet.task import LoopingCall

    from sys import stdout

    class Echo(Protocol):
        def connectionMade(self):
            print 'MADE'
            self.transport.write('XXXX')

        def dataReceived(self, data):
            print 'RECV', data
            self.transport.loseConnection()

        def __del__(self):
            print 'DEL PROTOCOL'

    class EchoClientFactory(ClientFactory):
        def startedConnecting(self, connector):
            print 'Started to connect.'

        def buildProtocol(self, addr):
            print 'Connected.'
            return Echo()

        def clientConnectionLost(self, connector, reason):
            print 'Lost connection.  Reason:', reason

        def clientConnectionFailed(self, connector, reason):
            print 'Connection failed. Reason:', reason

        def __del__(self):
            print 'DEL FACTORY'

    def connector():
        print 'CONNECTOR'
        factory = EchoClientFactory()
        reactor.connectTCP('localhost', 7, factory)

    #reactor.callLater(2, connector)
    register_loop = LoopingCall(connector)
    register_loop.start(1)

    reactor.run()

With this code I discover that the instances of EchoClientFactory()
are only deleted when the program shutdowns. They are not deleted when
the connections finish. I haven't found in the documentation if I need
to do some to get factory instances deleted.

--
Diego Woitasen




More information about the Twisted-Python mailing list