Skip to content

Commit 5b36d6c

Browse files
committed
[libc++][lit] Atomically update the persistent cache
When running multiple shards in parallel, one shard might write to the cache while another one is reading this cache. Instead of updating the file in place, write to a temporary file and swap the cache file using os.replace(). This is an atomic operation and means shards will either see the old state or the new one.
1 parent d2ab97b commit 5b36d6c

File tree

1 file changed

+7
-1
lines changed
  • libcxx/utils/libcxx/test

1 file changed

+7
-1
lines changed

libcxx/utils/libcxx/test/dsl.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ def f(config, *args, **kwargs):
6969
if cacheKey not in cache:
7070
cache[cacheKey] = function(config, *args, **kwargs)
7171
# Update the persistent cache so it knows about the new key
72-
with open(persistentCache, "wb") as cacheFile:
72+
# We write to a PID-suffixed file and rename the result to
73+
# ensure that the cache is not corrupted when running the test
74+
# suite with multiple shards. Since this file is in the same
75+
# directory as the destination, os.replace() will be atomic.
76+
unique_suffix = ".tmp." + str(os.getpid())
77+
with open(persistentCache + unique_suffix, "wb") as cacheFile:
7378
pickle.dump(cache, cacheFile)
79+
os.replace(persistentCache + unique_suffix, persistentCache)
7480
return cache[cacheKey]
7581

7682
return f

0 commit comments

Comments
 (0)