- # 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_file)
+ def refresh_cache():
+ ns.cfg = with_closing(file(src_path)) (
+ lambda f: yaml.load(f, loader))
+ try: with_closing(file(cache_path + '.tmp', 'w')) (
+ lambda f: f.write(json.write(ns.cfg)))
+ except: pass # silent failure
+ else: rename(cache_path + '.tmp', cache_path)
+ except IOError:
+ ns.cfg = with_closing(file(src_path)) (
+ lambda f: yaml.load(f, loader))
+ return ns.cfg