简体   繁体   English

连接尝试失败时出现扭曲的内存泄漏

[英]Twisted memory leak when connect attempt failed

i was being tortured twisted memory leaks. 我遭受酷刑扭曲的内存泄漏。

Here is my code: 这是我的代码:

# Python:  Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
# Twisted: Twisted-12.2.0.win32-py2.6
# OS:      Windows 7 (64bit)
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import reactor


class Echo(Protocol):
    def __del__(self):
        print 'Echo.__del__'

    def connectionMade(self):
        print 'Echo.connectionMade'
        self.transport.write('Hello world!')


class EchoClientFactory(ClientFactory):
    def __del__(self):
        print 'EchoClientFactory.__del__'

    def startedConnecting(self, connector):
        print 'Started connecting ...'

    def buildProtocol(self, addr):
        print 'connected. %r' % addr
        return Echo()

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


def connect(ip, port):
    factory = EchoClientFactory()
    reactor.connectTCP(ip, port, factory)


##Start
import gc
gc.enable()
gc.set_debug(gc.DEBUG_LEAK)

# Trying to connect to a port that does not exist
connect('127.0.0.1', 7777)

reactor.callLater(5, reactor.stop)
reactor.run()

# Show garbages
print "gc.collect()"
gc.collect()
print 'gc.garbage:', len(gc.garbage)

for i, item in enumerate(gc.garbage):
    print '%d) %r' % (i + 1, item)

after run, gc shows memory leak is happen: 运行后,gc显示发生了内存泄漏:

after run, gc shows memory leak is happen: 运行后,gc显示发生了内存泄漏:

after run, gc shows memory leak is happen: 运行后,gc显示发生了内存泄漏:

Started connecting ...
Connection failed.
EchoClientFactory.__del__
gc.collect()
gc: collectable <Client 02CEA7B0>
gc: collectable <dict 02CEFC00>
gc: collectable <tuple 02B13FD0>
gc: collectable <list 02CD2558>
gc: collectable <instancemethod 02818E40>
gc: collectable <instancemethod 02CF04B8>
gc: collectable <tuple 02B205F8>
gc.garbage: 7
1) <<class 'twisted.internet.tcp.Client'> to ('127.0.0.1', 7777) at 2cea7b0>
2) {'_tempDataBuffer': [], 'protocol': None, '_tempDataLen': 0, 'realAddress': ('127.0.0.1', 7777), 'doRead': <bound method Client.doConnect of <<class 'twisted.internet.tcp.Client'> to ('127.0.0.1', 7777) at 2cea7b0>>, 'doWrite': <bound method Client.doConnect of <<class 'twisted.internet.tcp.Client'> to ('127.0.0.1', 7777) at 2cea7b0>>, 'reactor': <twisted.internet.selectreactor.SelectReactor object at 0x02699D50>, 'addr': ('127.0.0.1', 7777)}
3) ('127.0.0.1', 7777)
4) []
5) <bound method Client.doConnect of <<class 'twisted.internet.tcp.Client'> to ('127.0.0.1', 7777) at 2cea7b0>>
6) <bound method Client.doConnect of <<class 'twisted.internet.tcp.Client'> to ('127.0.0.1', 7777) at 2cea7b0>>
7) ('127.0.0.1', 7777)

Only happened when the connect failed. 仅在连接失败时发生。

Any ideas on why? 有什么想法吗?

Adding __del__ to a program most likely adds object leaks to it. 在程序中添加__del__很可能会增加对象泄漏。 The presence of __del__ on an object in a cycle prevents the entire cycle from being collected. 循环中某个对象上存在__del__阻止收集整个循环。

Try debugging your actual program without using __del__ anywhere. 尝试调试实际程序,而不在任何地方使用__del__ Or if your actual program uses __del__ , try getting rid of it. 或者,如果您的实际程序使用__del__ ,请尝试摆脱它。

Maybe I found a solution. 也许我找到了解决方案。

In the twisted source code, open this file: Python26\\site-packages\\twisted\\internet\\base.py 在扭曲的源代码中,打开以下文件: Python26\\site-packages\\twisted\\internet\\base.py

class BaseConnector(styles.Ephemeral):
    .
    .
    .

    def connectionFailed(self, reason):
        self.cancelTimeout()
        if self.transport.doWrite == self.transport.doConnect:    # 1
            del self.transport.doRead                             # 2
            del self.transport.doWrite                            # 3
        self.transport = None
        self.state = "disconnected"
        self.factory.clientConnectionFailed(self, reason)
        if self.state == "disconnected":
            # factory hasn't called our connect() method
            self.factory.doStop()
            self.factoryStarted = 0
    .
    .
    .

Lines marked with 1,2,3 are my newly added. 标有1,2,3的行是我新添加的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM