coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

The process cannot access the file, because it is being used by another process" error during attempt to collect coverage

Open tepatelcmc opened this issue 1 year ago • 17 comments

I can see previous issue being fixed in an unofficial version - did it ever make it to stable release?

The issue still exist with running test in parallel with vstest code coverage (XPlat code coverage)

tepatelcmc avatar May 02 '23 01:05 tepatelcmc

Are you refering to #1155? The fix for this wasn't released yet, but it will be in the next release. You can try out the fix by consuming our nightly build (https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md).

daveMueller avatar May 06 '23 22:05 daveMueller

Don't think he means that one, but this one: https://github.com/coverlet-coverage/coverlet/issues/857

I'm facing this issue again when using coverlet.console v6.0.0 Since we didn't have the issue before my guess was a nugget, but it seems like it's actually the dotnet global tool (coverlet.console) which is causing the problem.

Just did some more testing with different versions of the tool: I don't have the issue using v3.1.x or v3.2.0, but I do with v6.0

With v6 I get this kind of exception:

---> System.IO.IOException: The process cannot access the file '(real path left out for nda reasons).pdb' because it is being used by another process. at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass17_0.<RestoreOriginalModules>b__0() in //src/coverlet.core/Helpers/InstrumentationHelper.cs:line 295 at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in //src/coverlet.core/Helpers/RetryHelper.cs:line 29 at Coverlet.Core.Helpers.RetryHelper.Do[T](Func1 action, Func1 backoffStrategy, Int32 maxAttemptCount) in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 55

mathijs-dumon avatar Jun 02 '23 21:06 mathijs-dumon

I just started encountering this same after installing the newly-released C# Dev Kit for VSCode. It seems the processes started by VSCode keep a handle open on the assemblies:

TpTrace Warning: 0 : 20944, 1, 2023/06/07, 15:33:20.113, 4930720815067, datacollector.dll, [coverlet]Unable to instrument module: C:\Source\Personal\fourth-pharos\backend\FourthPharos.Domain.Tests\bin\Debug\net7.0\FourthPharos.Domain.dll
System.IO.IOException: The process cannot access the file 'C:\Source\Personal\fourth-pharos\backend\FourthPharos.Domain.Tests\bin\Debug\net7.0\FourthPharos.Domain.dll' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at Coverlet.Core.Helpers.FileSystem.NewFileStream(String path, FileMode mode, FileAccess access) in /_/src/coverlet.core/Helpers/FileSystem.cs:line 52
   at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 207
   at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 149
   at Coverlet.Core.Coverage.PrepareModules() in /_/src/coverlet.core/Coverage.cs:line 135

Once I close VSCode and run dotnet test again, instrumentation works as expected. Probably not much Coverlet can do about it, just wanted to mention it. I'll open an issue in the Dev Kit repo, once it goes public.

mareklinka avatar Jun 07 '23 13:06 mareklinka

This issue is stale because it has been open for 3 months with no activity.

github-actions[bot] avatar Sep 10 '23 01:09 github-actions[bot]

This has been a problem for my team as we have minimum code coverage being applied to changed code as a policy in pull requests. This bug is preventing us from being able to complete pull requests.

I have found that with this bug in 6.0.0, we get this error in the dotnet test build log and 0% coverage reported for that project. I tried downgrading to 3.2.0. That version didn't throw the error but also reported 0% coverage. I have downgraded to 3.1.2 which:

  • does not throw the error
  • correctly reports coverage percentage
  • correctly identifies and excludes coverage for assemblies that are not in the repository

roryprimrose avatar Oct 14 '23 05:10 roryprimrose

This issue is stale because it has been open for 3 months with no activity.

github-actions[bot] avatar Jan 14 '24 01:01 github-actions[bot]

Issue should be closed because the related coverlet nuget package is not mentioned and comments for all packages are available. The new issue template for bugs should improve this.

Bertk avatar Feb 07 '24 15:02 Bertk

This issue is stale because it has been open for 3 months with no activity.

github-actions[bot] avatar May 12 '24 01:05 github-actions[bot]

I get the same error on one of my .NET 4.8 projects.

I am using the coverlet console tool as part of an Azure Pipeline.

Tool version is 6.0.2

The exit code from coverlet is 255, which is undocumented.

Command:

coverlet C:\agents\A1\_work\64/RAT.IntTest.dll --target dotnet --targetargs test C:\agents\A1\_work\64/RAT.IntTest.dll --no-build --logger trx --diag:log.txt --format cobertura --source-mapping-file C:\agents\A1\_work\64/SourceMapping.txt --use-source-link --output C:\agents\A1\_work\64/coverage/coverage.cobertura.xml --verbosity detailed

Output:

(...) BadImageFormatException during MetadataReaderProvider.FromPortablePdbStream in InstrumentationHelper.PortablePdbHasLocalSource, unable to check if module has got local source. TryWithCustomResolverOnDotNetCore for PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Unable to instrument module: C:\agents\A1_work\64\RAT.exe Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Try to add <PreserveCompilationContext>true</PreserveCompilationContext> to test projects </PropertyGroup> or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' --- End of inner exception stack trace --- at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.TryWithCustomResolverOnDotNetCore(AssemblyNameReference name) in //src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 217 at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.Resolve(AssemblyNameReference name) in //src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 129 at Mono.Cecil.MetadataResolver.Resolve(TypeReference type) at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type) at Mono.Cecil.TypeReference.Resolve() at Mono.Cecil.Mixin.CheckedResolve(TypeReference self) at Mono.Cecil.MetadataBuilder.GetConstantType(TypeReference constant_type, Object constant) at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type) at Mono.Cecil.MetadataBuilder.AddParameter(UInt16 sequence, ParameterDefinition parameter, ParamTable table) at Mono.Cecil.MetadataBuilder.AddParameters(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type) at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type) at Mono.Cecil.MetadataBuilder.AddTypes() at Mono.Cecil.MetadataBuilder.BuildTypes() at Mono.Cecil.MetadataBuilder.BuildModule() at Mono.Cecil.MetadataBuilder.BuildMetadata() at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader ) at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 stream, WriterParameters parameters) at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters) at Mono.Cecil.ModuleDefinition.Write(Stream stream, WriterParameters parameters) at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in //src/coverlet.core/Instrumentation/Instrumenter.cs:line 325 at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in //src/coverlet.core/Instrumentation/Instrumenter.cs:line 148 at Coverlet.Core.Coverage.PrepareModules() in //src/coverlet.core/Coverage.cs:line 135 One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) Unhandled exception. System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) ---> System.IO.IOException: The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.

BadImageFormatException during MetadataReaderProvider.FromPortablePdbStream in InstrumentationHelper.PortablePdbHasLocalSource, unable to check if module has got local source. TryWithCustomResolverOnDotNetCore for PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Unable to instrument module: C:\agents\A1_work\64\RAT.exe Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Try to add <PreserveCompilationContext>true</PreserveCompilationContext> to test projects </PropertyGroup> or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' --- End of inner exception stack trace --- at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.TryWithCustomResolverOnDotNetCore(AssemblyNameReference name) in //src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 217 at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.Resolve(AssemblyNameReference name) in //src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 129 at Mono.Cecil.MetadataResolver.Resolve(TypeReference type) at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type) at Mono.Cecil.TypeReference.Resolve() at Mono.Cecil.Mixin.CheckedResolve(TypeReference self) at Mono.Cecil.MetadataBuilder.GetConstantType(TypeReference constant_type, Object constant) at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type) at Mono.Cecil.MetadataBuilder.AddParameter(UInt16 sequence, ParameterDefinition parameter, ParamTable table) at Mono.Cecil.MetadataBuilder.AddParameters(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type) at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type) at Mono.Cecil.MetadataBuilder.AddTypes() at Mono.Cecil.MetadataBuilder.BuildTypes() at Mono.Cecil.MetadataBuilder.BuildModule() at Mono.Cecil.MetadataBuilder.BuildMetadata() at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader ) at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 stream, WriterParameters parameters) at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters) at Mono.Cecil.ModuleDefinition.Write(Stream stream, WriterParameters parameters) at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in //src/coverlet.core/Instrumentation/Instrumenter.cs:line 325 at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in //src/coverlet.core/Instrumentation/Instrumenter.cs:line 148 at Coverlet.Core.Coverage.PrepareModules() in //src/coverlet.core/Coverage.cs:line 135 One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) Unhandled exception. System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) ---> System.IO.IOException: The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.

tripleacoder avatar May 14 '24 10:05 tripleacoder

We still have this issue on 6.0.0, 6.0.1 and 6.0.2. Version 3.2.0 still works fine.

roryprimrose avatar May 28 '24 06:05 roryprimrose

We still have this issue on 6.0.0, 6.0.1 and 6.0.2. Version 3.2.0 still works fine.

Same here. v6.0.2 has the "because it is being used by another process." error but v3.2.0 works without issue.

aya-bjoseph avatar Jul 01 '24 17:07 aya-bjoseph

For me, using Coverlet 6.0.2 as a dotnet tool, it looks like it's actually the coverlet process itself, which is holding the lock. Behold this Process monitor log: image Process 33472 is the coverlet dotnet tool execution. You can observe that it performed a WriteFile on the first line, and there is no CloseFile after that so it clearly must still have a write handle of some kind open. On the last couple of lines the same process is trying to open a new write handle to the same file and receives a SHARING VIOLATION.

VS Code's Code.exe does indeed execute in the middle, but it's just reading file attributes. I was able to reproduce the error with no VS code open, it even occurs in our CI pipeline.

Here's the stack of the WriteFile that succeeds:

WriteFile, SUCCESS
0	FLTMGR.SYS	FltGetStreamContext + 0x20cb	0xfffff80255f2963b	C:\Windows\System32\drivers\FLTMGR.SYS
1	FLTMGR.SYS	FltGetStreamContext + 0x483	0xfffff80255f279f3	C:\Windows\System32\drivers\FLTMGR.SYS
2	FLTMGR.SYS	FltGetEcpListFromCallbackData + 0x2bf	0xfffff80255f6255f	C:\Windows\System32\drivers\FLTMGR.SYS
3	ntoskrnl.exe	NtWriteFile + 0x518	0xfffff80252496ed8	C:\Windows\system32\ntoskrnl.exe
4	ntoskrnl.exe	NtWriteFile + 0x263	0xfffff80252496c23	C:\Windows\system32\ntoskrnl.exe
5	ntoskrnl.exe	setjmpex + 0x9085	0xfffff8025222f805	C:\Windows\system32\ntoskrnl.exe
6	ntdll.dll	NtWriteFile + 0x14	0x7ffb6ca900a4	C:\Windows\System32\ntdll.dll
7	KernelBase.dll	WriteFile + 0x7b	0x7ffb6a24b9ab	C:\Windows\System32\KernelBase.dll
8	msvcr80.dll	is_wctype + 0x9da	0x70489f5a	C:\Windows\WinSxS\amd64_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.9680_none_88e394a52fab6222\msvcr80.dll
9	msvcr80.dll	write + 0xc5	0x7048a0e5	C:\Windows\WinSxS\amd64_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.9680_none_88e394a52fab6222\msvcr80.dll
10	diasymreader.dll	VSDllUnregisterServer + 0x2331c	0x7ffb08947354	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
11	diasymreader.dll	VSDllUnregisterServer + 0x23885	0x7ffb089478bd	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
12	diasymreader.dll	VSDllUnregisterServer + 0x24dbb	0x7ffb08948df3	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
13	diasymreader.dll	VSDllUnregisterServer + 0x24f2c	0x7ffb08948f64	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
14	diasymreader.dll	VSDllUnregisterServer + 0x25d4c	0x7ffb08949d84	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
15	diasymreader.dll	VSDllUnregisterServer + 0x2050d	0x7ffb08944545	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
16	diasymreader.dll	VSDllUnregisterServer + 0x205ef	0x7ffb08944627	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
17	diasymreader.dll	VSDllUnregisterServer + 0x11f1a	0x7ffb08935f52	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
18	diasymreader.dll	VSDllUnregisterServer + 0x1d899	0x7ffb089418d1	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
19	diasymreader.dll	VSDllUnregisterServer + 0x10ebd	0x7ffb08934ef5	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
20	diasymreader.dll	VSDllUnregisterServer + 0xbb06	0x7ffb0892fb3e	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
21	diasymreader.dll	VSDllUnregisterServer + 0x90a7	0x7ffb0892d0df	C:\Windows\Microsoft.NET\Framework64\v2.0.50727\diasymreader.dll
22	<unknown>	0x7ffa82a58495	0x7ffa82a58495	

Here's the CreateFile (=Open a new handle) operation that fails:

CreateFile, SHARING VIOLATION
0	FLTMGR.SYS	FltGetStreamContext + 0x20cb	0xfffff80255f2963b	C:\Windows\System32\drivers\FLTMGR.SYS
1	FLTMGR.SYS	FltGetStreamContext + 0x1b51	0xfffff80255f290c1	C:\Windows\System32\drivers\FLTMGR.SYS
2	FLTMGR.SYS	FltRequestFileInfoOnCreateCompletion + 0x4ef	0xfffff80255f61fef	C:\Windows\System32\drivers\FLTMGR.SYS
3	ntoskrnl.exe	IofCallDriver + 0x55	0xfffff80252049b05	C:\Windows\system32\ntoskrnl.exe
4	ntoskrnl.exe	SeQuerySecurityDescriptorInfo + 0xa01e	0xfffff802524aa9be	C:\Windows\system32\ntoskrnl.exe
5	ntoskrnl.exe	ObOpenObjectByNameEx + 0xf21	0xfffff802524d0221	C:\Windows\system32\ntoskrnl.exe
6	ntoskrnl.exe	ObOpenObjectByNameEx + 0x1f2	0xfffff802524cf4f2	C:\Windows\system32\ntoskrnl.exe
7	ntoskrnl.exe	NtCreateFile + 0xba7	0xfffff802524b6637	C:\Windows\system32\ntoskrnl.exe
8	ntoskrnl.exe	NtCreateFile + 0x79	0xfffff802524b5b09	C:\Windows\system32\ntoskrnl.exe
9	ntoskrnl.exe	setjmpex + 0x9085	0xfffff8025222f805	C:\Windows\system32\ntoskrnl.exe
10	ntdll.dll	NtCreateFile + 0x14	0x7ffb6ca90a44	C:\Windows\System32\ntdll.dll
11	KernelBase.dll	BasepCopyFileCallback + 0x1143	0x7ffb6a2707a3	C:\Windows\System32\KernelBase.dll
12	KernelBase.dll	BasepCopyFileExW + 0xc45	0x7ffb6a26d695	C:\Windows\System32\KernelBase.dll
13	KernelBase.dll	CopyFileExW + 0xe3	0x7ffb6a26c423	C:\Windows\System32\KernelBase.dll
14	System.Private.CoreLib.dll	System.Private.CoreLib.dll + 0x1a8936	0x7ffae1378936	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.30\System.Private.CoreLib.dll
15	System.Private.CoreLib.dll	System.Private.CoreLib.dll + 0x1a8859	0x7ffae1378859	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.30\System.Private.CoreLib.dll
16	System.Private.CoreLib.dll	System.Private.CoreLib.dll + 0x399f50	0x7ffae1569f50	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.30\System.Private.CoreLib.dll
17	System.Private.CoreLib.dll	System.Private.CoreLib.dll + 0x3908cd	0x7ffae15608cd	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.30\System.Private.CoreLib.dll
18	<unknown>	0x7ffa824010af	0x7ffa824010af

Here's the error stack of the coverlet failure, matching the SHARING_VIOLATION procmon event:

Error stack
System.IO.IOException: The process cannot access the file 'C:\GIT\REDACTED\X.Mocks.pdb' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in /_/src/coverlet.core/Helpers/FileSystem.cs:line 35
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass18_0.<RestoreOriginalModules>b__0() in /_/src/coverlet.core/Helpers/InstrumentationHelper.cs:line 297
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 28
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 55
   --- End of inner exception stack trace ---
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 62
   at Coverlet.Core.Helpers.RetryHelper.Retry(Action action, Func`1 backoffStrategy, Int32 maxAttemptCount) in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.InstrumentationHelper.RestoreOriginalModules() in /_/src/coverlet.core/Helpers/InstrumentationHelper.cs:line 295
   at Coverlet.Core.Helpers.InstrumentationHelper.<.ctor>b__7_0(Object s, EventArgs e) in /_/src/coverlet.core/Helpers/InstrumentationHelper.cs:line 32
   at System.AppContext.OnProcessExit()

My interpretation of the event stacks is that coverlet is trying to copy the file, while it still has open a previous handle, that was used to manipulate the PDB with diasymreader.dll.

PS: Why is coverlet editing the PDB anyway? Is it adjusting some offsets after instrumenting the executable shifted addresses around?

OskiKervinen-MF avatar Jul 15 '24 12:07 OskiKervinen-MF

I was able to catch coverlet.console.exe in the debugger, trying to restore the original modules, with process explorer showing it still having a handle to the PDB: image Curiously, the problem does not occur if enough time has passed since the target executable was built. That certainly makes it seems like a problem related to some other process having the handle, but it verifiably is coverlet itself.

Experiment done with a local debug build I made from commit c27f704e41863036854a04db7a486d292960392a of master.

Looking at the call stack, this RestoreOriginalModule invocation is coming from the exception handler in Coverage.PrepareModules. So it's possible the problem only occurs when something else has already gone wrong. The exception being handled was:

AssemblyResolutionException for 'System.Diagnostics.EventLog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Try to add <PreserveCompilationContext>true</PreserveCompilationContext> to test projects </PropertyGroup> or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line. I tried the hint of adding the PreserveCompilationContext and it didn't help. Both the original exception, nor the further error encountered while recovering from it still occur.

OskiKervinen-MF avatar Jul 18 '24 11:07 OskiKervinen-MF

I believe this (https://github.com/microsoft/testfx/issues/2563) issue on testfx is caused by this one

REscobar avatar Jul 19 '24 21:07 REscobar