Skip to content

AccessViolationException in HttpClient.SendAsync() on macOS arm64 in .NET 9 preview.4 #102313

Closed

Description

@martincostello

Description

I have an application I'm testing against .NET 9 daily builds. With the latest SDK and package versions I'm seeing System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt. errors in my tests when running on GitHub Actions' macOS arm64 runners.

I have 9 applications I'm doing this with, but I'm only observing this issue with one of them.

Reproduction Steps

  1. Clone martincostello/costellobot@18966b8
  2. Run build.ps1 on a macOS arm64 machine.

Expected behavior

The tests all pass.

Actual behavior

Lots of test failures with a stack trace similar to the below:

System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at MartinCostello.Costellobot.Infrastructure.IntegrationTests`1.PostWebhookAsync(String event, Object value, String webhookSecret, String delivery) in /_/tests/Costellobot.Tests/Infrastructure/IntegrationTests`1.cs:line 176
   at MartinCostello.Costellobot.Handlers.DeploymentStatusHandlerTests.PostWebhookAsync(DeploymentStatusDriver driver, String action) in /_/tests/Costellobot.Tests/Handlers/DeploymentStatusHandlerTests.cs:line 667
   at MartinCostello.Costellobot.Handlers.DeploymentStatusHandlerTests.Deployment_Is_Approved_For_Trusted_User_And_Dependency_When_Penultimate_Build_Skipped() in /_/tests/Costellobot.Tests/Handlers/DeploymentStatusHandlerTests.cs:line 128
--- End of stack trace from previous location ---

That corresponds to this code, but it doesn't appear to have been recently changed:

async Task<HttpResponseMessage> Core(
HttpRequestMessage request, HttpCompletionOption completionOption,
CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
{
bool telemetryStarted = StartSend(request);
bool responseContentTelemetryStarted = false;
HttpResponseMessage? response = null;
try
{
// Wait for the send request to complete, getting back the response.
response = await base.SendAsync(request, cts.Token).ConfigureAwait(false);
ThrowForNullResponse(response);
// Buffer the response content if we've been asked to.
if (ShouldBufferResponse(completionOption, request))
{
if (HttpTelemetry.Log.IsEnabled() && telemetryStarted)
{
HttpTelemetry.Log.ResponseContentStart();
responseContentTelemetryStarted = true;
}
await response.Content.LoadIntoBufferAsync(_maxResponseContentBufferSize, cts.Token).ConfigureAwait(false);
}
return response;
}
catch (Exception e)
{
HandleFailure(e, telemetryStarted, response, cts, originalCancellationToken, pendingRequestsCts);
throw;
}
finally
{
FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted);
}
}

Regression?

Yes.

Known Workarounds

None.

Configuration

.NET SDK 9.0.100-preview.4.24265.4

Other information

No response

Activity

  1. added
    untriaged
    on May 16, 2024
  2. dotnet-policy-service commented on May 16, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on May 16, 2024
  4. added this to the 9.0.0 milestone on May 16, 2024
  5. antonfirsov commented on May 16, 2024

    @antonfirsov
    Contributor

    @martincostello any chance you can help us by collecting traces? This could give us some preliminary ideas before someone manages to jump on the issue.

    dotnet-trace collect --providers System.Net.Http,Private.InternalDiagnostics.System.Net.Http,System.Net.Security,System.Net.Sockets,System.Net.NameResolution,System.Threading.Tasks.TplEventSource:0x80:4 --process-id <PID>
    
  6. antonfirsov commented on May 16, 2024

    @antonfirsov
    Contributor

    Could this be the root cause?

  7. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor

    any chance you can help us by collecting traces?

    Sure, I'll try and do this later today.

    Could #101479 (comment) be the root cause?

    I did spot that issue, but it seemed a bit too old to still be an issue in the nightlies?

  8. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor
    dotnet-trace collect --providers System.Net.Http,Private.InternalDiagnostics.System.Net.Http,System.Net.Security,System.Net.Sockets,System.Net.NameResolution,System.Threading.Tasks.TplEventSource:0x80:4 --process-id <PID>
    

    Just looked at this - how do I do this exactly when I don't know what the test process' ID will be until after I start the tests?

  9. wfurt commented on May 16, 2024

    @wfurt
    Member

    I know this annoying. I often add Console.Readline() to the repro and hit enter after tracing started. Other alternative is to add EventListener the the repro itself.

  10. wfurt commented on May 16, 2024

    @wfurt
    Member

    BTW the linked issue blames libgit2sharp. Do you see same issue on any other platform @martincostello ?

  11. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor

    At the moment my repro is just "run dotnet test" - I don't have anything more specific at the moment.

    I could maybe add something to the tests to have the tests start monitor itself (if that works?)

    I'm only seeing this on macOS, not Windows or Linux, but only in this one project. I'm not specifically using lib2gitsharp anywhere, and if it is being used, I would assume it's from an SDK or something and would/should be present in my other repos I'm testing nightlies with.

  12. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor

    If it helps narrow the search, this has started happening at some point since the 24th April, but all I had to go on was "dotnet test crashes" at that point and I figured it would stabilise at some point. It's only in the last day or so I've noticed it's progressed to not killing the whole process and giving me an exception message and stack trace.

  13. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor

    But that probably correlates more with GitHub Actions runners moving from macOS 12 x64 to macOS 14 arm64, so it was probably broken before then and my build was just on a different arch so I didnt see it before...

  14. martincostello commented on May 16, 2024

    @martincostello
    MemberAuthor

    I don't seem to be able to get hold of a .netrace file as now dotnet test is just crashing again. I do have a dump file I collected a few days ago, but it's ~1.3GB zipped and will contain secrets from my repository that are configured in environment variables.

    If you let me know what the right secure process to provide the dump file is, I can provide one tomorrow.

  15. 15 remaining items

  16. janvorli commented on Jun 5, 2024

    @janvorli
    Member

    @martincostello could you please share a dump with that crash with me? I am not aware of any instabilities due to the new exception handling in the runtime, so I really want to get to the bottom of this issue. My email is my alias at microsoft.com

  17. martincostello commented on Jun 5, 2024

    @martincostello
    MemberAuthor

    @janvorli Just emailed you a link.

  18. janvorli commented on Jun 5, 2024

    @janvorli
    Member

    @martincostello the dump you have shared with me is somehow broken. I am unable to see the stack trace of the failing thread across a signal handler. Would you be able to capture / share another one?

  19. martincostello commented on Jun 5, 2024

    @martincostello
    MemberAuthor

    Sure, I can try - what are the best flags to pass to dotnet test to get the most useful dump I can for you?

  20. janvorli commented on Jun 5, 2024

    @janvorli
    Member

    I don't have any preference for those. However, instead of letting .NET generate the dump, it might be useful to try to get a dump using the macOS dump collection facility. That means

    • running ulimit -c unlimited
    • running the dotnet test with sudo or adding your user to the wheel group or changing the protection of the /cores directory so that the OS can write the dump there

    After that, running the dotnet test from the same console should produce dump in the /cores directory.

    Sometimes, I had problems with dumps collected by our tools in specific cases, so that's why I am suggesting this.

  21. martincostello commented on Jun 5, 2024

    @martincostello
    MemberAuthor

    OK - I'm just working on a way to get my CI (I don't have access to any physical mac hardware myself) to upload crash dumps to somewhere securely without putting them into GitHub Actions artifacts first, then once that's working I'll try the extra steps above.

    In the meantime, I just had an instance of the tests not crash, but fail with the following stack trace which I've not seen before:

    System.ObjectDisposedException : The CancellationTokenSource has been disposed.
       at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
       at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
       at MartinCostello.Costellobot.Infrastructure.IntegrationTests`1.PostWebhookAsync(String event, Object value, String webhookSecret, String delivery) in /_/tests/Costellobot.Tests/Infrastructure/IntegrationTests`1.cs:line 176
       at MartinCostello.Costellobot.Handlers.DeploymentStatusHandlerTests.PostWebhookAsync(DeploymentStatusDriver driver, String action) in /_/tests/Costellobot.Tests/Handlers/DeploymentStatusHandlerTests.cs:line 667
       at MartinCostello.Costellobot.Handlers.DeploymentStatusHandlerTests.Deployment_Is_Approved_For_Trusted_User_And_Dependency_When_Penultimate_Build_Skipped() in /_/tests/Costellobot.Tests/Handlers/DeploymentStatusHandlerTests.cs:line 128
    --- End of stack trace from previous location ---
    
  22. janvorli commented on Jun 5, 2024

    @janvorli
    Member

    @martincostello I think the issue in the dumps you have shared with me is the issue is most likely a problem already fixed by #101971, so it should be fixed in preview 5. I guess the failures you are getting in preview 5 are likely something else.
    It would be great if you could share a dump from a crash with the preview 5 runtime too.

  23. martincostello commented on Jun 5, 2024

    @martincostello
    MemberAuthor

    @janvorli Sure - I'll try and get one of the preview 5 ones for you tomorrow.

  24. martincostello commented on Jun 6, 2024

    @martincostello
    MemberAuthor

    Not observing any crashes this morning - I'll keep any eye out as my automation updates to newer preview.5 builds daily. If I don't observe any further crashes by the time preview.5 officially ships I'll close this issue.

  25. martincostello commented on Jun 12, 2024

    @martincostello
    MemberAuthor

    I can't repro this as of the official preview 5, so I'm going to close this on the basis it must have gotten fixed at some point.

  26. wfurt commented on Jun 12, 2024

    @wfurt
    Member

    great. Thanks for the confirmation @martincostello

  27. locked and limited conversation to collaborators on Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ExceptionHandling-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions