Skip to content

Commit

Permalink
Fix some memory leaks.
Browse files Browse the repository at this point in the history
We now explicity del references to objects as connections are dropped.
Also, there was a small bug in the example where the loopingCall had
to be `del`ed for the Handler object to be garbage collected.

Closes wulczer#3
  • Loading branch information
rlotun committed Dec 23, 2010
1 parent bdab65b commit ee31d15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
12 changes: 10 additions & 2 deletions simple_server.py
Expand Up @@ -24,6 +24,9 @@ def __init__(self, transport):
WebSocketHandler.__init__(self, transport)
self.periodic_call = task.LoopingCall(self.send_time)

def __del__(self):
print 'Deleting handler'

def send_time(self):
# send current time as an ISO8601 string
data = datetime.utcnow().isoformat().encode('utf8')
Expand All @@ -36,9 +39,15 @@ def frameReceived(self, frame):

def connectionMade(self):
print 'Connected to client.'
# here would be a good place to register this specific handler
# in a dictionary mapping some client identifier (like IPs) against
# self (this handler object)

def connectionLost(self, reason):
print 'Lost connection.'
self.periodic_call.stop()
del self.periodic_call
# here is a good place to deregister this handler object


class FlashSocketPolicy(Protocol):
Expand All @@ -53,6 +62,7 @@ def connectionMade(self):
self.transport.loseConnection()



if __name__ == "__main__":
from twisted.internet import reactor

Expand All @@ -62,11 +72,9 @@ def connectionMade(self):
site = WebSocketSite(root)
site.addHandler('/test', Testhandler)
reactor.listenTCP(8080, site)

# run policy file server
factory = Factory()
factory.protocol = FlashSocketPolicy
reactor.listenTCP(843, factory)

reactor.run()

24 changes: 12 additions & 12 deletions websocket.py
Expand Up @@ -84,9 +84,6 @@ def finish():
handlerFactory = self.site.handlers.get(self.uri)
if not handlerFactory:
return finish()
transport = WebSocketTransport(self)
handler = handlerFactory(transport)
transport._attachHandler(handler)

# key1 and key2 exist and are a string of characters
# filter both keys to get a string with all numbers in order
Expand Down Expand Up @@ -121,6 +118,10 @@ def finish():
num1 = num1 / numSpaces1
num2 = num2 / numSpaces2

transport = WebSocketTransport(self)
handler = handlerFactory(transport)
transport._attachHandler(handler)

self.channel.setRawMode()

def finishHandshake(nonce):
Expand Down Expand Up @@ -161,7 +162,7 @@ def finishHandshake(nonce):
self.write("\r\n")

# concatenate num1 (32 bit in), num2 (32 bit int), nonce, and take md5 of result
res = struct.pack('>ii8s', num1, num2, nonce)
res = struct.pack('>II8s', num1, num2, nonce)
server_response = md5(res).digest()
self.write(server_response)

Expand Down Expand Up @@ -279,7 +280,6 @@ def __init__(self, resource, logPath=None, timeout=60*60*12,
self.handlers = {}
self.supportedProtocols = supportedProtocols or []


def addHandler(self, name, handlerFactory):
"""
Add or override a handler for the given C{name}.
Expand Down Expand Up @@ -308,14 +308,12 @@ def __init__(self, request):
self._request = request
self._request.notifyFinish().addErrback(self._connectionLost)


def _attachHandler(self, handler):
"""
Attach the given L{WebSocketHandler} to this transport.
"""
self._handler = handler


def _connectionMade(self):
"""
Called when a connection is made.
Expand All @@ -327,7 +325,9 @@ def _connectionLost(self, reason):
Forward connection lost event to the L{WebSocketHandler}.
"""
self._handler.connectionLost(reason)

del self._request.transport
del self._request
del self._handler

def getPeer(self):
"""
Expand Down Expand Up @@ -367,8 +367,9 @@ def loseConnection(self):
Close the connection.
"""
self._request.transport.loseConnection()


del self._request.transport
del self._request
del self._handler

class WebSocketHandler(object):
"""
Expand Down Expand Up @@ -445,7 +446,6 @@ def __init__(self, request, handler):
self._data = []
self._currentFrameLength = 0


def dataReceived(self, data):
"""
Parse data to read WebSocket frames.
Expand Down Expand Up @@ -485,5 +485,5 @@ def dataReceived(self, data):



___all__ = ["WebSocketHandler", "WebSocketSite"]
__all__ = ["WebSocketHandler", "WebSocketSite"]

0 comments on commit ee31d15

Please sign in to comment.