Skip to content
Snippets Groups Projects
Commit faf1306a authored by Antoine Millet's avatar Antoine Millet
Browse files

Fixed shutdown RpcConnection memory leak problem with RpcServer

parent c7a7ad8f
No related branches found
No related tags found
No related merge requests found
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):
'''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment