Commit dd6d7401 authored by Julien Muchembled's avatar Julien Muchembled

tests: fix potential resource leak in PortAllocator

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2666 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent d9b41a14
...@@ -57,7 +57,7 @@ class NotFound(Exception): ...@@ -57,7 +57,7 @@ class NotFound(Exception):
class PortAllocator(object): class PortAllocator(object):
lock = SocketLock('neo.PortAllocator') lock = SocketLock('neo.PortAllocator')
allocator_set = set() allocator_set = weakref.WeakKeyDictionary() # BBB: use WeakSet instead
def __init__(self): def __init__(self):
self.socket_list = [] self.socket_list = []
...@@ -67,7 +67,7 @@ class PortAllocator(object): ...@@ -67,7 +67,7 @@ class PortAllocator(object):
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if not self.lock.locked(): if not self.lock.locked():
self.lock.acquire() self.lock.acquire()
self.allocator_set.add(self) self.allocator_set[self] = None
self.socket_list.append(s) self.socket_list.append(s)
s.bind((local_ip, 0)) s.bind((local_ip, 0))
return s.getsockname()[1] return s.getsockname()[1]
...@@ -79,7 +79,7 @@ class PortAllocator(object): ...@@ -79,7 +79,7 @@ class PortAllocator(object):
def reset(self): def reset(self):
if self.lock.locked(): if self.lock.locked():
self.allocator_set.discard(self) self.allocator_set.pop(self, None)
if not self.allocator_set: if not self.allocator_set:
self.lock.release() self.lock.release()
if self.socket_list: if self.socket_list:
...@@ -87,6 +87,8 @@ class PortAllocator(object): ...@@ -87,6 +87,8 @@ class PortAllocator(object):
s.close() s.close()
self.__init__() self.__init__()
__del__ = reset
class NEOProcess(object): class NEOProcess(object):
pid = 0 pid = 0
...@@ -605,10 +607,6 @@ class NEOCluster(object): ...@@ -605,10 +607,6 @@ class NEOCluster(object):
def __del__(self): def __del__(self):
if self.cleanup_on_delete: if self.cleanup_on_delete:
os.removedirs(self.temp_dir) os.removedirs(self.temp_dir)
try:
self.port_allocator.reset()
except AttributeError:
pass
class NEOFunctionalTest(NeoTestBase): class NEOFunctionalTest(NeoTestBase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment