Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
After upgrading from dotnet 6 to dotnet 8, we started observing intermittent failures. Trying dotnet 7 also has the same failures.
Unfortunately this bug is extremely intermittent. We have not been able to create a minimal reproduction case. So far it has only been observed on Mac systems with Java GRPC clients. I've not been able to even trigger it on my desktop, only on CI systems. It is also very timing-sensitive; adding logging to either client or server tends to slow it down just enough for the bug not to manifest. Sorry.
There is only one client, with a thread making requests, a thread making healthcheck calls, and a thread processing a long-lived stream of events structured as an infinite stream of GRPC streaming responses. The failure is roughly four minutes in, and roughly at stream 256 (but not exactly! and it varies).
Once the response is lost the RPC that made it times out 30 seconds later with DEADLINE_EXCEEDED. However, in the meantime, healthcheck calls from the other thread work just fine.
By setting the log level to Trace I was able to obtain this:
Connection id "0HN12DQ56A4EB" received HEADERS frame for stream ID 243 with length 25 and flags END_HEADERS, PRIORITY.
Connection id "0HN12DQ56A4EB" received DATA frame for stream ID 243 with length 108 and flags END_STREAM.
Request starting HTTP/2 POST http://127.0.0.1:9091/StudioLinkProtocol.StudioLinkService/SetDeviceDescription - application/grpc -
All hosts are allowed.
3 candidate(s) found for the request path '/StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Endpoint 'gRPC - /StudioLinkProtocol.StudioLinkService/SetDeviceDescription' with route pattern '/StudioLinkProtocol.StudioLinkService/SetDeviceDescription' is valid for the request path '/StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Endpoint 'gRPC - Unimplemented method for StudioLinkProtocol.StudioLinkService' with route pattern 'StudioLinkProtocol.StudioLinkService/{unimplementedMethod:grpcunimplemented}' is valid for the request path '/StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Endpoint 'gRPC - Unimplemented service' with route pattern '{unimplementedService}/{unimplementedMethod:grpcunimplemented}' is valid for the request path '/StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Request matched endpoint 'gRPC - /StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
The endpoint does not specify the IRequestSizeLimitMetadata.
Executing endpoint 'gRPC - /StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Request deadline timeout of 00:00:29.9999450 started.
Reading message.
Connection id "0HN12DQ56A4EB", Request id "0HN12DQ56A4EB:000000F3": started reading request body.
Connection id "0HN12DQ56A4EB", Request id "0HN12DQ56A4EB:000000F3": done reading request body.
Deserializing 103 byte message to 'StudioLinkProtocol.SetDeviceDescriptionFile'.
Received message.
Sending message.
Serialized 'StudioLinkProtocol.OperationResult' to 0 byte message.
Message sent.
Request deadline stopped.
Executed endpoint 'gRPC - /StudioLinkProtocol.StudioLinkService/SetDeviceDescription'
Request finished HTTP/2 POST http://127.0.0.1:9091/StudioLinkProtocol.StudioLinkService/SetDeviceDescription - 200 - application/grpc 50.2121ms
Expected Behavior
After that I would expect to see
Connection id "0HN12DQ56A4EB" sending HEADERS frame for stream ID 243 with length 4 and flags END_HEADERS.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 243 with length 5 and flags NONE.
Connection id "0HN12DQ56A4EB" sending HEADERS frame for stream ID 243 with length 1 and flags END_STREAM, END_HEADERS.
However there are no such messages, and eventually the client gives up and sends
Connection id "0HN12DQ56A4EB" received RST_STREAM frame for stream ID 243 with length 4 and flags 0x0.
Steps To Reproduce
We do not have a minimal reproduction case for this.
Exceptions (if any)
No response
.NET Version
8.0.101
Anything else?
Grpc.AspNetCore 2.59.0
MacOS 13.6.3 BuildVersion: 22G436 (Ventura)
Activity
pjc50 commentedon Jan 31, 2024
It is possible that the preceding request is relevant, which transfers a large amount of data including a couple of window updates, settings, and ping frames.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 9510 and flags NONE.
Connection id "0HN12DQ56A4EB" received PING frame for stream ID 0 with length 8 and flags NONE.
Connection id "0HN12DQ56A4EB" sending PING frame for stream ID 0 with length 8 and flags ACK.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 0 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 241 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 0 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" received SETTINGS frame for stream ID 0 with length 6 and flags NONE.
Connection id "0HN12DQ56A4EB" sending SETTINGS frame for stream ID 0 with length 0 and flags ACK.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
(repeated many times)
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 9510 and flags NONE.
Connection id "0HN12DQ56A4EB" received PING frame for stream ID 0 with length 8 and flags NONE.
Connection id "0HN12DQ56A4EB" sending PING frame for stream ID 0 with length 8 and flags ACK.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 241 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 0 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" received WINDOW_UPDATE frame for stream ID 241 with length 4 and flags 0x0.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 16384 and flags NONE.
Connection id "0HN12DQ56A4EB" sending DATA frame for stream ID 241 with length 5732 and flags NONE.
Connection id "0HN12DQ56A4EB" sending HEADERS frame for stream ID 241 with length 1 and flags END_STREAM, END_HEADERS.
Connection id "0HN12DQ56A4EB" received SETTINGS frame for stream ID 0 with length 6 and flags NONE.
Connection id "0HN12DQ56A4EB" sending SETTINGS frame for stream ID 0 with length 0 and flags ACK.
Connection id "0HN12DQ56A4EB" received PING frame for stream ID 0 with length 8 and flags NONE.
Connection id "0HN12DQ56A4EB" sending PING frame for stream ID 0 with length 8 and flags ACK.
Tratcher commentedon Feb 2, 2024
Receiving repeated SETTINGS frames is suspicious. Any chance you can get a trace of those? The connection logging middleware would help, or wireshark if it's decrypted.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/connection-middleware?view=aspnetcore-8.0#connection-logging
pjc50 commentedon Feb 2, 2024
I think that's possible, will see on Monday.
pjc50 commentedon Feb 5, 2024
Wireshark output for the last SETTINGS before the failure and its ack:
Frame 30651: 71 bytes on wire (568 bits), 71 bytes captured (568 bits) on interface lo0, id 0
Null/Loopback
Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
Transmission Control Protocol, Src Port: 61800, Dst Port: 9091, Seq: 9766, Ack: 2903349, Len: 15
HyperText Transfer Protocol 2
Stream: SETTINGS, Stream ID: 0, Length 6
Length: 6
Type: SETTINGS (4)
Flags: 0x00
0... .... .... .... .... .... .... .... = Reserved: 0x0
.000 0000 0000 0000 0000 0000 0000 0000 = Stream Identifier: 0
Settings - Initial Windows size : 1559026
Settings Identifier: Initial Windows size (4)
Initial Window Size: 1559026
Frame 30658: 65 bytes on wire (520 bits), 65 bytes captured (520 bits) on interface lo0, id 0
Null/Loopback
Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
Transmission Control Protocol, Src Port: 9091, Dst Port: 61800, Seq: 2974777, Ack: 9781, Len: 9
HyperText Transfer Protocol 2
Stream: SETTINGS, Stream ID: 0, Length 0
Length: 0
Type: SETTINGS (4)
Flags: 0x01, ACK
0... .... .... .... .... .... .... .... = Reserved: 0x0
.000 0000 0000 0000 0000 0000 0000 0000 = Stream Identifier: 0
Selected packets dotnet issue.zip
pjc50 commentedon Feb 5, 2024
Settings packets only.zip
All 72 SETTINGS packets from that TCP stream.
Tratcher commentedon Feb 5, 2024
Weird, the client keeps sending settings frames with increasing Initial Windows Size on the same connection. It starts at 1048576 and gets as high as 5093722. That's not illegal, just highly unusual. The server ack's so it should be fine. It should also only affect new requests, not ongoing streams. I wonder if a buffer gets dropped/replaced somewhere?
noocyte commentedon Sep 21, 2026
We hit what looks like the same bug on ASP.NET Core 10.0.12 (Linux, Kestrel h2c, Grpc.AspNetCore 2.83.0, .NET
SocketsHttpHandler/Grpc.Net.Client 2.83.0 as the client). It is not limited to zero-size responses: our lost response is about 430 KB. Rate: 1 lost response in about 26,700 HTTP/2 requests in a 19-minute run across 37 server processes. It only ever hits the service whose responses span several stream windows.Kestrel trace for the lost stream (149). The preceding stream on the same connection (147) is a 430 KB response: 27 DATA frames, stream WINDOW_UPDATEs interleaved, trailers, and then four more WINDOW_UPDATEs for 147 arrive after its trailers were sent. 284 ms later stream 149 arrives, the app completes, hosting logs
Request finished 200, and Kestrel never sends a single frame for 149. The next stream (151), 300 ms later, is served normally.The client side (
Private.InternalDiagnostics.System.Net.Httptrace) confirms no frame for stream 149 arrived. No Kestrel warning or error was logged on the connection. There were no repeated SETTINGS frames: the connection saw only the initial SETTINGS exchange at 11:01:35, so the increasing-window SETTINGS from the original report are not a necessary condition.Pattern across our three occurrences (two on CI, one local): the lost stream was in every case the very next stream on the connection after a completed large response. Kestrel pools
Http2Streamobjects LIFO, so that next stream reuses the object of the stream that just completed.Possible mechanism, from reading release/10.0.
Http2OutputProducer.StreamResetresets every scheduling field except_isScheduled.Http2FrameWriter.WriteToOutputPipedequeues withwhile (_channel.Reader.TryRead(out var producer) && !producer.CompletedResponse), so a producer that was scheduled once more while its final write was in flight is dropped afterCompleteResponseAsyncwithout ever reachingObserveDataAndState, which is the only place_isScheduledis cleared. A reused stream then inherits_isScheduled == true, and everySchedule()for the new stream returns early. That matches the trace exactly, including "only the stream right after a completed multi-window response". I could not pin down whichSchedule()call races the final write; the late stream WINDOW_UPDATEs after the trailers are the obvious suspect. Resetting_isScheduledinStreamReset(or clearing it when the loop skips a completed producer) would make the reuse safe regardless of the race.I can share the full trace logs for the run if that helps.
Misiu commentedon Oct 2, 2026
TL;DR
SocketsHttpHandlertalks to Kestrel h2c on loopback.Http2Streamfinished with its tail waiting on the client's stream window.WINDOW_UPDATEresume decides to schedule the producer while data is still pending, but enqueues it only after the response has completed.WriteToOutputPipethen drops that entry without clearing_isScheduled, andStreamResetdoes not clear it either. The next request on that pooled stream can never be scheduled.Why this matters to us
We run a reverse proxy (YARP) that forwards requests over HTTP/2 to a Kestrel instance on the other side of a tunnel. One poisoned pooled stream means the next request on that connection gets no response at all, and Kestrel logs nothing. The proxy waits for its activity timeout (100 s by default) and then fails the request.
With the default client window, any response larger than 64 KiB can poison a stream. In our load tests, plain YARP → Kestrel h2c hit the same hang. We would like to see this fixed in Kestrel, ideally serviced in 10.0. Our workaround only covers clients we control, and only responses smaller than the window.
Root cause
Proven (instrumented repro, numbers from our runs):
Http2OutputProducer._isScheduled == trueat handler start, before anything was written._isScheduled == trueand_streamWindow == 65535(nothing sent), and no response HEADERS frame had been sent._isScheduled = falseat handler start when it is found stale gives 0 hangs in 15 runs of 120,000 requests, although each run had 11–29 poisoned starts.Http2Stream(0 of 109); it was always on one reused from the stream pool.END_STREAMsent immediately after aWINDOW_UPDATEfor that stream, in 109 of 109 cases. Only about 57% of all streams end that way._channeland recorded the call stack and producer state of every enqueue.CompletedResponseis already true. All 723 such dropped entries (2 runs) were enqueued fromHttp2Connection.ProcessWindowUpdateFrameAsync → Http2OutputProducer.TryUpdateStreamWindow → ScheduleResumeFromWindowUpdate → Schedule._unconsumedBytes == 0and_currentStatealready containingCompleted._completedResponse == true.The interleaving this implies (consistent with the code on release/10.0):
_streamWindow <= 0,_unconsumedBytes > 0).WINDOW_UPDATEarrives. Under_dataWriterLock,TryUpdateStreamWindow→UpdateStreamWindowdecides to resume (wasDepleted && _streamWindow > 0 && _unconsumedBytes > 0) and releases the lock.ScheduleResumeFromWindowUpdate()runs afterwards, outside the lock.WriteToOutputPipewrites the remaining bytes withEND_STREAM.ObserveDataAndStateclears_isScheduled, and usuallyCompleteResponseAsyncsets_completedResponse.ScheduleResumeFromWindowUpdatethen callsSchedule(). Its_completedResponsecheck is outside the lock and does not prevent this._isScheduledgoes back totrueand the producer is enqueued again.WriteToOutputPipedequeues that entry, seesCompletedResponse, and drops it (while (TryRead(out producer) && !producer.CompletedResponse)).ObserveDataAndStatenever runs for it, so_isScheduledstaystrue.Http2OutputProducer.StreamResetresets all scheduling state except_isScheduled. When the pooledHttp2Streamserves the next request, everySchedule()returns early and nothing is ever written.Relation to earlier comments
StreamResetandWriteToOutputPipeand identifies theSchedule()call that races the final write: the WINDOW_UPDATE resume.SETTINGS_INITIAL_WINDOW_SIZEgoes through the sameTryUpdateOutputWindow → TryUpdateStreamWindowpath for every open stream. That may explain the original report, where the client sent repeated SETTINGS with increasing window sizes.Workaround (client side)
Why it works: the race needs a response tail blocked on the stream window, followed by a WINDOW_UPDATE resume. If the response fits comfortably in the initial window, no data waits on the window, so no resume is ever scheduled.
Cost: 16 MiB is the default ceiling of SocketsHttpHandler's dynamic stream window (
DOTNET_SYSTEM_NET_HTTP_SOCKETSHTTPHANDLER_FLOWCONTROL_MAXSTREAMWINDOWSIZE). The worst-case per-stream buffering is therefore unchanged; the window only starts at the ceiling instead of growing to it.Limits:
Http2Limits.InitialStreamWindowSizesets Kestrel's receive window (request bodies), so it does not help.Minimal repro
hangs=0.hangs=20(41 s),hangs=21(41 s),hangs=28(51 s) andhangs=14(30 s) in four runs. With the workaround line applied:hangs=0. Each hang is a request that never receives response headers. Kestrel logs nothing.Full diagnostic repro
The full program is in the collapsed section at the end. It is a single file for net10.0 (
Microsoft.NET.Sdk.Web).dotnet run -c Releasehangs > 0; for each hang it prints the producer state, e.g._isScheduled at request start=True … _streamWindow=65535-- --window=16777216hangs=0-- --simulate-fixhangs=0, while still reporting how many requests started with a stale flag-- --probe-- --traceHttp2StreamendedResponse size vs. client stream window
Echo workload, 16 concurrent requests, one connection. "Static" means
System.Net.SocketsHttpHandler.Http2FlowControl.DisableDynamicWindowSizing = true.Suggested fix (for the maintainers to judge)
_isScheduledinHttp2OutputProducer.StreamResettogether with the other scheduling state, so a pooled stream never inherits it.WriteToOutputPipeskips asCompletedResponsecannot leave_isScheduledset. For example: clear it when such an entry is skipped, or have the WINDOW_UPDATE resume re-check_unconsumedBytes/_completedResponseunder_dataWriterLockbefore callingSchedule().Two caveats:
whilewhen it meets a completed producer. That looks harmless today, but it is worth reviewing alongside this change.--simulate-fixclears the flag at request start. That is only a diagnostic, not a proposed patch: 2 of 260 stale starts did not hang, which suggests that producer was still legitimately queued.Environment
95017c711e6a), .NET runtime 10.0.12, SDK 10.0.401.Full diagnostic repro (single file, net10.0, Microsoft.NET.Sdk.Web)
Misiu commentedon Oct 12, 2026
PR #69690 resolves this issue for us. We've verified the fix against .NET 10.0.12, with the test results documented in the PR.
Could someone from the Kestrel team take a look at the PR and advise on the next steps toward getting it reviewed and merged? We'd also appreciate knowing whether the fix could be considered for an upcoming .NET 10 servicing release.
Thanks for your help!
elafarge commentedon Oct 16, 2026
Just chiming in to say that we've been experiencing the issue as well and would love the patch PR to be merged :)
In case that's useful to anyone until the script is merged and released, we've came up with a slightly different workaround, we're using a gRPC middleware to check - via reflexion (only used at startup time) - if the Http2OutputProducer as
_isScheduledset totrueand set it back tofalsewhen a stream is opened.It's gRPC specific but I think a similar "generic" HTTP middleware could be implemented.
One important note if you wish to implement something similar: ensure that there's no reflexion (and ideally no allocation, boxing, etc.) involved in the middleware as it could have a significant performance penalty for EVERY HTTP/2 request.
The reflexion based accessor & patch method for
Http2OutputProducerAnd the gRPC middleware: