Skip to content

[.NET6 fix port] AccessViolationException with PublishSingleFile #81181

@marek22

Description

@marek22

Description

I am requesting a port of the fix for issue #62145 from PR #74389 for .NET 6 since it is the LTS release. I confirmed that changing from .NET 6 to .NET 7 fixes the problem in a test application but cannot migrate all applications to .NET 7 at this time.

This bug is impacting me specifically when using the New Relic Agent in Docker when targeting .NET 6 and using the .NET 6 runtime image (mcr.microsoft.com/dotnet/runtime:6.0) but goes away when targeting .NET 7 and using the .NET 7 runtime image (mcr.microsoft.com/dotnet/runtime:7.0). When running containers in the 6.0 configuration it will sporadically throw the AccessViolationException (about 1-2% of the time) but never on the 7.0 configuration.

Reproduction Steps

I copied the reproduction example from the previous issue and tested it in both .NET 6 and .NET 7 by just changing the TargetFramework from net6.0 to net7.0 and was able to reproduce the issue only in .NET 6 using the most recent (6.0.13) runtime with the fix working in .NET 7.

using System.Diagnostics.Tracing;

namespace SegFaultTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            EventListener eventListener = new MyEventListener();

            Console.WriteLine("Press any key to abort...");

            // Do some stuff
            for (int i = 1; i < int.MaxValue; i++)
            {
                if (Console.KeyAvailable)
                {
                    break;
                }

                string a = new('a', i % 10000);
            }

            Console.WriteLine("Done.");
        }
    }

    internal class MyEventListener : EventListener
    {
        protected override void OnEventSourceCreated(EventSource eventSource)
        {
            if (eventSource.Name.Equals("Microsoft-Windows-DotNETRuntime", StringComparison.Ordinal))
            {
                EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)1);
            }
        }

        protected override void OnEventWritten(EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"Event written {eventData.EventId,3} {eventData.EventName ?? string.Empty}");
        }
    }
}

Expected behavior

No AccessViolationException thrown

Actual behavior

AccessViolationException is thrown

Regression?

No response

Known Workarounds

Upgrade to .NET 7

Configuration

Project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	<SelfContained>true</SelfContained>
	<PublishSingleFile>True</PublishSingleFile>
	<PublishReadyToRun>False</PublishReadyToRun>
	<RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

</Project>

Other information

New Relic issue related to this: link
Original dotnet issue: link
Original PR for .NET 7: link

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions