Skip to content
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

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

Open
tepatelcmc opened this issue May 2, 2023 · 17 comments
Labels
bug Something isn't working tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly

Comments

@tepatelcmc
Copy link

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)

@daveMueller
Copy link
Collaborator

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 daveMueller added the waiting for customer Waiting for customer action label May 6, 2023
@mathijs-dumon
Copy link

Don't think he means that one, but this one:
#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.b__0() in //src/coverlet.core/Helpers/InstrumentationHelper.cs:line 295
at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.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

@mareklinka
Copy link

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.

@github-actions
Copy link

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

@github-actions github-actions bot added the stale label Sep 10, 2023
@roryprimrose
Copy link

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

@github-actions github-actions bot removed the stale label Oct 15, 2023
Copy link

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

@github-actions github-actions bot added the stale label Jan 14, 2024
@Bertk Bertk added the needs more info More details are needed label Feb 7, 2024
@Bertk
Copy link
Collaborator

Bertk commented Feb 7, 2024

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.

@github-actions github-actions bot removed the stale label Feb 11, 2024
Copy link

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

@github-actions github-actions bot added the stale label May 12, 2024
@tripleacoder
Copy link

tripleacoder commented May 14, 2024

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 true to test projects 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.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 true to test projects 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.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.

@github-actions github-actions bot removed the stale label May 19, 2024
@roryprimrose
Copy link

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

@aya-bjoseph
Copy link

aya-bjoseph commented Jul 1, 2024

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.

@OskiKervinen-MF
Copy link

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
Copy link

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 c27f704 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 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 added a commit to OskiKervinen-MF/cecil-fork that referenced this issue Jul 19, 2024
The Problem:

Problem observed when being used in coverlet:
coverlet-coverage/coverlet#1471
If an exception caused `NativePdbWriter.Write` to never be
called, it would not call `SymWriter.Close`,
which in turn meant `Marshal.ReleaseComObject` was left uncalled.

The garbage collector will eventually destroy the object
and thereby release the COM object, but that happens non-deterministically.
The COM object is holding to a file handle, which prevents
other operations on the written file until it is released.
The result was random file access issues.

The Solution:

Luckily NativePdbWriter is IDisposable. I added a call to
`writer.Close` there. But now it could be called twice,
so I had to add a boolean to SymWriter to avoid releasing
everything twice.
@REscobar
Copy link

I believe this (microsoft/testfx#2563) issue on testfx is caused by this one

@Bertk Bertk added the driver-console Issue related to dotnet net tool driver label Sep 15, 2024
@Bertk
Copy link
Collaborator

Bertk commented Sep 15, 2024

@tripleacoder Please check known issues BadImageFormatException .NET Framework 4.7.x, 4.8.x

This issue is related to “Unable to instrument module” exception and “The process cannot access the file 'XXX.dll or XXX.pdb' because it is being used by another process.”

Related external issue: Fixed: SymWriter COM object is not released on exception

@Bertk Bertk added tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly and removed needs more info More details are needed driver-console Issue related to dotnet net tool driver waiting for customer Waiting for customer action labels Sep 15, 2024
@Bertk Bertk added the bug Something isn't working label Sep 15, 2024
@tripleacoder
Copy link

@tripleacoder Please check known issues BadImageFormatException .NET Framework 4.7.x, 4.8.x

This issue is related to “Unable to instrument module” exception and “The process cannot access the file 'XXX.dll or XXX.pdb' because it is being used by another process.”

Thanks, changing DebugType to 'portable' made the error go away.
However, I am using DebugType ='full' in my Nuget packages to allow debugging into them, so I cannot use the workaround for those, at least not without further testing.

jbevain added a commit to jbevain/cecil that referenced this issue Sep 25, 2024
* Fixed: SymWriter COM object is not released on exception

The Problem:

Problem observed when being used in coverlet:
coverlet-coverage/coverlet#1471
If an exception caused `NativePdbWriter.Write` to never be
called, it would not call `SymWriter.Close`,
which in turn meant `Marshal.ReleaseComObject` was left uncalled.

The garbage collector will eventually destroy the object
and thereby release the COM object, but that happens non-deterministically.
The COM object is holding to a file handle, which prevents
other operations on the written file until it is released.
The result was random file access issues.

The Solution:

Luckily NativePdbWriter is IDisposable. I added a call to
`writer.Close` there. But now it could be called twice,
so I had to add a boolean to SymWriter to avoid releasing
everything twice.

* Code style

---------

Co-authored-by: Jb Evain <[email protected]>
@Bertk
Copy link
Collaborator

Bertk commented Oct 14, 2024

Note

Portable PDB - Why a new format?
While the Windows PDB format has worked okay over the years, with .NET Core the Roslyn team decided it was time to go back to the drawing board and come up with a new format. A few of the reasons:

  • The Windows PDB format is complex, and not well documented. This complexity is important for some of the native code scenarios that the format was designed for, but it is unnecessary for the .NET scenarios. The portable format is open source and documented.
  • At the time the effort was begun there wasn't a cross-platform library that could read or write the original windows PDB format.
  • The Windows PDB format is not a compact representation for managed code. Significant size reductions can be obtained with a new format without losing any information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly
Projects
None yet
Development

No branches or pull requests

10 participants