From 50600a11f7de56eb19b80984fc840bb74038607b Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 20:00:53 +0000 Subject: [PATCH] http2: a RunAsync begun off the reactor reads only once its SETTINGS are out, so a request already in is served on the reactor (#614) A RunAsync begun on the thread pool knows no reactor and learns it from the first read that completes there. Before #593 the loop awaited its SETTINGS flush, which a TCP pipe completes on the reactor, so that first read was made from the reactor. #593 started the preface flush detached, as each pass's flush now is, so the first read was made from the pool, and completed there at once when the client's preface and request were already in: a client need not wait for the server's preface (RFC 9113 3.4). That pass then ran on the pool with no reactor known, so the handler's writes, flushes and body reads ran inline on its own pool thread while the SETTINGS flush resumed on the reactor. Stage, the queue and the pump are reactor-only, so frames were torn: the client's FRAME_SIZE_ERROR and PROTOCOL_ERROR, and echoes that came back with other bytes. A RunAsync begun off the reactor awaits its SETTINGS flush again, as the nghttp2 twin does. Begun on the reactor, the loop still reads on behind that flush. OffReactorH2FixTests: the two "h2 RunAsync begun on the thread pool" tests also check that the connection made no read before its SETTINGS flush completed. With the fix reverted the http2 one fails in 10 of 10 rounds, "the connection read only once its SETTINGS were out (1 reads made before that flush completed)"; the nghttp2 twin passes either way. --- src/protocols/ioxide.http2/Http2Connection.cs | 4 ++- .../Protocols/OffReactorH2FixTests.cs | 25 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index 1a2a338d..7b8022d2 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -193,7 +193,9 @@ private async Task RunCoreAsync(Action dispatch) // already waiting on them. WriteSettings(); CreditConnection(MaxWindow - DefaultWindow); // SETTINGS size streams only; this opens the connection (RFC 9113 6.9.2) - await FlushPassAsync(); + + // Begun off the reactor it waits for this flush, which a TCP pipe completes on the reactor, so its first read is made there and learns it. + await (_reactor is null ? FlushAsync() : FlushPassAsync()); while (!IsBroken) { diff --git a/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs b/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs index 102bc82a..9dd5c712 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs @@ -156,6 +156,7 @@ private static void AssertLateStart(Func serve) AssertEcho(client, port); calls.AssertAllOff(); transport.AssertOnReactor(); + transport.AssertPrefaceFirst(); } // Reads the first chunk, then parks for the next until the teardown wakes it: where that ran is the verdict. @@ -289,12 +290,15 @@ public void AssertAllOn() /// flushes complete on the thread pool, as a default Pipe completes a read, and a flush it held back, /// there. With fromReactorRead a connection counts once a read of its completes on the reactor: begun on /// the pool it writes its SETTINGS from there, before it can know its reactor, and the harness's listen - /// probe, closed by then, never learns it. + /// probe, closed by then, never learns it. Such a connection makes no read until that flush is done, whose + /// completion brings it to the reactor: a read made before, from the pool, completes there at once when the + /// client sent its request with the preface. /// private sealed class Transport(bool viaPool, bool fromReactorRead) { private int _off; private int _faultReads; + private int _readsBeforePreface; // From now on a read that completes throws, on the thread pool, as a pipe whose writer failed does. public void FaultReads() => Volatile.Write(ref _faultReads, 1); @@ -304,6 +308,10 @@ private sealed class Transport(bool viaPool, bool fromReactorRead) public void AssertOnReactor() => Assert.True(Volatile.Read(ref _off) == 0, $"the transport was only called from its reactor ({_off} calls from elsewhere)"); + public void AssertPrefaceFirst() => + Assert.True(Volatile.Read(ref _readsBeforePreface) == 0, + $"the connection read only once its SETTINGS were out ({_readsBeforePreface} reads made before that flush completed)"); + // One connection's transport. private sealed class Duplex : IDuplexPipe { @@ -311,6 +319,7 @@ private sealed class Duplex : IDuplexPipe private readonly Transport _owner; private readonly bool _viaPool; private int _counting; + private int _prefaceOut; public Duplex(IDuplexPipe inner, Reactor reactor, Transport owner, bool viaPool, bool counting) { @@ -333,6 +342,15 @@ public void Note() } } + public void NoteRead() + { + Note(); + if (Volatile.Read(ref _prefaceOut) == 0) + { + Interlocked.Increment(ref _owner._readsBeforePreface); + } + } + public async ValueTask Read(ValueTask pending) { ReadResult read = await pending; @@ -354,6 +372,7 @@ public async ValueTask Read(ValueTask pending) public async ValueTask Flush(ValueTask pending) { FlushResult flushed = await pending; + Volatile.Write(ref _prefaceOut, 1); if (_viaPool) { await LeaveReactor(); @@ -366,13 +385,13 @@ private sealed class Reader(PipeReader inner, Duplex owner) : PipeReader { public override ValueTask ReadAsync(CancellationToken cancellationToken = default) { - owner.Note(); + owner.NoteRead(); return owner.Read(inner.ReadAsync(cancellationToken)); } public override bool TryRead(out ReadResult result) { - owner.Note(); + owner.NoteRead(); return inner.TryRead(out result); }