-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.InvalidOperationException: Reading is already in progress thrown by api-gateway like application #33409
Comments
What's happening here? The ContentLength should be set on the StreamContent you're returning. What's the other
We've had a few reports of EnableBuffering causing conflicts with StreamContent, but not this specific error. Does it work if you remove EnableBuffering? It's not clear from your description if buffering is required for your scenario. Do you need to send the content multiple places? |
This comment has been minimized.
This comment has been minimized.
No, I don't need to send the content multiple times, but something (outside of my knowledge) seems to be reading the content, reason why I had to enable buffering, BUT as I stated before, I'd very much like to NOT have that. I tried adding the content-length but then I started getting I think it's worth saying that under certain scenarios, like local testing, I saw different outcomes. The issue is reproduceable under semi-heavy traffic (+100rps) and the downstream service is a go service. |
Can you provide the full stack traces of the exceptions you're observing as well as when you're seeing them. |
See dotnet/runtime#53914 for a similar scenario and suggestions. |
So we normally see error when the app isn't properly awaiting a request body read. In this case, it's HttpClient that's reading the request body and the I wonder if HttpClient didn't wait for its read from Kestrel's request body to complete and if this is the same issue as dotnet/runtime#53914 |
@BrennanConroy I didn't mean for that to be this issue. I meant for this to be on #33373, but looking at that more closely now, I think HttpRequestPipeReader needs to be a little more thread safe. |
Sorry @Tratcher I had a typo in my code and I have updated already. It was supposed to look like this:
|
@BrennanConroy here are some logs I got using the code as follow:
Stacktrace:
|
Ok, I'd misread some of your earlier description, this makes sense now. There are situations where HttpClient does need to retry sending the request body. If the body wasn't buffered then you get the above error on the retry. Some of the retry conditions are discussed in dotnet/runtime#53914 (comment). 100-continue can be used to mitigate some of these retry situations, but they can't be completely avoided. Edit That doesn't directly explain the other error |
Closing as a dupe of dotnet/runtime#53914 |
This is how my C# app works. I'm getting a request and I want to pass it to a downstream service, so yes, I'm acting as a api-gateway. Problem is that I'm seeing
System.InvalidOperationException: Reading is already in progress exceptions
.The code goes like this:
The MapContent method looks like this:
This is the Exception:
I have obviously omitted many details and code but this is the essence.
Any idea how to reuse the stream to pass it to another service? THE CONTENT CANNOT BE COPIED since this will have a big performance degradation, so this is not acceptable
var content = new ByteArrayContent(await ToByteArray(request.Body));
. It will be awesome also not to useEnableBuffering
since it uses files, but I'm not sure if I can, or the impact of it (that's one of the things I wanted to test with this new code).Thanks a lot in advanced!
Further technical details
.NET 5.0
dotnet --info
Microsoft Visual Studio Professional 2019. Version 16.9.1
The text was updated successfully, but these errors were encountered: