Protect redis access with a lock, as pmain.process() and background can both mutate the endpoints dict potentially at the same time.
@@ -153,10 +153,12 @@ class SDNConnect(object):
self.get_sdn_context()
self.endpoints = {}
self.investigations = 0
+ self.redis_lock = threading.Lock()
self.connect_redis()
def get_stored_endpoints(self):
- # load existing endpoints if any
+ ''' load existing endpoints from Redis. '''
+ with self.redis_lock:
endpoints = {}
if self.r:
try:
@@ -528,10 +530,10 @@ class SDNConnect(object):
(item[0], item[4], item[5]), int(time.time()))
self.store_endpoints()
self.get_stored_endpoints()
- return
def store_endpoints(self):
- # store latest version of endpoints in redis
+ ''' store current endpoints in Redis. '''
+ with self.redis_lock:
if self.r:
try:
serialized_endpoints = []
@@ -574,7 +576,6 @@ class SDNConnect(object):
except Exception as e: # pragma: no cover
self.logger.error(
'Unable to store endpoints in Redis because {0}'.format(str(e)))
- return
class Monitor(object):
