-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc++][lit] Atomically update the persistent cache #66538
Conversation
@llvm/pr-subscribers-libcxx ChangesWhen 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.Full diff: https://github.com/llvm/llvm-project/pull/66538.diff 1 Files Affected:
diff --git a/libcxx/utils/libcxx/test/dsl.py b/libcxx/utils/libcxx/test/dsl.py
index 847cebf5962f6aa..7d4df6b01eabdae 100644
--- a/libcxx/utils/libcxx/test/dsl.py
+++ b/libcxx/utils/libcxx/test/dsl.py
@@ -69,8 +69,12 @@ def f(config, *args, **kwargs):
if cacheKey not in cache:
cache[cacheKey] = function(config, *args, **kwargs)
# Update the persistent cache so it knows about the new key
- with open(persistentCache, "wb") as cacheFile:
+ # We write to a temporary file and rename the result to ensure
+ # that the cahe is not corrupted when running the test suite
+ # with multiple shards.
+ with open(persistentCache + ".tmp", "wb") as cacheFile:
pickle.dump(cache, cacheFile)
+ os.replace(persistentCache + ".tmp", persistentCache)
return cache[cacheKey]
return f
|
d288487
to
f3fc44c
Compare
libcxx/utils/libcxx/test/dsl.py
Outdated
@@ -69,8 +69,12 @@ def f(config, *args, **kwargs): | |||
if cacheKey not in cache: | |||
cache[cacheKey] = function(config, *args, **kwargs) | |||
# Update the persistent cache so it knows about the new key | |||
with open(persistentCache, "wb") as cacheFile: | |||
# We write to a temporary file and rename the result to ensure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have the same problem with the creation of the .tmp
file now? Don't we need to generate a unique file name instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm that's a good point, in my limited testing this was enough to avoid the race, but really it should be a per-shard suffix. Will update to use the PID.
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.
f3fc44c
to
5b36d6c
Compare
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.
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.
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.
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.