- # reload the source and regenerate the cache
- cfg = wrap(file(src_path), lambda f: yaml.load(f, default_loader))
- try: wrap(file(cache_path, 'w'), lambda f: f.write(json.write(cfg)))
- except: pass # silent failure
- return cfg
+ # Atomically reload the source and regenerate the cache. The read and
+ # write must be a single transaction, or a stale version may be
+ # written (if another read/write of a more recent configuration
+ # is interleaved). The final atomic rename is to keep this
+ # transactionally isolated from the above cache read. If we fail to
+ # acquire the lock, just try to load the master configuration.
+ import yaml
+ try: loader = yaml.CSafeLoader
+ except: loader = yaml.SafeLoader
+ try:
+ with lock_file(lock_path):
+ with closing(file(src_path)) as f:
+ ns.cfg = yaml.load(f, loader)
+ try:
+ with closing(file(cache_path + '.tmp', 'w')) as f:
+ f.write(json.write(ns.cfg))
+ except: pass # silent failure
+ else: rename(cache_path + '.tmp', cache_path)
+ except IOError:
+ with closing(file(src_path)) as f:
+ ns.cfg = yaml.load(f, loader)
+ return ns.cfg