Skip to content

Commit f5bddaa

Browse files
nOOb3167sfan5
authored andcommittedMar 18, 2018
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().
1 parent 78abbee commit f5bddaa

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed
 

‎server.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,19 @@ def server_points(server):
349349
def purgeOld(self):
350350
with self.lock:
351351
self.list = [server for server in self.list if time.time() <= server["update_time"] + app.config["PURGE_TIME"]]
352-
self.save()
352+
self.save()
353353

354354
def load(self):
355-
try:
356-
with open(os.path.join("static", "list.json"), "r") as fd:
357-
data = json.load(fd)
358-
except FileNotFoundError:
359-
return
355+
with self.lock:
356+
try:
357+
with open(os.path.join("static", "list.json"), "r") as fd:
358+
data = json.load(fd)
359+
except FileNotFoundError:
360+
return
360361

361-
if not data:
362-
return
362+
if not data:
363+
return
363364

364-
with self.lock:
365365
self.list = data["list"]
366366
self.maxServers = data["total_max"]["servers"]
367367
self.maxClients = data["total_max"]["clients"]

0 commit comments

Comments
 (0)
Please sign in to comment.