aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Deadlock in Microsoft.AspNetCore.Mvc.Testing on .NET 7

Open z1c0 opened this issue 2 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

The following combination causes a deadlock when trying to read HttpContext.Request.Body:

  • NET 7 (7.0.100)
  • Microsoft.AspNetCore.Mvc.Testing (7.0.0)
  • Using the Microsoft.AspNetCore.Hosting.HttpRequestIn.Start DiagnosticListener event.
  • Using synchronous IO to read the request body (e.g. ReadToEnd()).

The code snippet below is from the linked reproducer sample. The call to ReadToEnd() hangs forever.

internal class KeyValueObserver : IObserver<KeyValuePair<string, object>>
{
    public void OnNext(KeyValuePair<string, object> kvp)
    {
        if (kvp.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")
        {
            var context = kvp.Value as HttpContext;
            
            var bodyControlFeature = context.Features.Get<IHttpBodyControlFeature>();
            bodyControlFeature.AllowSynchronousIO = true;
            
            var stream = new StreamReader(context.Request.Body);
            var body = stream.ReadToEnd();  // Hangs forever!
            body.ToString();
        }
    }
}

Running the same code with previous versions of .NET (Core) works without a problem.

Expected Behavior

The call should not hang as with previous versions.

Steps To Reproduce

Reproducer project: https://github.com/z1c0/Mvc.TestingHang Run the unit test method BasicTests.TestPost.

  • NET 7.0: The test hangs forever
  • NET 6.0: The test terminates and the request body can be read correctly.

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

I could reproduce the problem on Windows, Linux, and macOS. The same code worked fine with previous versions (.NET Core 3.1, .NET 5.0, .NET 6.0 on all platforms).

z1c0 avatar Nov 22 '22 10:11 z1c0

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Nov 23 '22 18:11 ghost

I am getting the same error

MehdiNaz avatar Apr 18 '23 20:04 MehdiNaz

please help me ERORRRRRR Microsoft.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 285) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

MehdiNaz avatar Apr 21 '23 20:04 MehdiNaz

please help me ERORRRRRR Microsoft.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 285) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

MehdiNaz avatar Apr 21 '23 20:04 MehdiNaz