Fix locking

Calls to save() in purgeOld() and update() were in race condition.
Race condition was eliminated by extending the lock scope within purgeOld().
Calls to load() were in race condition with themselves.
While current use of load() (called only during construction of class ServerList()) does not manifest the bug, any future change introducing concurrent use of load() would.
Race condition potential was eliminated by extending the lock scope within load().
This commit is contained in:
nOOb3167 2018-03-17 10:02:01 -04:00 committed by sfan5
parent 78abbee771
commit f5bddaaef5

View File

@ -349,19 +349,19 @@ class ServerList:
def purgeOld(self):
with self.lock:
self.list = [server for server in self.list if time.time() <= server["update_time"] + app.config["PURGE_TIME"]]
self.save()
self.save()
def load(self):
try:
with open(os.path.join("static", "list.json"), "r") as fd:
data = json.load(fd)
except FileNotFoundError:
return
if not data:
return
with self.lock:
try:
with open(os.path.join("static", "list.json"), "r") as fd:
data = json.load(fd)
except FileNotFoundError:
return
if not data:
return
self.list = data["list"]
self.maxServers = data["total_max"]["servers"]
self.maxClients = data["total_max"]["clients"]