Deadlock in Microsoft.AspNetCore.Mvc.Testing on .NET 7
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### 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](https://github.com/z1c0/Mvc.TestingHang).
The call to `ReadToEnd()` hangs forever.
```cs
internal class KeyValueObserver : IObserver>
{
public void OnNext(KeyValuePair kvp)
{
if (kvp.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")
{
var context = kvp.Value as HttpContext;
var bodyControlFeature = context.Features.Get();
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).
Contributor guide
Assessment
This issue has not been assessed yet.