coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

[BUG] Fatal error 0xC0000005 on instrumentation

Open rootix opened this issue 7 months ago • 1 comments

Describe the bug We use coverlet.collector together with dotnet test command. On our CI-Environemnt (Windows Agent on Azure DevOps OnPrem) we get periodically failures while the tests are running. We have over 20 Test projects and everytime it is a different project that has the error. Sometimes it runs trough without any issue.

I searched for the stacktrace (below) and found some vstest issues which pointed to change from 17.13.0 to 17.14.preview (https://github.com/microsoft/vstest/issues/2952#issuecomment-2739425376) and another post which mentioned 17.10.0 as the one where the issues does not occur with vstest (https://github.com/microsoft/vstest/issues/10402). But i think the issue is not vstest itself but Coverlet which produces the initial error which causes then the vstest process to fail. I experienced the issue with 17.13.0 and 17.14.preview but not with 17.10.0 after 2 pipeline runs. But i got on both other versions runs without an issue, so i have low confidence that this is the fix.

To Reproduce I cannot provide the projects that have this issue and don't know how to make a minimal reproduction for this issue.

Expected behavior There is no error with coverlet enabled.

Actual behavior The stacktrace that we get is:

   Fatal error. 0xC0000005
      at Mono.Cecil.Pdb.ISymUnmanagedWriter2.Close()
      at Mono.Cecil.Pdb.SymWriter.Close()
      at Mono.Cecil.Pdb.NativePdbWriter.Write()
      at Mono.Cecil.ModuleWriter.Write(Mono.Cecil.ModuleDefinition, Mono.Disposable`1<System.IO.Stream>, Mono.Cecil.WriterParameters)
      at Mono.Cecil.ModuleWriter.WriteModule(Mono.Cecil.ModuleDefinition, Mono.Disposable`1<System.IO.Stream>, Mono.Cecil.WriterParameters)
      at Mono.Cecil.ModuleDefinition.Write(System.IO.Stream, Mono.Cecil.WriterParameters)
      at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule()
      at Coverlet.Core.Instrumentation.Instrumenter.Instrument()
      at Coverlet.Core.Coverage.PrepareModules()
      at Coverlet.Collector.DataCollection.CoverageWrapper.PrepareModules(Coverlet.Core.Coverage)
      at Coverlet.Collector.DataCollection.CoverageManager.InstrumentModules()
      at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(System.Object, Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.SessionStartEventArgs)
      at System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)
      at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(System.Object, System.Span`1<System.Object>, System.Reflection.BindingFlags)
      at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
      at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
      at System.Delegate.DynamicInvokeImpl(System.Object[])
      at Microsoft.VisualStudio.TestPlatform.Utilities.MulticastDelegateUtilities.SafeInvoke(System.Delegate, System.Object, System.Object, System.String)
      at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.TestPlatformDataCollectionEvents.RaiseEvent(Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.DataCollectionEventArgs)
      at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionManager.SessionStarted(Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.SessionStartEventArgs)
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestHandler.HandleBeforeTestRunStart(Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Message)
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestHandler.ProcessRequests()
      at Microsoft.VisualStudio.TestPlatform.DataCollector.DataCollectorMain.Run(System.String[])
      at Microsoft.VisualStudio.TestPlatform.DataCollector.Program.Main(System.String[])
   
   System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
    ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
      at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
      --- End of inner exception stack trace ---
      at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
      at System.Net.Sockets.NetworkStream.ReadByte()
      at System.IO.BinaryReader.Read7BitEncodedInt()
      at System.IO.BinaryReader.ReadString()
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.ReceiveRawMessage() in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 321
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult(String settingsXml, IEnumerable`1 sources, Boolean isTelemetryOptedIn, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollectionRequestSender.cs:line 119
      at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass21_0.<BeforeTestRunStart>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 197
      at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 288
   System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
    ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
      at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
      --- End of inner exception stack trace ---
      at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
      at System.IO.BufferedStream.Flush()
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.WriteAndFlushToChannel(String rawMessage) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 413
      at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendAfterTestRunEndAndGetResult(ITestMessageEventHandler runEventsHandler, Boolean isCancelled) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollectionRequestSender.cs:line 155
      at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass20_0.<AfterTestRunEnd>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 155

The tests are executed with the following command (per project since we split unit and integration test projects):

dotnet.exe test <path-to-csproj>.csproj --configuration Release --no-build --no-restore --verbosity minimal --collect "XPlat Code Coverage" --settings <path-to>\coverlet.runsettings --logger nunit;LogFilePath=<path-to>/<project>.TestResults.xml

Configuration (please complete the following information): Please provide more information on your .NET configuration: * Which coverlet package and version was used? -> 6.0.4 * Which version of .NET is the code running on? 9.0.200 * What OS and version, and what distro if applicable? Azure DevOps Agent Runner (Windows) * What is the architecture (x64, x86, ARM, ARM64)? -> x64 * Do you know whether it is specific to that configuration? -> No

Additional context

Content of our coverlet.runsettings:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>cobertura</Format>
          <Exclude>[Namespace.*.Test.*]*, [Namespace.Shared.Test]*,[*]*.DbAccess.Migrations.*</Exclude>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

rootix avatar Apr 24 '25 15:04 rootix

Thank you for sharing this information. I am not sure whether this sporadic problem is coverlet related. Does this error also occur on developer systems (local environment). Coverlet unit test and system tests are also executed with Azure DevOps CI.

  • Maybe you can check whether this problem also occur with the nightly build version of coverlet.collector

  • Could you please check whether a DLL is blocked by a hung up test process

  • Which target framework is used by test project (I guess you use net9.0)

https://github.com/coverlet-coverage/coverlet/blob/0a2024d31a4c0948fabe7d1bfe8c4e1f0725cab2/src/coverlet.core/Instrumentation/Instrumenter.cs#L324C6-L324C74

https://github.com/jbevain/cecil/blob/3136847ea620fb9b4a3ff96bc4f573148e8bd2e4/symbols/pdb/Mono.Cecil.Pdb/SymWriter.cs#L80-L91

Potential Causes

  1. File locking issues during instrumentation
  2. Concurrent access to PDB files
  3. Possible race conditions in cleanup/restore phases

Investigation Steps

  1. Local Environment Testing

    • Verify if the issue reproduces consistently in local development environments
  2. Process Isolation

    • Check for hung processes holding file locks
    • Ensure all file handles are properly released
  3. Build Environment

Bertk avatar Apr 28 '25 07:04 Bertk

My team began experiencing the same intermittent error as soon as the Azure DevOps pipeline for a large-ish app was updated to use the windows-2025 agent image. We have tried different images several times since then, and for us, the problem definitely occurs in windows-2025 only. It does not occur on developer workstations. We have as many as 15 builds running concurrently and see this issue several times each day.

Coverlet is collectors 6.0.4. The app in question targets .NET Framework 4.8.1 mostly, with some code multi-targeted with .NET 6. The crashed test runs happen in tests targeting both frameworks.

In our case, the error message gives a bit more description.

##[command]"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_work\_temp\4afc94c1-32f2-4e14-917b-5daea54031bc.ps1'"
Test run for D:\a\_work\1\s\REDACTED.Tests\bin\Debug\REDACTED.Tests.dll (.NETFramework,Version=v4.8.1)
VSTest version 17.11.1 (x64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at Mono.Cecil.Pdb.ISymUnmanagedWriter2.Close()
--------------------------------
   at Mono.Cecil.Pdb.SymWriter.Close()
   at Mono.Cecil.Pdb.NativePdbWriter.Write()
   at Mono.Cecil.ModuleWriter.Write(Mono.Cecil.ModuleDefinition, Mono.Disposable`1<System.IO.Stream>, Mono.Cecil.WriterParameters)
   at Mono.Cecil.ModuleWriter.WriteModule(Mono.Cecil.ModuleDefinition, Mono.Disposable`1<System.IO.Stream>, Mono.Cecil.WriterParameters)
   at Mono.Cecil.ModuleDefinition.Write(System.IO.Stream, Mono.Cecil.WriterParameters)
   at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule()
   at Coverlet.Core.Instrumentation.Instrumenter.Instrument()
   at Coverlet.Core.Coverage.PrepareModules()
   at Coverlet.Collector.DataCollection.CoverageWrapper.PrepareModules(Coverlet.Core.Coverage)
   at Coverlet.Collector.DataCollection.CoverageManager.InstrumentModules()
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(System.Object, Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.SessionStartEventArgs)
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(System.Object, System.Span`1<System.Object>, System.Reflection.BindingFlags)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Delegate.DynamicInvokeImpl(System.Object[])
   at Microsoft.VisualStudio.TestPlatform.Utilities.MulticastDelegateUtilities.SafeInvoke(System.Delegate, System.Object, System.Object, System.String)
   at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.TestPlatformDataCollectionEvents.RaiseEvent(Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.DataCollectionEventArgs)
   at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionManager.SessionStarted(Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.SessionStartEventArgs)
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestHandler.HandleBeforeTestRunStart(Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Message)
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestHandler.ProcessRequests()
   at Microsoft.VisualStudio.TestPlatform.DataCollector.DataCollectorMain.Run(System.String[])
   at Microsoft.VisualStudio.TestPlatform.DataCollector.Program.Main(System.String[])

System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
 ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
   at System.Net.Sockets.NetworkStream.ReadByte()
   at System.IO.BinaryReader.Read7BitEncodedInt()
   at System.IO.BinaryReader.ReadString()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.ReceiveRawMessage() in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 321
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult(String settingsXml, IEnumerable`1 sources, Boolean isTelemetryOptedIn, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollectionRequestSender.cs:line 119
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass21_0.<BeforeTestRunStart>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 197
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 288
  Skipped REDACTED [1 ms]
  Skipped REDACTED
  Skipped REDACTED
  Skipped REDACTED
  Skipped REDACTED
  Skipped REDACTED
  Skipped REDACTED
System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
 ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Flush()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.WriteAndFlushToChannel(String rawMessage) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 413
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendAfterTestRunEndAndGetResult(ITestMessageEventHandler runEventsHandler, Boolean isCancelled) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollectionRequestSender.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass20_0.<AfterTestRunEnd>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 288
Results File: D:\a\_work\_temp\results\AzDevOps_15994438e000000_2025-08-19_17_22_51.trx

Passed!  - Failed:     0, Passed: 21192, Skipped:     6, Total: 21198, Duration: 6 m - REDACTED.Tests.dll (net481)
##[error]PowerShell exited with code '1'.

The AzDO yaml that runs the tests is:

- pwsh: dotnet test REDACTED.Tests --no-build -c $(configuration)
    --logger trx
    --settings Coverlet.runsettings
    --blame-hang-timeout 10m
    --results-directory $(Agent.TempDirectory)\results
  displayName: Run unit tests (old)

Our runsettings file is:

<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat Code Coverage">
        <Configuration>
          <Format>opencover</Format>
          <Exclude>[REDACTED.Tests*]*</Exclude>
          <ExcludeByAttribute>GeneratedCode,TestSDKAutoGeneratedCode</ExcludeByAttribute>
          <SkipAutoProps>true</SkipAutoProps>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

sharpjs avatar Aug 19 '25 18:08 sharpjs

I just confirmed that the error occurs for us both in test suites targeting .NET Framework 4.8.1 and in those targeting .NET 6.

Test run for D:\a\_work\1\s\OGpro.Tests.Unit\bin\Debug\net6.0\OGpro.Tests.Unit.dll (.NETCoreApp,Version=v6.0)
VSTest version 17.11.1 (x64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at Mono.Cecil.Pdb.ISymUnmanagedWriter2.Close()
--------------------------------
   at Mono.Cecil.Pdb.SymWriter.Close()
   at Mono.Cecil.Pdb.NativePdbWriter.Write()
...SAME AS PREVIOUS...

sharpjs avatar Aug 19 '25 22:08 sharpjs