Skip to content

Commit 53fbc49

Browse files
authored
Merge pull request #383 from ViktorHofer/SigKill
Restore backup assemblies when sigkill
2 parents 4ebe395 + 32a57ef commit 53fbc49

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ namespace Coverlet.Core.Helpers
1414
{
1515
internal static class InstrumentationHelper
1616
{
17+
private static readonly Dictionary<string, string> _backupList = new Dictionary<string, string>();
18+
19+
static InstrumentationHelper()
20+
{
21+
AppDomain.CurrentDomain.ProcessExit += (s,e) => RestoreOriginalModules();
22+
}
23+
1724
public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly)
1825
{
1926
Debug.Assert(directories != null);
@@ -87,11 +94,13 @@ public static void BackupOriginalModule(string module, string identifier)
8794
var backupPath = GetBackupPath(module, identifier);
8895
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb");
8996
File.Copy(module, backupPath, true);
97+
_backupList.Add(module, backupPath);
9098

9199
var symbolFile = Path.ChangeExtension(module, ".pdb");
92100
if (File.Exists(symbolFile))
93101
{
94102
File.Copy(symbolFile, backupSymbolPath, true);
103+
_backupList.Add(symbolFile, backupSymbolPath);
95104
}
96105
}
97106

@@ -108,18 +117,39 @@ public static void RestoreOriginalModule(string module, string identifier)
108117
{
109118
File.Copy(backupPath, module, true);
110119
File.Delete(backupPath);
120+
_backupList.Remove(module);
111121
}, retryStrategy, 10);
112122

113123
RetryHelper.Retry(() =>
114124
{
115125
if (File.Exists(backupSymbolPath))
116126
{
117-
File.Copy(backupSymbolPath, Path.ChangeExtension(module, ".pdb"), true);
127+
string symbolFile = Path.ChangeExtension(module, ".pdb");
128+
File.Copy(backupSymbolPath, symbolFile, true);
118129
File.Delete(backupSymbolPath);
130+
_backupList.Remove(symbolFile);
119131
}
120132
}, retryStrategy, 10);
121133
}
122134

135+
public static void RestoreOriginalModules()
136+
{
137+
// Restore the original module - retry up to 10 times, since the destination file could be locked
138+
// See: https://github.com/tonerdo/coverlet/issues/25
139+
var retryStrategy = CreateRetryStrategy();
140+
141+
foreach (string key in _backupList.Keys.ToList())
142+
{
143+
string backupPath = _backupList[key];
144+
RetryHelper.Retry(() =>
145+
{
146+
File.Copy(backupPath, key, true);
147+
File.Delete(backupPath);
148+
_backupList.Remove(key);
149+
}, retryStrategy, 10);
150+
}
151+
}
152+
123153
public static void DeleteHitsFile(string path)
124154
{
125155
// Retry hitting the hits file - retry up to 10 times, since the file could be locked

0 commit comments

Comments
 (0)