From 82e1d1c1661a5c5b969dae9f32c72e0bda9e2334 Mon Sep 17 00:00:00 2001 From: Roger Date: Thu, 9 Dec 2010 07:02:10 +0800 Subject: [PATCH] Fix memory leak of MuxWrapper object. (Note by apenwarr: I used Roger's original patch as the basis for this one, but implemented it a different way. All errors are thus my fault, but Roger gets the credit for actually tracking down the circular reference that caused the memory leak.) --- ssnet.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ssnet.py b/ssnet.py index 05ff447..e8dde65 100644 --- a/ssnet.py +++ b/ssnet.py @@ -264,6 +264,8 @@ class Proxy(Handler): if (self.wrap1.shut_read and self.wrap2.shut_read and not self.wrap1.buf and not self.wrap2.buf): self.ok = False + self.wrap1.nowrite() + self.wrap2.nowrite() class Mux(Handler): @@ -425,11 +427,19 @@ class MuxWrapper(SockWrapper): def noread(self): if not self.shut_read: self.shut_read = True + self.maybe_close() def nowrite(self): if not self.shut_write: self.shut_write = True self.mux.send(self.channel, CMD_EOF, '') + self.maybe_close() + + def maybe_close(self): + if self.shut_read and self.shut_write: + # remove the mux's reference to us. The python garbage collector + # will then be able to reap our object. + self.mux.channels[self.channel] = None def too_full(self): return self.mux.too_full