Cleanup the singleton instances map.

This prevents closed resources from being reused.
This commit is contained in:
Pedro Algarvio 2019-01-07 19:13:35 +00:00
parent c34a6cda36
commit bd6c46dfe3
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF
3 changed files with 31 additions and 5 deletions

View File

@ -257,6 +257,7 @@ class IPCClient(object):
client = object.__new__(cls)
# FIXME
client.__singleton_init__(io_loop=io_loop, socket_path=socket_path)
client._instance_key = key
loop_instance_map[key] = client
client._refcount = 1
client._refcount_lock = threading.RLock()
@ -400,11 +401,12 @@ class IPCClient(object):
# that a closed entry may not be reused.
# This forces this operation even if the reference
# count of the entry has not yet gone to zero.
if self.io_loop in IPCClient.instance_map:
loop_instance_map = IPCClient.instance_map[self.io_loop]
key = six.text_type(self.socket_path)
if key in loop_instance_map:
del loop_instance_map[key]
if self.io_loop in self.__class__.instance_map:
loop_instance_map = self.__class__.instance_map[self.io_loop]
if self._instance_key in loop_instance_map:
del loop_instance_map[self._instance_key]
if not loop_instance_map:
del self.__class__.instance_map[self.io_loop]
class IPCMessageClient(IPCClient):

View File

@ -241,6 +241,7 @@ class AsyncTCPReqChannel(salt.transport.client.ReqChannel):
# references it-- this forces a reference while we return to the caller
obj = object.__new__(cls)
obj.__singleton_init__(opts, **kwargs)
obj._instance_key = key
loop_instance_map[key] = obj
obj._refcount = 1
obj._refcount_lock = threading.RLock()
@ -308,6 +309,17 @@ class AsyncTCPReqChannel(salt.transport.client.ReqChannel):
self._closing = True
self.message_client.close()
# Remove the entry from the instance map so that a closed entry may not
# be reused.
# This forces this operation even if the reference count of the entry
# has not yet gone to zero.
if self.io_loop in self.__class__.instance_map:
loop_instance_map = self.__class__.instance_map[self.io_loop]
if self._instance_key in loop_instance_map:
del loop_instance_map[self._instance_key]
if not loop_instance_map:
del self.__class__.instance_map[self.io_loop]
def __del__(self):
with self._refcount_lock:
# Make sure we actually close no matter if something

View File

@ -135,6 +135,7 @@ class AsyncZeroMQReqChannel(salt.transport.client.ReqChannel):
# references it-- this forces a reference while we return to the caller
obj = object.__new__(cls)
obj.__singleton_init__(opts, **kwargs)
obj._instance_key = key
loop_instance_map[key] = obj
obj._refcount = 1
obj._refcount_lock = threading.RLock()
@ -230,6 +231,17 @@ class AsyncZeroMQReqChannel(salt.transport.client.ReqChannel):
else:
log.debug('No message_client attr for AsyncZeroMQReqChannel found. Not destroying sockets.')
# Remove the entry from the instance map so that a closed entry may not
# be reused.
# This forces this operation even if the reference count of the entry
# has not yet gone to zero.
if self._io_loop in self.__class__.instance_map:
loop_instance_map = self.__class__.instance_map[self._io_loop]
if self._instance_key in loop_instance_map:
del loop_instance_map[self._instance_key]
if not loop_instance_map:
del self.__class__.instance_map[self._io_loop]
def __del__(self):
with self._refcount_lock:
# Make sure we actually close no matter if something