Skip to content

Restore backup assemblies when sigkill #383

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

Merged
merged 1 commit into from
May 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Coverlet.Core.Helpers
{
internal static class InstrumentationHelper
{
private static readonly Dictionary<string, string> _backupList = new Dictionary<string, string>();

static InstrumentationHelper()
{
AppDomain.CurrentDomain.ProcessExit += (s,e) => RestoreOriginalModules();
}

public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly)
{
Debug.Assert(directories != null);
Expand Down Expand Up @@ -87,11 +94,13 @@ public static void BackupOriginalModule(string module, string identifier)
var backupPath = GetBackupPath(module, identifier);
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb");
File.Copy(module, backupPath, true);
_backupList.Add(module, backupPath);

var symbolFile = Path.ChangeExtension(module, ".pdb");
if (File.Exists(symbolFile))
{
File.Copy(symbolFile, backupSymbolPath, true);
_backupList.Add(symbolFile, backupSymbolPath);
}
}

Expand All @@ -108,18 +117,39 @@ public static void RestoreOriginalModule(string module, string identifier)
{
File.Copy(backupPath, module, true);
File.Delete(backupPath);
_backupList.Remove(module);
}, retryStrategy, 10);

RetryHelper.Retry(() =>
{
if (File.Exists(backupSymbolPath))
{
File.Copy(backupSymbolPath, Path.ChangeExtension(module, ".pdb"), true);
string symbolFile = Path.ChangeExtension(module, ".pdb");
File.Copy(backupSymbolPath, symbolFile, true);
File.Delete(backupSymbolPath);
_backupList.Remove(symbolFile);
}
}, retryStrategy, 10);
}

public static void RestoreOriginalModules()
{
// Restore the original module - retry up to 10 times, since the destination file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var retryStrategy = CreateRetryStrategy();

foreach (string key in _backupList.Keys.ToList())
{
string backupPath = _backupList[key];
RetryHelper.Retry(() =>
{
File.Copy(backupPath, key, true);
File.Delete(backupPath);
_backupList.Remove(key);
}, retryStrategy, 10);
}
}

public static void DeleteHitsFile(string path)
{
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
Expand Down