Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/protocols/ioxide.http2/Http2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
25 changes: 22 additions & 3 deletions tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private static void AssertLateStart(Func<IDuplexPipe, Calls, Task> 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.
Expand Down Expand Up @@ -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.
/// </summary>
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);
Expand All @@ -304,13 +308,18 @@ 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
{
private readonly Reactor _reactor;
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)
{
Expand All @@ -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<ReadResult> Read(ValueTask<ReadResult> pending)
{
ReadResult read = await pending;
Expand All @@ -354,6 +372,7 @@ public async ValueTask<ReadResult> Read(ValueTask<ReadResult> pending)
public async ValueTask<FlushResult> Flush(ValueTask<FlushResult> pending)
{
FlushResult flushed = await pending;
Volatile.Write(ref _prefaceOut, 1);
if (_viaPool)
{
await LeaveReactor();
Expand All @@ -366,13 +385,13 @@ private sealed class Reader(PipeReader inner, Duplex owner) : PipeReader
{
public override ValueTask<ReadResult> 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);
}

Expand Down
Loading