# HG changeset patch # User Ludovic Chabant # Date 1623821783 25200 # Node ID a7c43131d871bdbd18e99f6bda5a9a3599d94df6 # Parent 161cba5d031abaad28868d49156ab300f3083288 bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :( diff -r 161cba5d031a -r a7c43131d871 piecrust/cache.py --- a/piecrust/cache.py Tue Jun 15 22:34:51 2021 -0700 +++ b/piecrust/cache.py Tue Jun 15 22:36:23 2021 -0700 @@ -93,6 +93,8 @@ def write(self, path, content): with self.openWrite(path, mode='w', encoding='utf8') as fp: fp.write(content) + fp.flush() + os.fsync(fp) def openWrite(self, path, mode='w', encoding=None): cache_path = self.getCachePath(path) @@ -186,6 +188,8 @@ fs_key = _make_fs_cache_key(key) with self.fs_cache.openWrite(fs_key, mode='wb') as fp: pickle.dump(item, fp, pickle.HIGHEST_PROTOCOL) + fp.flush() + os.fsync(fp) def get(self, key, item_maker, fs_cache_time=None, save_to_fs=True): self._last_access_hit = True @@ -221,6 +225,8 @@ if self.fs_cache is not None and save_to_fs: with self.fs_cache.openWrite(fs_key, mode='wb') as fp: pickle.dump(item, fp, pickle.HIGHEST_PROTOCOL) + fp.flush() + os.fsync(fp) return item diff -r 161cba5d031a -r a7c43131d871 piecrust/page.py --- a/piecrust/page.py Tue Jun 15 22:34:51 2021 -0700 +++ b/piecrust/page.py Tue Jun 15 22:36:23 2021 -0700 @@ -271,8 +271,18 @@ page_time = source.getItemMtime(content_item) if cache.isValid(cache_path, page_time): try: + cache_raw = cache.read(cache_path) + if not cache_raw: + for i in range(5): + cache_raw = cache.read(cache_path) + if cache_raw: + logger.warn("Had to re-pull the cache %d time(s)!" % (i + 1)) + break + if not cache_raw: + raise Exception("Cache is busted!") + cache_data = json.loads( - cache.read(cache_path), + cache_raw, object_pairs_hook=collections.OrderedDict) config = PageConfiguration( values=cache_data['config'],