Repository navigation
Conversation
…ritten to the buffer handed out last, and refuse a hint or an Advance past what a block holds rather than wrap the sum (#617) IBufferWriter has Advance apply to the buffer handed out last, and GetSpan and GetMemory return at least the hint or throw. nghttp3: GetMemory marked its scratch as outstanding and GetSpan left the mark, so GetMemory, GetSpan, Advance copied the scratch over the bytes written into the span. GetSpan now ends an outstanding Memory. All four writers checked room as _staged + hint in int. Beside staged bytes a hint near int.MaxValue wrapped that sum negative, the check passed, and GetSpan or GetMemory handed out the rest of the block instead of throwing. The room check is now done in uint, where two non-negative ints cannot wrap, and growth refuses a block past Array.MaxLength (int.MaxValue for nghttp3's native block) with OutOfMemoryException. Advance in http2, http3 and nghttp3 had the same int sum in its bounds check, so a count near int.MaxValue was accepted and left the staged count negative; it is checked in uint too. Growth moves out of line, so the room check still inlines into GetSpan.
Owner
Author
|
Closed: both cases need call patterns no handler uses (GetMemory then GetSpan with no Advance between them, and size hints near int.MaxValue). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #617.
What was wrong
IBufferWriter<byte>hasAdvanceapply to the buffer handed out last, andGetSpanandGetMemoryreturn at leastsizeHintbytes or throw. The streamed response writers broke both, and a third check had the same flaw as the second:GetMemoryhands out a managed scratch array and marks it outstanding, andAdvancecopies that scratch into the native staging block.GetSpanleft the mark set, soGetMemory(16),GetSpan(3)filled with "abc",Advance(3)staged three bytes of the scratch over "abc". Reproduced at main: the client received[1, 13, 0].EnsureStagingcomputedneeded = _staged + sizeHint. With bytes staged and a hint nearint.MaxValuethe sum wrapped negative, the check passed, andGetSpanorGetMemoryreturned the rest of the block instead of throwing. Reproduced at main with 16 bytes staged and a hint ofint.MaxValue: http2, http3 and nghttp3 handed out 16368 bytes and nghttp2 4080. (nghttp3'sGetMemoryrents its scratch from the pool, which already refused.)_staged + count > capacity).Advance(int.MaxValue)after a span was accepted and left the staged count negative, reproduced in all three. On nghttp3 a flush then handed nghttp3 that count as asize_tlength, which it refused withNGHTTP3_ERR_STREAM_DATA_OVERFLOW, failing the connection (reproduced). From reading, http2 then ends the stream on an empty body, dropping the bytes staged before, and http3 fails the response when its flush rents a negative length.What changes
GetSpanclears the outstanding-Memory mark, soAdvancestages the span.EnsureStagingin all four writers checks room as(uint)_staged + (uint)sizeHint > (uint)capacity: both are non-negative ints, so the uint sum cannot wrap. Growth moves to aNoInliningGrowStaging, which throwsOutOfMemoryExceptionwhen the staged bytes and the hint pass what a block can hold:Array.MaxLengthfor the managed arrays (http2, http3, nghttp2),int.MaxValuefor nghttp3's native block, as no span is longer.Advancein http2, http3 and nghttp3 checks its bound the same way.Left as it is: nghttp2's
Advancehas no bounds check at all (_staged += count). Adding one is new validation on its hot path rather than a fix of a check that exists, so it is not in this PR; its over-advance still surfaces later, from a span slice, asArgumentOutOfRangeException.Behaviour a caller would notice
GetMemory, thenGetSpan, thenAdvancesends what was written into the span.GetSpanorGetMemorywith a hint that, beside what is staged, passes what a block can hold throwsOutOfMemoryException, asArrayBufferWriter<T>does, instead of returning less than the hint. Flushing first and asking again gets a fresh block.Advancewith a count that wrapped the sum throwsInvalidOperationException, as anyAdvancepast the span already did.AdvanceafterGetMemory, with a count past the Memory and a sum pastint.MaxValue, now throwsOutOfMemoryExceptionfrom the growth where it threwArgumentOutOfRangeExceptionfrom slicing the scratch.The hot path (GetSpan, Advance)
GetSpan and Advance cost what they did:
GetSpan(16)/Advance(16)loop on http2, the hot loop at main and on this branch is the same instructions in the same order, condition codes aside.GetSpangains one byte store, the one that clears the Memory mark.EnsureStagingwhole intoGetSpan, growth included (115 to 132 bytes of IL). A first cut of this fix with the guard and its message inline took it to about 200 bytes: it stopped being inlined, and aGetSpanplusAdvancewent from about 0.7 ns to 1.6 ns. With growth in aNoInliningGrowStagingwhat inlines is the check alone, 26 to 35 bytes of IL.Measured with BenchmarkDotNet (in process, one P-core, one writer per process, main and this branch alternated), in ns per
GetSpan(16)plusAdvance(16)on a bare writer, and per 16-byteWritethroughIBufferWriter<byte>:Two more http3 runs of this branch were disturbed by other sessions' load on the box (standard deviations of 0.27 and 0.45 ns, iterations swinging from 0.8 to 3.4 ns within one process) and are left out of the table. Before this I timed a hand-rolled loop, which came out 12% slower on http2, whose loop is instruction for instruction main's, and 17% faster on nghttp3: that is where the loop lands rather than what it costs, and why the table above is BenchmarkDotNet's.
Tests
Each part of the fix was reverted on its own, its test run, and the file restored:
GetSpanleaves the Memory markh3 body: a span asked for after a Memory is what Advance sends(E2E)body content differs at offset 0h3 writer: GetSpan and GetMemory refuse a hint the staging block cannot hold beside what it has staged, rather than hand out less(E2E)expected [GetSpan refused, GetMemory refused], got [GetSpan handed out 16368 bytes, GetMemory refused]GrowStagingwithout its guardAdvancebound summed in inth3 writer: an Advance past the span it was handed is refused, however large its count(E2E)expected [refused], got [advanced]http3 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less(E2E)expected [GetSpan refused, GetMemory refused], got [GetSpan handed out 16368 bytes, GetMemory handed out 16368 bytes]GrowStagingwithout its guardAdvancebound summed in inthttp3 writer: an Advance past the span it was handed is refused, however large its count(E2E)expected [refused], got [advanced]h2 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less(Unit)expected [GetSpan refused, GetMemory refused], got [GetSpan handed out 16368 bytes, GetMemory handed out 16368 bytes]GrowStagingwithout its guardAdvancebound summed in inth2 writer: an Advance past the span it was handed is refused, however large its count(Unit)expected [refused], got [advanced]nghttp2 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less(Unit)expected [GetSpan refused, GetMemory refused], got [GetSpan handed out 4080 bytes, GetMemory handed out 4080 bytes]GrowStagingwithout its guardexpected [GetSpan refused, GetMemory refused], got [GetSpan handed out 8176 bytes, GetMemory handed out 16368 bytes]Each revert failed that one test and nothing else (Unit 265 of 266, the two touched E2E classes 14 of 15). The Memory in the first test is filled with
#before the span is written, so the bytes sent instead of the span are the same whatever the pool's array held.The probes live in the harness (
BufferWriterProbes): each stages 16 bytes, reaches past the block, reports what the writer did, and the test then checks those 16 bytes are still what the response carries. The h2 and nghttp2 tests run in Unit against the in-process strict client; the h3 and http3 ones in E2E against a real QUIC server and the nghttp3 test client.Suites
Built (
ioxide.slnx, Release) and run pinned to the safe CPUs, one suite at a time:tests/Ioxide.Tests.Unit/Program.csregisters the new unit tests.Overlaps
Nghttp3ResponseWriter.cs(the growth size inEnsureStaging,GetMemory's default) and adds tests toHttp3BodyTests.cs. It merges cleanly: its capacity line lands unchanged inGrowStaging, after the new guard, whereneededis still an int no larger thanint.MaxValue, and its tests and the one added here sit in different places. Merged together with http2: an answer waiting on the client's credit has its unread upload credited, so a client that uploads everything first gets it (#588) #604 on a scratch branch, the tree built and Unit (274) and the two touched E2E classes (18) passed.Http2ResponseWriter.cs(CompleteAsync,FlushCore,Send, the newReleaseStaging,Reset), none of the lines here; it merges cleanly.Http3ResponseWriter.FailAsynconly; clean.tests/Ioxide.Tests.Unit/Program.csandtests/Ioxide.Tests.E2E/Program.cssit apart from those of nghttp2: a request says whether a body follows its HEADERS, so a host need not guess from content-length (#581) #594, ngtcp2: an application error code past 2^62 - 1 is refused with ArgumentOutOfRangeException instead of aborting the process (#586) #595, http2: DATA after a request's END_STREAM resets its stream with STREAM_CLOSED instead of reading as more of its body (#589) #599, kestrel: a connection whose reactor stops ends in Kestrel and finishes its dispose, instead of waiting forever on pipe continuations the reactor dropped (#583) #600, http3: a response that ends before its request asks the client to stop sending the rest (#587) #601 and http2: an answer waiting on the client's credit has its unread upload credited, so a client that uploads everything first gets it (#588) #604, and of the branches pushed so far for reactor: after the stop, buffer returns from other threads fill a queue nothing drains, and the one that finds it full spins forever #607, http2: DATA and HEADERS after END_STREAM on a buffered stream, and HEADERS after END_STREAM on a body-reader stream, get no STREAM_CLOSED #615 and nghttp2, nghttp3, http3: a synchronous buffered handler that answers null takes down the whole connection, and on h3 an awaited one is never answered #616 (the first spot chosen collided with fix-615's and was moved).git merge-tree --write-treeagainst every open branch (fix-581 to fix-592) and every pushed branch for reactor: a read after the stop hands out bytes queued before it, as slices of the shared receive slab the stop freed #605 to docs, tests: the h2 streamed-both page text and an early-answer test comment still describe the connection window #593 opened #618 exits 0.GetSpan,GetMemory,Advanceor growth; the one for nghttp2, nghttp3, http3: a synchronous buffered handler that answers null takes down the whole connection, and on h3 an awaited one is never answered #616 changes the nghttp2, nghttp3 and http3 connections' buffered dispatch, in other files. Nothing was pushed yet for nghttp2: the connection's receive window stays at 65,535, so a connection has at most 64 KiB of request body in flight across its streams #612 and nghttp2: a response that ends before its request does lets the client upload the rest, which is credited and dropped #613.For the reviewer
OutOfMemoryException, asArrayBufferWriter<T>throws and as the core's write slab does when it cannot grow that far.ArgumentOutOfRangeExceptionon the hint would be the other reading, but the hint alone can be fine: it is the hint beside what is staged that does not fit, and a flush makes room.GetMemoryhands out its scratch whatever is staged, so a Memory the block cannot take beside what is staged (more than a GiB staged) is refused atAdvance, by the growth, not atGetMemory.Advanceis left as it is (above).src/ioxidechanges.