Skip to content
Closed
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
10 changes: 8 additions & 2 deletions src/ioxide/Reactor/Reactor.RingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,19 @@ private void DrainStoppedOps()
}
}

// At stop, each once the kernel is done with it. A waiter that submits again reuses its slot, past this loop.
// At stop, each once the kernel is done with it. The free slots go first: an op a waiter submits again from its
// completion fails as a handoff after the stop does, where it would otherwise wait in an SQ nothing submits again.
private void CompleteParkedOps()
{
_opFree = [];
_opFreeTop = 0;

int slots = _opTargets.Length;
for (int slot = 0; slot < slots; slot++)
{
if (_opTargets[slot] is { } target && CancelAndWait(Tag(KindClient, 0, slot)))
{
_opTargets[slot] = null;
_opFree[_opFreeTop++] = slot;
target.Complete(-ECANCELED);
}
}
Expand Down Expand Up @@ -271,6 +274,9 @@ private int AllocOpSlot(IRingCompletion completion)
{
if (_opFreeTop == 0)
{
// The stop empties it (CompleteParkedOps); else only a full table gets here, so no submit pays for the test.
ObjectDisposedException.ThrowIf(_opFree.Length == 0, this);

int oldLength = _opTargets.Length;
Array.Resize(ref _opTargets, oldLength * 2);
Array.Resize(ref _opFree, oldLength * 2);
Expand Down
42 changes: 42 additions & 0 deletions tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,48 @@ public static void Register(Runner runner)
Assert.True(finished.Task.Wait(ReturnsWithinMs), "the handler is still parked in its wait, its finally not run");
});

runner.Test("stop: a RingTimer wait a handler starts again from the one the stop ended fails rather than waiting forever", () =>
{
var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var again = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);

(int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (r, conn) =>
{
if (!await IsThisTestsConnection(conn))
{
conn.DecRef();
return;
}

var timer = new RingTimer(r);
try
{
parked.TrySetResult();
await timer.DelayAsync(600_000);

// Inline in the completion the stop gives, as a retry loop would.
int result = await timer.DelayAsync(600_000);
again.TrySetResult($"the second wait returned {result}");
}
catch (Exception e)
{
again.TrySetResult(e.GetType().Name);
}
finally
{
conn.DecRef();
}
}, new ServerConfig { Tcp = new TcpOptions() });

using Socket client = Connect(port);
client.Send("hi"u8);
Assert.True(parked.Task.Wait(5_000), "the handler never parked");

StopAndJoin(reactor, thread);
Assert.True(again.Task.Wait(ReturnsWithinMs), "the handler is still parked in the wait it started again");
Assert.Equal(nameof(ObjectDisposedException), again.Task.Result);
});

runner.Test("stop: a handler parked in a flush its client holds up gets to run its finally", () =>
{
var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down
36 changes: 36 additions & 0 deletions tests/Ioxide.Tests.Http/HttpClientConnectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ public static void Register(Runner runner)
}
});

runner.Test("httpclient h1: a connect its reactor's stop cancels ends with the stop, rather than going on to the next address and waiting forever", () =>
{
using var silent = new TcpListener(IPAddress.Parse("127.0.0.3"), 0);
silent.Start(0);
var silentAt = (IPEndPoint)silent.LocalEndpoint;
Socket[] queued = FillAcceptQueue(silentAt);

try
{
// 127.0.0.2 refuses at once, should the kernel ever be handed the connect to it.
var started = new TaskCompletionSource<Task>(TaskCreationOptions.RunContinuationsAsynchronously);
(_, Reactor reactor, Thread thread) = TestServer.StartConfigured(
(_, connection) => { connection.DecRef(); return Task.CompletedTask; },
new ServerConfig { Tcp = new TcpOptions() },
r => started.SetResult(ClientTransport.ConnectAsync(r, [silentAt.Address, IPAddress.Parse("127.0.0.2")],
(ushort)silentAt.Port, tls: null, serverName: null, connectTimeoutMs: 0).AsTask()));

Task connect = started.Task.Result;
Thread.Sleep(200); // the first connect reaches the kernel, which holds it
Assert.True(!connect.IsCompleted, $"the connect ended before the stop: {connect.Status}");

reactor.Stop();
Assert.True(thread.Join(TimeSpan.FromSeconds(10)), "the reactor did not stop");
Assert.True(((IAsyncResult)connect).AsyncWaitHandle.WaitOne(3_000), "the connect is still waiting 3 s after its reactor stopped");
Assert.True(connect.Exception?.InnerException is ObjectDisposedException,
$"expected ObjectDisposedException, got {connect.Status}: {connect.Exception?.InnerException?.GetType().Name}");
}
finally
{
foreach (Socket socket in queued)
{
socket.Dispose();
}
}
});

runner.Test("httpclient h1: an idle connection past MaxLifetimeMs is replaced by the sweep", () =>
{
int clientPort = 0;
Expand Down
Loading