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
5 changes: 5 additions & 0 deletions src/ioxide/Reactor/Loop/Reactor.Drainers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public void EnqueueReturnQ(ushort bid)
SpinWait sw = default;
while (!_returnQ.TryEnqueue(bid))
{
// Nothing drains the queue past the stop, and there is no ring left to take the buffer.
if (_postsClosed)
{
return;
}
sw.SpinOnce();
}
// Without the wake, a queued return waits for an unrelated CQE; if the ring
Expand Down
5 changes: 5 additions & 0 deletions src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public void EnqueueReturnQIncremental(int fd, ushort gen, ushort bid)
SpinWait sw = default;
while (!_returnQInc!.TryEnqueue(packed))
{
// Nothing drains the queue past the stop, and there is no ring left to take the slice.
if (_postsClosed)
{
return;
}
sw.SpinOnce();
}
Wake();
Expand Down
79 changes: 79 additions & 0 deletions tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,85 @@ public static void Register(Runner runner)
}
}
}, skip: incremental && !TestServer.KernelAtLeast(6, 12));

runner.Test($"stop: handlers holding more buffers than the return queue takes give them all back from another thread after the stop ({mode})", () =>
{
// Past the queue's 16384 entries shared, 65536 incremental. One-byte buffers make each byte a
// recv, so each byte sent is a buffer, or a slice, of its own, and 32768 is the most a ring takes.
int connections = incremental ? 3 : 1;
int perConnection = incremental ? 22_000 : 17_000;
var tcp = new TcpOptions { RecvQueueEntries = 32_768 };
ServerConfig config = incremental
? new ServerConfig { Incremental = new IncrementalOptions { MaxConnections = 8, RecvSlots = 32_768, RecvBufferSize = 1 }, Tcp = tcp }
: new ServerConfig { RecvSlots = 32_768, RecvBufferSize = 1, Tcp = tcp };

using var holding = new CountdownEvent(connections);
using var gaveBack = new CountdownEvent(connections);
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
int returned = 0;

(int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) =>
{
var held = new List<SpscRecvRing.Item>();
try
{
while (held.Count < perConnection)
{
RecvSnapshot snapshot = await conn.ReadAsync();
while (conn.TryGetItem(snapshot, out SpscRecvRing.Item item))
{
if (item.HasBuffer)
{
held.Add(item);
}
}
if (snapshot.IsClosed)
{
return; // the harness's listen probe
}
}

holding.Signal();
await release.Task; // resumes on the thread pool, after the stop
foreach (SpscRecvRing.Item item in held)
{
conn.ReturnBuffer(in item);
Interlocked.Increment(ref returned);
}
gaveBack.Signal();
}
finally
{
conn.DecRef();
}
}, config);

var clients = new List<Socket>();
try
{
for (int c = 0; c < connections; c++)
{
Socket client = Connect(port);
clients.Add(client);
client.Send(new byte[perConnection]);
}
Assert.True(holding.Wait(10_000), $"{holding.CurrentCount} of {connections} handlers never came to hold their buffers");

StopAndJoin(reactor, thread);
release.TrySetResult();

Assert.True(gaveBack.Wait(ReturnsWithinMs),
$"{Volatile.Read(ref returned)} of the {connections * perConnection} buffers went back after the stop, and the next return still spins on a queue nothing drains");
}
finally
{
release.TrySetResult();
foreach (Socket client in clients)
{
client.Dispose();
}
}
}, skip: incremental && !TestServer.KernelAtLeast(6, 12));
}

runner.Test("stop: a flush from another thread after the reactor stopped returns", () =>
Expand Down
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.Unit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private static int Main()
SpillSegmentTests.Register(runner);
RecvQueueExtendTests.Register(runner);
StoppedPostTests.Register(runner);
StoppedReturnTests.Register(runner);
HttpClientGrowthTests.Register(runner);

return runner.Summary();
Expand Down
72 changes: 72 additions & 0 deletions tests/Ioxide.Tests.Unit/StoppedReturnTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Runtime.CompilerServices;
using ioxide;

namespace Ioxide.Tests;

/// <summary>
/// A buffer return from another thread that finds the reactor's return queue full. While the reactor runs, the loop
/// drains the queue and the return waits for room; once it has stopped nothing drains it, and the return must not wait.
/// </summary>
/// <remarks>
/// The real returns, on a reactor that never runs, with this test setting the flag the end of Teardown sets.
/// </remarks>
internal static class StoppedReturnTests
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_postsClosed")]
private static extern ref bool PostsClosedOf(Reactor reactor);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "InitIncremental")]
private static extern void InitIncremental(Reactor reactor);

public static void Register(Runner runner)
{
foreach (bool incremental in new[] { false, true })
{
string mode = incremental ? "incremental" : "shared ring";

runner.Test($"reactor: a buffer return that finds its queue full waits while the reactor runs, and leaves when it stops ({mode})", () =>
{
Reactor reactor = NeverRunning(incremental);
int capacity = incremental ? 1 << 16 : 1 << 14;
for (int i = 0; i < capacity; i++)
{
Return(reactor, incremental);
}

var waiting = new Thread(() => Return(reactor, incremental)) { IsBackground = true, Name = "full-queue-return" };
waiting.Start();
Assert.True(!waiting.Join(200), "a return that found the queue full while the reactor runs was dropped instead of waiting for room");

Volatile.Write(ref PostsClosedOf(reactor), true);
Assert.True(waiting.Join(3_000), "a return waiting on the full queue still spins after the stop, where nothing will drain it");
});
}
}

// Every caller is another thread, and the wake finds no eventfd to write.
private static Reactor NeverRunning(bool incremental)
{
var reactor = new Reactor(0, new ServerConfig
{
ReactorCount = 1, Tcp = null, Udp = null, Incremental = incremental ? new IncrementalOptions() : null,
});
if (incremental)
{
InitIncremental(reactor);
}

return reactor;
}

private static void Return(Reactor reactor, bool incremental)
{
if (incremental)
{
reactor.EnqueueReturnQIncremental(3, 0, 0);
}
else
{
reactor.EnqueueReturnQ(0);
}
}
}
Loading