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
25 changes: 22 additions & 3 deletions src/protocols/ioxide.http3/Http3Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private void DispatchReady(Func<Http3Request, Http3Response> handler)
resp = new Http3Response { Status = 500 };
}

Submit(rs.Request.StreamId, resp);
SubmitAnswer(rs.Request.StreamId, resp);
Recycle(rs);
}
_ready.Clear();
Expand Down Expand Up @@ -726,7 +726,7 @@ private void DispatchReadyAsync(Func<Http3Request, ValueTask<Http3Response>> han
{
RetireBody(rs);
Http3Response response = pending.Result; // once: a ValueTask is consumed by reading it
Submit(sid, response);
SubmitAnswer(sid, response);
StopReadingIfAnsweredEarly(rs);
Recycle(rs);
}
Expand Down Expand Up @@ -757,11 +757,30 @@ private async Task CompleteBufferedAsync(ValueTask<Http3Response> pending, ReqSt
{
return;
}
Submit(rs.Request.StreamId, resp);
SubmitAnswer(rs.Request.StreamId, resp);
StopReadingIfAnsweredEarly(rs);
Recycle(rs);
}

// A null answer has sent nothing, so it gets what a handler that threw gets.
private void SubmitAnswer(long streamId, Http3Response resp)
{
if (resp is null)
{
AnswerNull(streamId);
return;
}
Submit(streamId, resp);
}

// Out of line, as only a handler that answered null comes here.
[MethodImpl(MethodImplOptions.NoInlining)]
private void AnswerNull(long streamId)
{
Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {new NullReferenceException().Message}");
Submit(streamId, new Http3Response { Status = 500 });
}

/// <summary>Back for the next stream once the handler is done and the response submitted.</summary>
private void Recycle(ReqStream rs)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Runtime.CompilerServices;

namespace ioxide.nghttp2;

Expand Down Expand Up @@ -174,7 +175,7 @@ private void DispatchBuffered(Func<Nghttp2Request, ValueTask<Nghttp2Response>> h
{
try
{
SubmitResponse(pending.StreamId, inFlight.Result);
SubmitAnswer(pending.StreamId, inFlight.Result);
}
finally
{
Expand Down Expand Up @@ -210,18 +211,37 @@ private async Task CompleteAsync(ValueTask<Nghttp2Response> inFlight, PendingReq
await new ReactorSwitch(_reactor);
// Nobody can observe this task, so an escaping exception would vanish silently and the
// peer would wait on a stream that is never coming.
Console.Error.WriteLine(
$"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}");

if (!IsBroken)
{
SubmitResponse(pending.StreamId, new Nghttp2Response { Status = 500 });
}
AnswerFault(pending.StreamId, exception);
}
finally
{
Retire(pending);
await FlushEgressAsync();
}
}

// A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch
// around the submit stops the JIT inlining its P/Invoke.
private void SubmitAnswer(int streamId, Nghttp2Response response)
{
if (response is null)
{
AnswerFault(streamId, new NullReferenceException());
return;
}
SubmitResponse(streamId, response);
}

// A handler that threw, or answered null, gets a 500 on its own stream. Out of line, as only a fault comes here.
[MethodImpl(MethodImplOptions.NoInlining)]
private void AnswerFault(int streamId, Exception exception)
{
Console.Error.WriteLine(
$"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}");

if (!IsBroken)
{
SubmitResponse(streamId, new Nghttp2Response { Status = 500 });
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace ioxide.nghttp3;

/// <summary>
Expand Down Expand Up @@ -78,7 +80,7 @@ private void DispatchBufferedSync(Func<Nghttp3Request, Nghttp3Response> handler)
response = new Nghttp3Response { Status = 500 };
}

QueueResponse(request.StreamId, response);
QueueAnswer(request, response);
ReleaseRequest(request); // sync handler returned - the contract just ended
}
_readyStreamIds.Clear();
Expand Down Expand Up @@ -113,7 +115,7 @@ private void DispatchBufferedAsync(Func<Nghttp3Request, ValueTask<Nghttp3Respons
if (pending.IsCompletedSuccessfully)
{
RetireBody(request);
QueueResponse(streamId, pending.Result); // fast path: the handler never awaited
QueueAnswer(request, pending.Result); // fast path: the handler never awaited
RequestHandled(request);
}
else
Expand Down Expand Up @@ -141,13 +143,12 @@ private async Task CompleteBufferedAsync(ValueTask<Nghttp3Response> pending, Ngh
}

await new ReactorSwitch(Reactor); // back on the reactor, wherever the handler finished
long streamId = request.StreamId;
request.HandlerDone = true;
RetireBody(request);

if (_nghttp3Handle != 0 && !_protocolFailed)
{
QueueResponse(streamId, response);
QueueAnswer(request, response);
PumpEgress();
}
StopReadingIfAnsweredEarly(request);
Expand All @@ -158,4 +159,24 @@ private async Task CompleteBufferedAsync(ValueTask<Nghttp3Response> pending, Ngh
ReleaseRequest(request);
}
}

// A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch
// around the submit stops the JIT inlining its P/Invoke.
private void QueueAnswer(Nghttp3Request request, Nghttp3Response response)
{
if (response is null)
{
AnswerNull(request);
return;
}
QueueResponse(request.StreamId, response);
}

// Out of line, as only a handler that answered null comes here.
[MethodImpl(MethodImplOptions.NoInlining)]
private void AnswerNull(Nghttp3Request request)
{
ReportHandlerFault(request, new NullReferenceException());
QueueResponse(request.StreamId, new Nghttp3Response { Status = 500 });
}
}
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.E2E/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private static int Main()
Http3RequestReuseTests.Register(runner);
H3BodyModeTests.Register(runner);
H3BufferedFaultTests.Register(runner);
H3NullAnswerTests.Register(runner);
StreamResetTests.Register(runner);
HandlerFaultHookTests.Register(runner);
H3BodyLimitTests.Register(runner);
Expand Down
Loading
Loading