Avoid race condition during config update
This commit is contained in:
parent
aa66f8d615
commit
1a0ccb22b7
18
app.py
18
app.py
|
@ -97,8 +97,9 @@ def verify_node(d):
|
|||
if "/" in node:
|
||||
raise ValueError("invalid node name %s", node)
|
||||
try:
|
||||
log.info("getting client config from %s", "config/" + node)
|
||||
with open("config/" + node) as fh:
|
||||
fn = "config/" + node
|
||||
log.info("getting client config from %s", fn)
|
||||
with open(fn) as fh:
|
||||
node_conf = json.load(fh)
|
||||
except Exception as e:
|
||||
log.warning("got %s opening %s", e, "config/" + node)
|
||||
|
@ -112,8 +113,19 @@ def verify_node(d):
|
|||
if timestamp > node_conf["last"]:
|
||||
node_conf["last"] = timestamp
|
||||
os.replace("config/" + node, "config/" + node + ".old")
|
||||
with open("config/" + node, "w") as fh:
|
||||
tmpfn = fn + "." + str(os.getpid())
|
||||
oldfn = fn + ".old"
|
||||
with open(tmpfn, "w") as fh:
|
||||
json.dump(node_conf, fh) # XXX
|
||||
try:
|
||||
os.unlink(oldfn)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
try:
|
||||
os.link(fn, oldfn)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
os.rename(tmpfn, fn)
|
||||
return node
|
||||
else:
|
||||
abort(409, "timestamp out of sync")
|
||||
|
|
Loading…
Reference in New Issue