Skip to content

Commit e939796

Browse files
committed
Write to a temp file and move, in case there is contention
1 parent f8e161d commit e939796

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteMefComposition.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,13 @@ private static async Task TrySaveCachedExportProviderAsync(CachedComposition cac
120120
var cacheDirectory = Path.GetDirectoryName(compositionCacheFile).AssumeNotNull();
121121
var directoryInfo = Directory.CreateDirectory(cacheDirectory);
122122

123-
using var cacheStream = new FileStream(compositionCacheFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true);
124-
await cache.SaveAsync(runtimeComposition, cacheStream, cancellationToken).ConfigureAwait(false);
123+
var tempFilePath = Path.Combine(cacheDirectory, Path.GetRandomFileName());
124+
using (var cacheStream = new FileStream(compositionCacheFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
125+
{
126+
await cache.SaveAsync(runtimeComposition, cacheStream, cancellationToken).ConfigureAwait(false);
127+
}
128+
129+
File.Move(tempFilePath, compositionCacheFile);
125130
}
126131
catch (Exception)
127132
{

0 commit comments

Comments
 (0)