Skip to content

Commit 7355d9f

Browse files
authored
Merge pull request #65 from sjp/fix-file-locking
Avoid leaving file handles open
2 parents b42cca2 + 2b4f9e4 commit 7355d9f

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public static string[] GetDependencies(string module)
1919

2020
public static bool HasPdb(string module)
2121
{
22-
using (var peReader = new PEReader(File.OpenRead(module)))
22+
using (var moduleStream = File.OpenRead(module))
23+
using (var peReader = new PEReader(moduleStream))
2324
{
2425
foreach (var entry in peReader.ReadDebugDirectory())
2526
{

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.Collections.Generic;
21
using System.IO;
32
using System.Linq;
43

54
using Coverlet.Core.Helpers;
6-
using Coverlet.Core.Extensions;
75

86
using Mono.Cecil;
97
using Mono.Cecil.Cil;
@@ -13,8 +11,8 @@ namespace Coverlet.Core.Instrumentation
1311
{
1412
internal class Instrumenter
1513
{
16-
private string _module;
17-
private string _identifier;
14+
private readonly string _module;
15+
private readonly string _identifier;
1816
private InstrumenterResult _result;
1917

2018
public Instrumenter(string module, string identifier)
@@ -48,25 +46,26 @@ public InstrumenterResult Instrument()
4846
private void InstrumentModule()
4947
{
5048
using (var stream = new FileStream(_module, FileMode.Open, FileAccess.ReadWrite))
49+
using (var resolver = new DefaultAssemblyResolver())
5150
{
52-
var resolver = new DefaultAssemblyResolver();
5351
resolver.AddSearchDirectory(Path.GetDirectoryName(_module));
5452
var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver };
55-
ModuleDefinition module = ModuleDefinition.ReadModule(stream, parameters);
56-
57-
foreach (var type in module.GetTypes())
53+
using (var module = ModuleDefinition.ReadModule(stream, parameters))
5854
{
59-
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
60-
continue;
61-
62-
foreach (var method in type.Methods)
55+
foreach (var type in module.GetTypes())
6356
{
64-
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
65-
InstrumentMethod(method);
57+
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
58+
continue;
59+
60+
foreach (var method in type.Methods)
61+
{
62+
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
63+
InstrumentMethod(method);
64+
}
6665
}
67-
}
6866

69-
module.Write(stream);
67+
module.Write(stream);
68+
}
7069
}
7170
}
7271

@@ -138,7 +137,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
138137
return pathInstr;
139138
}
140139

141-
private bool IsBranchTarget(ILProcessor processor, Instruction instruction)
140+
private static bool IsBranchTarget(ILProcessor processor, Instruction instruction)
142141
{
143142
foreach (var _instruction in processor.Body.Instructions)
144143
{
@@ -155,7 +154,7 @@ private bool IsBranchTarget(ILProcessor processor, Instruction instruction)
155154
return false;
156155
}
157156

158-
private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
157+
private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
159158
{
160159
if (instruction.Operand is Instruction _instruction)
161160
{
@@ -175,7 +174,7 @@ private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTa
175174
}
176175
}
177176

178-
private void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
177+
private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
179178
{
180179
if (handler.FilterStart == oldTarget)
181180
handler.FilterStart = newTarget;

0 commit comments

Comments
 (0)