From 335dc2927bf874aa3ee3d4154257040b3b59db1a Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 8 Oct 2011 17:31:30 -0400 Subject: Break reference cycle to fix potential memory leaks for callback handlers. --- sleekxmpp/xmlstream/handler/base.py | 7 ++++++- sleekxmpp/xmlstream/handler/waiter.py | 4 ++-- sleekxmpp/xmlstream/xmlstream.py | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/base.py b/sleekxmpp/xmlstream/handler/base.py index 6ec9b6a3..7f05c757 100644 --- a/sleekxmpp/xmlstream/handler/base.py +++ b/sleekxmpp/xmlstream/handler/base.py @@ -6,6 +6,8 @@ See the file LICENSE for copying permission. """ +import weakref + class BaseHandler(object): @@ -43,7 +45,10 @@ class BaseHandler(object): stream -- The XMLStream instance the handler should monitor. """ self.name = name - self.stream = stream + if stream is not None: + self.stream = weakref.ref(stream) + else: + self.stream = None self._destroy = False self._payload = None self._matcher = matcher diff --git a/sleekxmpp/xmlstream/handler/waiter.py b/sleekxmpp/xmlstream/handler/waiter.py index 341c01fe..25dc161c 100644 --- a/sleekxmpp/xmlstream/handler/waiter.py +++ b/sleekxmpp/xmlstream/handler/waiter.py @@ -85,14 +85,14 @@ class Waiter(BaseHandler): value sleekxmpp.xmlstream.RESPONSE_TIMEOUT. """ if timeout is None: - timeout = self.stream.response_timeout + timeout = self.stream().response_timeout try: stanza = self._payload.get(True, timeout) except queue.Empty: stanza = False log.warning("Timed out waiting for %s" % self.name) - self.stream.removeHandler(self.name) + self.stream().remove_handler(self.name) return stanza def check_delete(self): diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 0a66a974..8abbb3a1 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -19,6 +19,7 @@ import threading import time import types import random +import weakref try: import queue except ImportError: @@ -719,7 +720,7 @@ class XMLStream(object): """ if handler.stream is None: self.__handlers.append(handler) - handler.stream = self + handler.stream = weakref.ref(self) def remove_handler(self, name): """ -- cgit v1.2.3