Skip to content

Commit 872656d

Browse files
MikaelMayercpitclaudel
authored andcommitted
Fixed a concurrency issue (#2126)
1 parent a30ee22 commit 872656d

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Source/Dafny/DafnyConsolePrinter.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -7,23 +8,20 @@
78
namespace Microsoft.Dafny;
89

910
public class DafnyConsolePrinter : ConsolePrinter {
10-
private readonly Dictionary<string, List<string>> fsCache = new();
11+
private readonly ConcurrentDictionary<string, List<string>> fsCache = new();
1112
public List<(Implementation, VerificationResult)> VerificationResults { get; } = new();
1213

1314
private string GetFileLine(string filename, int lineIndex) {
14-
List<string> lines;
15-
if (!fsCache.ContainsKey(filename)) {
15+
List<string> lines = fsCache.GetOrAdd(filename, key => {
1616
try {
1717
// Note: This is not guaranteed to be the same file that Dafny parsed. To ensure that, Dafny should keep
1818
// an in-memory version of each file it parses.
1919
lines = File.ReadLines(filename).ToList();
2020
} catch (Exception) {
2121
lines = new List<string>();
2222
}
23-
fsCache.Add(filename, lines);
24-
} else {
25-
lines = fsCache[filename];
26-
}
23+
return lines;
24+
});
2725
if (0 <= lineIndex && lineIndex < lines.Count) {
2826
return lines[lineIndex];
2927
}

0 commit comments

Comments
 (0)