mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Don't use key in weakvaluedict
because it could lie.
Example: ```python import weakref class O(object): pass d = weakref.WeakValueDictionary() key = 'key' value = O() d[key] = value d[key] # <O object at 0x...> del value key in d # True d.get(key) # None ```
This commit is contained in:
parent
016189f62f
commit
e721c7eee2
4 changed files with 30 additions and 35 deletions
|
@ -372,17 +372,18 @@ class AsyncAuth(object):
|
|||
loop_instance_map = AsyncAuth.instance_map[io_loop]
|
||||
|
||||
key = cls.__key(opts)
|
||||
if key not in loop_instance_map:
|
||||
auth = loop_instance_map.get(key)
|
||||
if auth is None:
|
||||
log.debug('Initializing new AsyncAuth for {0}'.format(key))
|
||||
# we need to make a local variable for this, as we are going to store
|
||||
# it in a WeakValueDictionary-- which will remove the item if no one
|
||||
# references it-- this forces a reference while we return to the caller
|
||||
new_auth = object.__new__(cls)
|
||||
new_auth.__singleton_init__(opts, io_loop=io_loop)
|
||||
loop_instance_map[key] = new_auth
|
||||
auth = object.__new__(cls)
|
||||
auth.__singleton_init__(opts, io_loop=io_loop)
|
||||
loop_instance_map[key] = auth
|
||||
else:
|
||||
log.debug('Re-using AsyncAuth for {0}'.format(key))
|
||||
return loop_instance_map[key]
|
||||
return auth
|
||||
|
||||
@classmethod
|
||||
def __key(cls, opts, io_loop=None):
|
||||
|
@ -1008,14 +1009,15 @@ class SAuth(AsyncAuth):
|
|||
Only create one instance of SAuth per __key()
|
||||
'''
|
||||
key = cls.__key(opts)
|
||||
if key not in SAuth.instances:
|
||||
auth = SAuth.instances.get(key)
|
||||
if auth is None:
|
||||
log.debug('Initializing new SAuth for {0}'.format(key))
|
||||
new_auth = object.__new__(cls)
|
||||
new_auth.__singleton_init__(opts)
|
||||
SAuth.instances[key] = new_auth
|
||||
auth = object.__new__(cls)
|
||||
auth.__singleton_init__(opts)
|
||||
SAuth.instances[key] = auth
|
||||
else:
|
||||
log.debug('Re-using SAuth for {0}'.format(key))
|
||||
return SAuth.instances[key]
|
||||
return auth
|
||||
|
||||
@classmethod
|
||||
def __key(cls, opts, io_loop=None):
|
||||
|
|
|
@ -248,15 +248,16 @@ class IPCClient(object):
|
|||
# FIXME
|
||||
key = str(socket_path)
|
||||
|
||||
if key not in loop_instance_map:
|
||||
client = loop_instance_map.get(key)
|
||||
if client is None:
|
||||
log.debug('Initializing new IPCClient for path: {0}'.format(key))
|
||||
new_client = object.__new__(cls)
|
||||
client = object.__new__(cls)
|
||||
# FIXME
|
||||
new_client.__singleton_init__(io_loop=io_loop, socket_path=socket_path)
|
||||
loop_instance_map[key] = new_client
|
||||
client.__singleton_init__(io_loop=io_loop, socket_path=socket_path)
|
||||
loop_instance_map[key] = client
|
||||
else:
|
||||
log.debug('Re-using IPCClient for {0}'.format(key))
|
||||
return loop_instance_map[key]
|
||||
return client
|
||||
|
||||
def __singleton_init__(self, socket_path, io_loop=None):
|
||||
'''
|
||||
|
|
|
@ -221,17 +221,18 @@ class AsyncTCPReqChannel(salt.transport.client.ReqChannel):
|
|||
loop_instance_map = cls.instance_map[io_loop]
|
||||
|
||||
key = cls.__key(opts, **kwargs)
|
||||
if key not in loop_instance_map:
|
||||
obj = loop_instance_map.get(key)
|
||||
if obj is None:
|
||||
log.debug('Initializing new AsyncTCPReqChannel for {0}'.format(key))
|
||||
# we need to make a local variable for this, as we are going to store
|
||||
# it in a WeakValueDictionary-- which will remove the item if no one
|
||||
# references it-- this forces a reference while we return to the caller
|
||||
new_obj = object.__new__(cls)
|
||||
new_obj.__singleton_init__(opts, **kwargs)
|
||||
loop_instance_map[key] = new_obj
|
||||
obj = object.__new__(cls)
|
||||
obj.__singleton_init__(opts, **kwargs)
|
||||
loop_instance_map[key] = obj
|
||||
else:
|
||||
log.debug('Re-using AsyncTCPReqChannel for {0}'.format(key))
|
||||
return loop_instance_map[key]
|
||||
return obj
|
||||
|
||||
@classmethod
|
||||
def __key(cls, opts, **kwargs):
|
||||
|
|
|
@ -80,28 +80,19 @@ class AsyncZeroMQReqChannel(salt.transport.client.ReqChannel):
|
|||
loop_instance_map = cls.instance_map[io_loop]
|
||||
|
||||
key = cls.__key(opts, **kwargs)
|
||||
if key not in loop_instance_map:
|
||||
obj = loop_instance_map.get(key)
|
||||
if obj is None:
|
||||
log.debug('Initializing new AsyncZeroMQReqChannel for {0}'.format(key))
|
||||
# we need to make a local variable for this, as we are going to store
|
||||
# it in a WeakValueDictionary-- which will remove the item if no one
|
||||
# references it-- this forces a reference while we return to the caller
|
||||
new_obj = object.__new__(cls)
|
||||
new_obj.__singleton_init__(opts, **kwargs)
|
||||
loop_instance_map[key] = new_obj
|
||||
obj = object.__new__(cls)
|
||||
obj.__singleton_init__(opts, **kwargs)
|
||||
loop_instance_map[key] = obj
|
||||
log.trace('Inserted key into loop_instance_map id {0} for key {1} and process {2}'.format(id(loop_instance_map), key, os.getpid()))
|
||||
else:
|
||||
log.debug('Re-using AsyncZeroMQReqChannel for {0}'.format(key))
|
||||
try:
|
||||
return loop_instance_map[key]
|
||||
except KeyError:
|
||||
# In iterating over the loop_instance_map, we may have triggered
|
||||
# garbage collection. Therefore, the key is no longer present in
|
||||
# the map. Re-gen and add to map.
|
||||
log.debug('Initializing new AsyncZeroMQReqChannel due to GC for {0}'.format(key))
|
||||
new_obj = object.__new__(cls)
|
||||
new_obj.__singleton_init__(opts, **kwargs)
|
||||
loop_instance_map[key] = new_obj
|
||||
return loop_instance_map[key]
|
||||
return obj
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
cls = self.__class__
|
||||
|
|
Loading…
Add table
Reference in a new issue