Skip to content

Commit ebfff1f

Browse files
use ConcurrentDictionary
1 parent 53fbc49 commit ebfff1f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 14 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.Diagnostics;
45
using System.IO;
@@ -14,11 +15,11 @@ namespace Coverlet.Core.Helpers
1415
{
1516
internal static class InstrumentationHelper
1617
{
17-
private static readonly Dictionary<string, string> _backupList = new Dictionary<string, string>();
18+
private static readonly ConcurrentDictionary<string, string> _backupList = new ConcurrentDictionary<string, string>();
1819

1920
static InstrumentationHelper()
2021
{
21-
AppDomain.CurrentDomain.ProcessExit += (s,e) => RestoreOriginalModules();
22+
AppDomain.CurrentDomain.ProcessExit += (s, e) => RestoreOriginalModules();
2223
}
2324

2425
public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly)
@@ -94,13 +95,19 @@ public static void BackupOriginalModule(string module, string identifier)
9495
var backupPath = GetBackupPath(module, identifier);
9596
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb");
9697
File.Copy(module, backupPath, true);
97-
_backupList.Add(module, backupPath);
98+
if (!_backupList.TryAdd(module, backupPath))
99+
{
100+
throw new ArgumentException($"Key already found '{module}'");
101+
}
98102

99103
var symbolFile = Path.ChangeExtension(module, ".pdb");
100104
if (File.Exists(symbolFile))
101105
{
102106
File.Copy(symbolFile, backupSymbolPath, true);
103-
_backupList.Add(symbolFile, backupSymbolPath);
107+
if (!_backupList.TryAdd(symbolFile, backupSymbolPath))
108+
{
109+
throw new ArgumentException($"Key already found '{module}'");
110+
}
104111
}
105112
}
106113

@@ -117,7 +124,7 @@ public static void RestoreOriginalModule(string module, string identifier)
117124
{
118125
File.Copy(backupPath, module, true);
119126
File.Delete(backupPath);
120-
_backupList.Remove(module);
127+
_backupList.TryRemove(module, out string _);
121128
}, retryStrategy, 10);
122129

123130
RetryHelper.Retry(() =>
@@ -127,7 +134,7 @@ public static void RestoreOriginalModule(string module, string identifier)
127134
string symbolFile = Path.ChangeExtension(module, ".pdb");
128135
File.Copy(backupSymbolPath, symbolFile, true);
129136
File.Delete(backupSymbolPath);
130-
_backupList.Remove(symbolFile);
137+
_backupList.TryRemove(symbolFile, out string _);
131138
}
132139
}, retryStrategy, 10);
133140
}
@@ -145,7 +152,7 @@ public static void RestoreOriginalModules()
145152
{
146153
File.Copy(backupPath, key, true);
147154
File.Delete(backupPath);
148-
_backupList.Remove(key);
155+
_backupList.TryRemove(key, out string _);
149156
}, retryStrategy, 10);
150157
}
151158
}

0 commit comments

Comments
 (0)