[pyOpenSSL] memory leak?

Martin Sjögren md9ms at mdstud.chalmers.se
Wed Jan 8 19:41:55 CET 2003


ons 2003-01-08 klockan 12.24 skrev Zoltan Felleg:
> this method for me results in constantly growing memory usage of the 
> server (about 10-20MB per minute). and the id of the Connection object 
> is constantly changing (growing), while the Context remains the same (as 
> expected), so i'm assuming the Connection objects returned by the accept 
> method do not get deleted. (i tied to explicitly delete both the 
> connection object and its context, but the leak remained).

Hmm, I guess I'll have to monitor it a bit longer.

> the script i was using follows:

I think I've figured out the problem.

> class client:
>      def __init__(self):
>          self.ctx = OpenSSL.SSL.Context(OpenSSL.SSL.TLSv1_METHOD)
>          self.ctx.set_verify(OpenSSL.SSL.VERIFY_PEER, self.verifycb)

Using self.verifycb introduces a cyclic reference.

>          self.ctx.use_privatekey_file(client_kf)
>          self.ctx.use_certificate_file(client_cf)
>          self.ctx.load_verify_locations(ca_cf)
>          sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>          self.co = OpenSSL.tsafe.Connection(self.ctx, sock)

The use of self.ctx here is also cyclic.

>      def verifycb(self, conn, cert, errnum, depth, ok):
>          return ok
> 
> 
> if __name__ == '__main__':
>      time.sleep(5)
>      for i in range(10000):
>          print i
>          clt = client()
>          clt.co.close()
>          del clt.ctx
>          del clt.co
>      time.sleep(10)
> 
> when both the del clt.ctx and del clt.co lines are there, there is no 
> leak at all. if both of them are commented out, the memory usage goes to 
> about 100MB, and if only the context is deleted, the memory usage is 
> about 80MB.

Add a
    def __del__(self): print 'del'
method and you'll see that when both the del:s in the loop are present,
it'll print 'del', but if you comment any one of them they will
disappear.

Compare with a similar program.

===
class foo:
    def __init__(self, x):
        self.foo = x

class bar:
    def __init__(self):
        self.bar = foo(self.m)
    def m(self):
        pass
    def __del__(self):
        print 'del'

while 1:
    x = bar()
    #del x.bar
===

It has the exact same problem.


/Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Detta ?r en digitalt signerad	meddelandedel
URL: <http://mail.python.org/pipermail/pyopenssl-users/attachments/20030108/5dfefd68/attachment.pgp>


More information about the pyopenssl-users mailing list