Commit faf1306a authored by Antoine Millet's avatar Antoine Millet
Browse files

Fixed shutdown RpcConnection memory leak problem with RpcServer

parent c7a7ad8f
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line

import gc
import ssl
import time
import errno
import socket
import select
import weakref
import logging
import threading

from sjrpc.core import RpcConnection

import pyev
@@ -62,6 +62,16 @@ class RpcServer(object):
            conn = self._wrap(sock)
            self.register(conn)

    def _clean_conn(self, ref):
        '''
        Callback called by weakref when an object is about to be collected by
        garbage collector.
        '''
        try:
            self._clients.remove(ref)
        except KeyError:
            pass

#
# Public methods:
#
@@ -72,7 +82,10 @@ class RpcServer(object):

        :param conn: the connection to register.
        '''
        self._clients.add(conn)
        self._clients.add(weakref.ref(conn, self._clean_conn))
        gc.collect() # Force a manual garbage collection to avoid memory leak
                     # with RpcConnections. This is maybe not required but I
                     # need to read docs about python's gc and circular refs.

    def unregister(self, conn, shutdown=False):
        '''
@@ -85,7 +98,6 @@ class RpcServer(object):
        if conn in self._clients:
            if shutdown:
                conn.shutdown()
            self._clients.remove(conn)

    def run(self):
        '''