diff --git a/src/clients/ioxide.file/AssetReader.cs b/src/clients/ioxide.file/AssetReader.cs index 600b43af..8627e1a7 100644 --- a/src/clients/ioxide.file/AssetReader.cs +++ b/src/clients/ioxide.file/AssetReader.cs @@ -41,7 +41,8 @@ public ValueTask ReadAsync(int fd, long offset) /// /// Build a per-reactor reader pool and register it as a service /// (GetService<RingPool<AssetReader>>()). bounds - /// concurrent reads; size for the largest asset. + /// concurrent reads; size for the largest asset. The reactor's stop + /// disposes the pool, and with it each reader once it is back. /// public static RingPool CreatePool(Reactor reactor, int readers, int bufferBytes) { @@ -52,6 +53,7 @@ public static RingPool CreatePool(Reactor reactor, int readers, int } reactor.AddService(pool); + reactor.AddStopper(pool.Dispose); return pool; } diff --git a/src/clients/ioxide.httpclient/Http1/HttpClientPool.cs b/src/clients/ioxide.httpclient/Http1/HttpClientPool.cs index b4d801b8..329e9c81 100644 --- a/src/clients/ioxide.httpclient/Http1/HttpClientPool.cs +++ b/src/clients/ioxide.httpclient/Http1/HttpClientPool.cs @@ -62,6 +62,7 @@ public static HttpClientPool Start(Reactor reactor, HttpClientOptions options) } reactor.AddService(pool); reactor.AddTicker(pool.Sweep); + reactor.AddStopper(pool.Dispose); return pool; } @@ -145,8 +146,7 @@ private async ValueTask AcquireAsync() { if (_disposed) { - throw new HttpClientException( - $"pool for {_options.Host}:{_options.Port} is disposed"); + throw new ObjectDisposedException(nameof(HttpClientPool), $"pool for {_options.Host}:{_options.Port} is disposed"); } // Newest first: a recently used connection is the one most likely still warm at the @@ -332,8 +332,10 @@ private string AcquireTimeoutMessage() /// /// Close the idle connections and stop replenishing. Connections currently checked out by an /// in-flight request are not touched - the request owns them, and they are dropped by - /// when it finishes, because a disposed pool takes nothing back. Called - /// from another thread, it is queued to the reactor and returns. + /// when it finishes, because a disposed pool takes nothing back. The requests + /// waiting for a connection, and any made after, fail with . + /// Called from another thread, it is queued to the reactor and returns. A pool from + /// is disposed by its reactor's stop. /// public void Dispose() { diff --git a/src/clients/ioxide.httpclient/Http1/RingHttpClient.cs b/src/clients/ioxide.httpclient/Http1/RingHttpClient.cs index 03a0fcd9..25c759a0 100644 --- a/src/clients/ioxide.httpclient/Http1/RingHttpClient.cs +++ b/src/clients/ioxide.httpclient/Http1/RingHttpClient.cs @@ -41,6 +41,7 @@ public static RingHttpClient Start(Reactor reactor, RingHttpClientOptions? optio var client = new RingHttpClient(reactor, options ?? new RingHttpClientOptions()); reactor.AddService(client); reactor.AddTicker(client.Sweep); + reactor.AddStopper(client.Dispose); return client; } @@ -119,10 +120,7 @@ private static bool TryGetLocation(HttpClientResponse response, Uri current, [No private HttpClientPool PoolFor(Uri url) { - if (_disposed) - { - throw new HttpClientException("client is disposed"); - } + ObjectDisposedException.ThrowIf(_disposed, this); // An IPv6 address goes to the pool bare; a name in its DNS form. string host = url.HostNameType == UriHostNameType.IPv6 ? url.DnsSafeHost : url.IdnHost; @@ -160,8 +158,9 @@ private void Sweep() } } - /// Close every origin's idle connections; requests in flight finish first, as with the pool. - /// Called from another thread, it is queued to the reactor and returns. + /// Close every origin's idle connections and free the TLS context; requests in flight finish + /// first, and those waiting fail with , as with the pool. Called + /// from another thread, it is queued to the reactor and returns. Its reactor's stop disposes it. public void Dispose() { // The origins and their pools are the reactor's. diff --git a/src/clients/ioxide.httpclient/Tls/TlsClientContext.cs b/src/clients/ioxide.httpclient/Tls/TlsClientContext.cs index c3f3eafe..2f6e497a 100644 --- a/src/clients/ioxide.httpclient/Tls/TlsClientContext.cs +++ b/src/clients/ioxide.httpclient/Tls/TlsClientContext.cs @@ -28,6 +28,8 @@ public sealed unsafe class TlsClientContext : IDisposable internal nint Handle => _ctx; + internal bool IsDisposed => _disposed; + internal TlsClientOptions Options { get; } private TlsClientContext(nint ctx, TlsClientOptions options) diff --git a/src/clients/ioxide.httpclient/Tls/TlsClientStream.cs b/src/clients/ioxide.httpclient/Tls/TlsClientStream.cs index 9c257031..1ff8d606 100644 --- a/src/clients/ioxide.httpclient/Tls/TlsClientStream.cs +++ b/src/clients/ioxide.httpclient/Tls/TlsClientStream.cs @@ -63,6 +63,12 @@ internal static async ValueTask HandshakeAsync(RingSocket socke // Everything from here to the constructor is unowned: the stream that would free it does // not exist yet, and ClientTransport hands the socket over on the understanding that the // stream closes it on failure. So each step that can throw unwinds explicitly. + if (context.IsDisposed) + { + socket.Dispose(); // a RingHttpClient its reactor's stop disposed freed the context while this connect ran + throw new ObjectDisposedException(nameof(TlsClientContext)); + } + nint ssl = BoringSsl.SSL_new(context.Handle); if (ssl == 0) { diff --git a/src/clients/ioxide.pg/PgConnection.cs b/src/clients/ioxide.pg/PgConnection.cs index 1b6b4157..9b0f6df6 100644 --- a/src/clients/ioxide.pg/PgConnection.cs +++ b/src/clients/ioxide.pg/PgConnection.cs @@ -70,14 +70,15 @@ public static async Task ConnectAsync(IRingHost host, PgOptions op RingSocket socket = RingSocket.CreateTcp(host); var connection = new PgConnection(host, socket, options.MaxReceiveBytes); var deadline = new OpenDeadline(socket); - if (options.CommandTimeoutMs > 0) - { - host.SubmitTimeout(options.CommandTimeoutMs * 1_000_000L, deadline); - } - bool connected = false; try { + // Inside the try: refused at a stop, it must still free the socket and the buffers. + if (options.CommandTimeoutMs > 0) + { + host.SubmitTimeout(options.CommandTimeoutMs * 1_000_000L, deadline); + } + int rc = await socket.ConnectAsync(options.Host, options.Port); if (rc < 0) { diff --git a/src/clients/ioxide.pg/PgPool.cs b/src/clients/ioxide.pg/PgPool.cs index e3600c1d..b5690e38 100644 --- a/src/clients/ioxide.pg/PgPool.cs +++ b/src/clients/ioxide.pg/PgPool.cs @@ -10,7 +10,7 @@ namespace ioxide.pg; /// Safe to call from any thread. The pool belongs to its reactor and takes no lock: a query made on /// another thread moves to the reactor first, and completes there. /// -public sealed class PgPool +public sealed class PgPool : IDisposable { // Bound the slow-path wait so a pool that can't reach Postgres fails fast with a clear error // instead of parking queries forever; each attempt drives one coalesced connect try. @@ -22,6 +22,7 @@ public sealed class PgPool private int _next; private int _opening; // in-flight connect attempts (coalesces opens) private long _reopenAtMs; // jittered backoff gate for background replenishment + private bool _disposed; // Slow-path queries waiting for an open attempt to finish, one completion each: a task awaited by several // resumes only the first inline and sends the rest through the thread pool. @@ -43,6 +44,7 @@ public static PgPool Start(Reactor reactor, PgOptions options) } reactor.AddService(pool); reactor.AddTicker(pool.Sweep); // per-command timeout sweep + background replenishment + reactor.AddStopper(pool.Dispose); return pool; } @@ -58,6 +60,11 @@ private async Task OpenOneAsync() try { PgConnection connection = await PgConnection.ConnectAsync(_host, _options); + if (_disposed) + { + connection.Dispose(); + return; + } _connections.Add(connection); _reopenAtMs = 0; // healthy - allow immediate replenishment } @@ -69,15 +76,20 @@ private async Task OpenOneAsync() finally { _opening--; - // Wake every slow-path waiter whether the attempt succeeded or failed, so a run of - // failures surfaces as a fast error instead of a permanent hang. All are taken out - // before any is woken, so one that waits again waits for the next attempt. - TaskCompletionSource[] waiters = [.. _openWaiters]; - _openWaiters.Clear(); - foreach (TaskCompletionSource waiter in waiters) - { - waiter.TrySetResult(); - } + // Whether the attempt succeeded or failed, so a run of failures surfaces as a fast error + // instead of a permanent hang. + WakeOpenWaiters(); + } + } + + // All are taken out before any is woken, so one that waits again waits for the next attempt. + private void WakeOpenWaiters() + { + TaskCompletionSource[] waiters = [.. _openWaiters]; + _openWaiters.Clear(); + foreach (TaskCompletionSource waiter in waiters) + { + waiter.TrySetResult(); } } @@ -157,6 +169,7 @@ private async ValueTask WaitForConnectionAsync() await new ReactorSwitch(_host); for (int attempt = 1; ; attempt++) { + ObjectDisposedException.ThrowIf(_disposed, this); PgConnection? c = Pick(); if (c != null) { @@ -183,6 +196,11 @@ private async ValueTask WaitForConnectionAsync() // Reactor-thread ticker (~250 ms): time out stuck connections and replenish toward PoolSize. private void Sweep() { + if (_disposed) + { + return; // AddTicker has no removal + } + long now = Native.MonotonicMs; int timeoutMs = _options.CommandTimeoutMs; @@ -210,4 +228,28 @@ private void Sweep() } private static int BackoffMs() => 200 + Random.Shared.Next(400); // jittered ~200-600 ms + + /// + /// Close the connections and stop replenishing: queries in flight fail with the connection, and those + /// waiting for one, and any made after, with . Called from another + /// thread, it is queued to the reactor and returns. A pool from is disposed by its + /// reactor's stop. + /// + public void Dispose() + { + // The connections and the waiters are the reactor's. + if (!_host.OnReactorThread) + { + _host.ScheduleOnReactor(static pool => ((PgPool)pool!).Dispose(), this); + return; + } + + _disposed = true; + foreach (PgConnection connection in _connections) + { + connection.Dispose(); + } + _connections.Clear(); + WakeOpenWaiters(); // each fails at the disposed check + } } diff --git a/src/clients/ioxide.redis.stackexchange/Transport/IoxideTransport.Connect.cs b/src/clients/ioxide.redis.stackexchange/Transport/IoxideTransport.Connect.cs index d263e278..78a9b18b 100644 --- a/src/clients/ioxide.redis.stackexchange/Transport/IoxideTransport.Connect.cs +++ b/src/clients/ioxide.redis.stackexchange/Transport/IoxideTransport.Connect.cs @@ -15,7 +15,17 @@ internal static async ValueTask ConnectAsync( foreach (IPAddress address in addresses) { RingSocket ringSocket = RingSocket.CreateTcp(reactor, address.AddressFamily); - Task connect = ringSocket.ConnectAsync(address, (ushort)port).AsTask(); + Task connect; + try + { + connect = ringSocket.ConnectAsync(address, (ushort)port).AsTask(); + } + catch + { + ringSocket.Dispose(); // refused by a stopped reactor, which a reconnect keeps trying + throw; + } + int result; try { diff --git a/src/ioxide/Client/RingPool.cs b/src/ioxide/Client/RingPool.cs index 848bee5b..9788d185 100644 --- a/src/ioxide/Client/RingPool.cs +++ b/src/ioxide/Client/RingPool.cs @@ -7,11 +7,12 @@ namespace ioxide; /// property holds under contention. A return from another thread sends the renter back to the /// reactor it rented on. /// -public sealed class RingPool where T : class +public sealed class RingPool : IDisposable where T : class { private readonly Stack _idle = new(); private readonly Queue> _waiters = new(); private readonly object _gate = new(); + private bool _disposed; // under _gate /// /// Rent a resource, waiting (without blocking a thread) if none is idle. @@ -21,6 +22,7 @@ public ValueTask RentAsync() TaskCompletionSource waiter; lock (_gate) { + ObjectDisposedException.ThrowIf(_disposed, this); if (_idle.TryPop(out T? item)) { return new ValueTask(item); @@ -45,16 +47,49 @@ private static async ValueTask WaitAsync(Task waiting, Reactor? reactor) /// public void Return(T item) { - TaskCompletionSource? waiter; + TaskCompletionSource? waiter = null; lock (_gate) { - if (!_waiters.TryDequeue(out waiter)) + if (!_disposed && !_waiters.TryDequeue(out waiter)) { _idle.Push(item); return; } } + if (waiter is null) + { + (item as IDisposable)?.Dispose(); // a disposed pool keeps nothing + return; + } + waiter.SetResult(item); // outside the lock - runs the renter's continuation } + + /// + /// Dispose the idle resources, and each one returned from now on; the renters waiting, and any + /// rent made after, fail with . + /// + public void Dispose() + { + T[] idle; + TaskCompletionSource[] waiters; + lock (_gate) + { + _disposed = true; + idle = [.. _idle]; + _idle.Clear(); + waiters = [.. _waiters]; + _waiters.Clear(); + } + + foreach (TaskCompletionSource waiter in waiters) + { + waiter.SetException(new ObjectDisposedException(GetType().Name)); + } + foreach (T item in idle) + { + (item as IDisposable)?.Dispose(); + } + } } diff --git a/src/ioxide/Connection/Tcp/TcpConnection.FileRead.cs b/src/ioxide/Connection/Tcp/TcpConnection.FileRead.cs index 6f34841e..9be8cdfe 100644 --- a/src/ioxide/Connection/Tcp/TcpConnection.FileRead.cs +++ b/src/ioxide/Connection/Tcp/TcpConnection.FileRead.cs @@ -46,6 +46,7 @@ public sealed unsafe partial class TcpConnection /// looks for -EPIPE. /// /// A flush is in flight, or another file read is. + /// The reactor is stopping. /// /// is negative, or so large that the write position plus /// passes . diff --git a/src/ioxide/IRingHost.cs b/src/ioxide/IRingHost.cs index b495f0d1..42a72a52 100644 --- a/src/ioxide/IRingHost.cs +++ b/src/ioxide/IRingHost.cs @@ -8,7 +8,8 @@ namespace ioxide; /// /// Buffers cross as because pointers can't survive an await. Safe from any /// thread (off-reactor callers are marshalled). Results: bytes transferred (0 for connect), or a -/// negative errno. +/// negative errno. From the reactor's stop on, a submission throws , +/// and the stop cancels each one it already had. /// public interface IRingHost { diff --git a/src/ioxide/Native/Native.IoUring.cs b/src/ioxide/Native/Native.IoUring.cs index 82faefab..75a67ba8 100644 --- a/src/ioxide/Native/Native.IoUring.cs +++ b/src/ioxide/Native/Native.IoUring.cs @@ -48,7 +48,6 @@ public static unsafe partial class Native { public const int IORING_CQE_BUFFER_SHIFT = 16; public const uint IORING_REGISTER_PBUF_RING = 22; public const uint IORING_UNREGISTER_PBUF_RING = 23; - public const uint IORING_REGISTER_SYNC_CANCEL = 24; public const uint IORING_REGISTER_RING_FDS = 20; public const uint IORING_UNREGISTER_RING_FDS = 21; public const uint IORING_POLL_ADD_MULTI = 1u << 0; @@ -201,16 +200,6 @@ public struct io_uring_rsrc_update { public ulong data; } - // Argument struct for IORING_REGISTER_SYNC_CANCEL; the rest stays zero. - [StructLayout(LayoutKind.Sequential)] - public struct io_uring_sync_cancel_reg { - public ulong addr; - public int fd; - public uint flags; - public __kernel_timespec timeout; - public ulong pad0, pad1, pad2, pad3; - } - // include/uapi/linux/time_types.h - the timespec IORING_OP_TIMEOUT reads. [StructLayout(LayoutKind.Sequential)] public struct __kernel_timespec { public long tv_sec; public long tv_nsec; } diff --git a/src/ioxide/Reactor/Loop/Reactor.Drainers.cs b/src/ioxide/Reactor/Loop/Reactor.Drainers.cs index 1c75a425..1cdfa833 100644 --- a/src/ioxide/Reactor/Loop/Reactor.Drainers.cs +++ b/src/ioxide/Reactor/Loop/Reactor.Drainers.cs @@ -82,6 +82,11 @@ public void EnqueueReturnQ(ushort bid) SpinWait sw = default; while (!_returnQ.TryEnqueue(bid)) { + // Nothing drains the queue past the stop. + if (_postsClosed) + { + return; + } sw.SpinOnce(); } // Without the wake, a queued return waits for an unrelated CQE; if the ring @@ -152,12 +157,8 @@ private void Recycle(TcpConnection conn, int fd) } close(fd); conn.Clear(); - ReturnToPool(conn); - } - // A cleared connection: kept for the next accept, or its memory freed right here once the pool is full. - private void ReturnToPool(TcpConnection conn) - { + // Kept for the next accept, or its memory freed right here once the pool is full. if (_pool.Count < _poolMax) { _pool.Push(conn); @@ -166,6 +167,7 @@ private void ReturnToPool(TcpConnection conn) { conn.Dispose(); } + _tcpLive--; } #endregion diff --git a/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs b/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs index 2e15c390..08477e65 100644 --- a/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs +++ b/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs @@ -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. + if (_postsClosed) + { + return; + } sw.SpinOnce(); } Wake(); diff --git a/src/ioxide/Reactor/Loop/Reactor.Loop.cs b/src/ioxide/Reactor/Loop/Reactor.Loop.cs index a8d17d97..eb3bec4d 100644 --- a/src/ioxide/Reactor/Loop/Reactor.Loop.cs +++ b/src/ioxide/Reactor/Loop/Reactor.Loop.cs @@ -5,7 +5,8 @@ public sealed unsafe partial class Reactor { private void Loop() { - while (!_stopRequested) + long stopDeadlineMs = 0; + while (true) { ClearWakePending(); if (_incremental) DrainReturnQIncremental(); @@ -17,6 +18,12 @@ private void Loop() RearmStarvedRecvs(); QuicFireDueTimers(); + // After the drains, so a stop whose last connection one of them let go of ends without a wait. + if (_stopRequested && Stopped(ref stopDeadlineMs)) + { + return; + } + // The transient three, which the loop carries on from: a signal, the kernel short of // resources, and overflow entries it could not flush - the CQ drain below is what EBUSY // wants anyway. Until #220 this could never match (the wrapper returned -1 for every diff --git a/src/ioxide/Reactor/Reactor.RingHost.cs b/src/ioxide/Reactor/Reactor.RingHost.cs index a537f70c..06e2770d 100644 --- a/src/ioxide/Reactor/Reactor.RingHost.cs +++ b/src/ioxide/Reactor/Reactor.RingHost.cs @@ -53,6 +53,17 @@ public T GetService() where T : class internal void OwnTlsService(TlsService service) => (_tlsServices ??= []).Add(service); + // One per client started on this reactor, several of one type included: the stop runs them first, once. + private List? _stoppers; + + /// + /// Register a callback the reactor runs once on its thread when it stops, before it closes its + /// listeners and connections. Call from OnStart. A client started on the reactor disposes + /// itself there: it closes its idle connections, fails the requests waiting on it and frees what + /// it holds, while the reactor can still run what that wakes. + /// + public void AddStopper(Action stopper) => (_stoppers ??= []).Add(stopper); + /// /// Runs on the reactor thread before the loop starts - open ring-native clients here. /// @@ -66,8 +77,11 @@ public T GetService() where T : class /// /// Raised on the reactor's own thread when it is ending because of a fault rather than a /// - a kernel that refuses to create the ring included. It runs before the - /// teardown: the listeners, connections and ring are closed only once it returns. Handle it to - /// log, restart, or bring the process down deliberately. + /// teardown, which drains nothing: once it returns, the clients started on the reactor stop, its + /// listeners close and its connections are aborted, and the ring and what nothing holds are freed. + /// A connection still open stays allocated until the process exits, its descriptor and the + /// receive memory it can reach included, since no completion will let it go. Handle it to log, + /// restart, or bring the process down deliberately. /// /// /// Without a handler the exception propagates out of , which on a bare @@ -124,6 +138,7 @@ public void SubmitTimeout(long nanoseconds, IRingCompletion completion) return; } + ObjectDisposedException.ThrowIf(_stopRequested, this); SubmitTimeoutCore(nanoseconds, completion); } @@ -154,6 +169,7 @@ private void SubmitClientOp(byte opcode, int fd, nint buffer, int length, long o return; } + ObjectDisposedException.ThrowIf(_stopRequested, this); SubmitClientOpCore(opcode, fd, buffer, length, offset, completion); } @@ -165,8 +181,8 @@ private void SubmitClientOp(byte opcode, int fd, nint buffer, int length, long o [MethodImpl(MethodImplOptions.NoInlining)] private void HandOff(byte opcode, int fd, nint buffer, int length, long offset, IRingCompletion completion) { - // Nothing submits it after a stop. Throwing, as a ReactorSwitch does, also ends a loop that ignores an error result. - ObjectDisposedException.ThrowIf(_postsClosed, this); + // From the stop on nothing is submitted. Throwing, as a ReactorSwitch does, also ends a loop that ignores an error result. + ObjectDisposedException.ThrowIf(_stopRequested, this); _remoteOps.Enqueue(new RemoteOp(opcode, fd, buffer, length, offset, completion)); Wake(); @@ -191,17 +207,14 @@ private void DrainStoppedOps() } } - // At stop, each once the kernel is done with it. A waiter that submits again reuses its slot, past this loop. - private void CompleteParkedOps() + // The stop's: each op still in the table ends through its own completion, as a cancelled one does while running. + private void CancelClientOps() { - int slots = _opTargets.Length; - for (int slot = 0; slot < slots; slot++) + for (int slot = 0; slot < _opTargets.Length; slot++) { - if (_opTargets[slot] is { } target && CancelAndWait(Tag(KindClient, 0, slot))) + if (_opTargets[slot] != null) { - _opTargets[slot] = null; - _opFree[_opFreeTop++] = slot; - target.Complete(-ECANCELED); + SubmitCancel(Tag(KindClient, 0, slot)); } } } @@ -234,9 +247,14 @@ private void DrainRemoteOps() { while (_remoteOps.TryDequeue(out RemoteOp op)) { + // Handed over before the stop and taken after it: submitted now, it could miss the stop's one cancel. + if (_stopRequested) + { + op.Completion.Complete(-ECANCELED); + } // The only place the two submissions have to be told apart, and off the hot path: // once per cross-thread handover, not once per op. - if (op.Opcode == IORING_OP_TIMEOUT) + else if (op.Opcode == IORING_OP_TIMEOUT) { SubmitTimeoutCore(op.Offset, op.Completion); } diff --git a/src/ioxide/Reactor/Reactor.Runner.cs b/src/ioxide/Reactor/Reactor.Runner.cs index 400ef75c..34218b77 100644 --- a/src/ioxide/Reactor/Reactor.Runner.cs +++ b/src/ioxide/Reactor/Reactor.Runner.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ioxide.tls; using static ioxide.Native; @@ -121,23 +122,63 @@ private void AnnounceListening() { } address => address.ToString(), }; - // Still on the reactor thread, in dependency order: sockets close while the ring is alive - // (in-flight ops surface as errors/cancels and are dropped), the ring fd goes next, and native - // memory the kernel could reference is freed only after that. - private void Teardown(bool startFailed) + // How long a stop waits for its handlers to let go of their connections and for its ring ops to end. + private const int StopDrainMs = 1_000; + + // The stop's first pass closes everything; the passes after it run as usual until nothing is held or the deadline. + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Stopped(ref long deadlineMs) { - // A faulted loop never set it: UdpSendTo sends directly from here, as the ring may never be submitted again. - _stopRequested = true; + if (deadlineMs == 0) + { + deadlineMs = Native.MonotonicMs + StopDrainMs; + // Left queued past the closes, an entry would reach whatever takes its socket's number; a failed prep ends a submit early. + while (_ring.SubmitAndWait(0) > 0) + { + } + CloseAll(); + DrainPostQ(); // what the stoppers and the closes woke: posted here, past the pass's own drain + CancelClientOps(); + } - // Every connection closed to its handler first, then the queue sent while each socket it names is still its - // own: left queued, a later submit would send it to whatever took the number (#531). - MarkAcceptedTcpConnectionsClosed(); - bool submitted = !startFailed && SubmitQueued(); + return (_tcpLive == 0 && _opFreeTop == _opTargets.Length) || Native.MonotonicMs >= deadlineMs; + } + // The clients stop, then every socket closes, ending every read, write and wait on one; Teardown runs it for a + // loop that faulted. + private void CloseAll() + { + RunStoppers(); CloseTcpListeners(); + CloseTcpConnections(); TeardownQuic(); CloseUdpFds(); - CloseAcceptedTcpSockets(); + } + + // Once, though CloseAll runs again in Teardown; one that throws must not leave the others unrun. + private void RunStoppers() + { + List? stoppers = _stoppers; + _stoppers = null; + foreach (Action stopper in stoppers ?? []) + { + try + { + stopper(); + } + catch (Exception e) + { + Console.Error.WriteLine($"[r{_id}] stopper faulted: {e.Message}"); + } + } + } + + // Everything the reactor owns is freed here, on its thread, but what a handler still holds and what it can reach. + private void Teardown(bool startFailed) + { + // A faulted loop never set it: UdpSendTo sends directly from here, as the ring may never be submitted again. + _stopRequested = true; + CloseAll(); // Taken away before it is closed, and its writers waited out: closing under one would // free the number and let that writer's 8 bytes land in whatever socket took it next. @@ -153,15 +194,6 @@ private void Teardown(bool startFailed) close(wakeFd); } - // While the ring is still mapped; what a waiter submits again can't name a socket this stop closed. With the SQ - // not emptied nothing is cancelled: the kernel may still take what is left there. - ReleaseAcceptedTcpConnections(canCancel: submitted); - if (submitted) - { - CompleteParkedOps(); - } - ReturnReleasedTcpConnections(); - if (_opTimespecs != null) { NativeMemory.Free(_opTimespecs); @@ -186,23 +218,25 @@ private void Teardown(bool startFailed) _ring.Dispose(); } - // Pooled connections are past their last send (#250) and their incremental ring is unregistered, - // so their slabs can go, the ones the stop returned included. One a handler still holds stays (#223). + // Pooled connections are past their last send (#250) and their incremental ring is unregistered. while (_pool.TryPop(out TcpConnection? pooled)) { pooled.Dispose(); } - // Shared provided-buffer ring (incremental mode allocates per connection instead). - if (_bufRing != null) + // What a connection still held has queued, and what its reader took, points into the shared slab. + if (_tcpLive == 0) { - NativeMemory.AlignedFree(_bufRing); - _bufRing = null; - } - if (_bufSlab != null) - { - NativeMemory.AlignedFree(_bufSlab); - _bufSlab = null; + if (_bufRing != null) + { + NativeMemory.AlignedFree(_bufRing); + _bufRing = null; + } + if (_bufSlab != null) + { + NativeMemory.AlignedFree(_bufSlab); + _bufSlab = null; + } } FreeUdpMemory(); @@ -218,32 +252,6 @@ private void Teardown(bool startFailed) } } - // True once the kernel has taken everything queued, a few tries allowed: a cancel can't find what never reached it. - private bool SubmitQueued() - { - for (int attempt = 0; attempt < 16 && _ring.SqPending > 0; attempt++) - { - _ring.SubmitAndWait(0); - } - - return _ring.SqPending == 0; - } - - // True once the kernel is done with the request this tag names: cancelled, finished, or never submitted. A - // request running in an io-wq worker is waited for, 1 s at most. - private bool CancelAndWait(ulong userData) - { - var reg = new io_uring_sync_cancel_reg - { - addr = userData, - fd = -1, - timeout = new __kernel_timespec { tv_sec = 1 }, - }; - int result = io_uring_register(_ring.Fd, IORING_REGISTER_SYNC_CANCEL, ®, 1); - - return result == 0 || result == -ENOENT; - } - // Both are no-ops unless that ring was registered: incremental mode leaves _bufRing null (one // ring per connection instead), and a reactor with no datagram transport leaves _udpBufRing null. private void UnregisterSharedBufRings() @@ -261,15 +269,22 @@ private void UnregisterSharedBufRings() } } - // Set cross-thread by Stop(); the loop checks it at the top of each iteration and exits, after which - // Run() tears the ring down on this (the reactor) thread - mandatory for a single-issuer ring. + // Set cross-thread by Stop(); the loop checks it once a pass, and the teardown that follows the loop runs on this + // (the reactor) thread - mandatory for a single-issuer ring. private volatile bool _stopRequested; /// - /// Requests the reactor to stop. Safe to call from any thread. The loop finishes its current - /// iteration and exits, then closes the listeners and wake fd and disposes the - /// io_uring ring on the reactor thread (a single-issuer / DEFER_TASKRUN ring must be torn down on the - /// thread that owns it). Join the reactor thread after calling this to await teardown. + /// Requests the reactor to stop. Safe to call from any thread. From then on a ring op submitted on + /// the reactor throws . On its next pass the reactor runs the + /// stoppers its clients registered (), closes its listeners, aborts every + /// connection, so its peer gets a reset and nothing still queued is sent (a kTLS connection is shut + /// down instead, and what it queued may still go out), and cancels every ring op + /// once, so every handler's reads, writes and waits end. The loop then runs on, posts and switches + /// included, until every connection has been let go and every ring op has completed, or about a + /// second has passed, and frees all the reactor owns on its thread. A connection a + /// handler still holds by then stays allocated, its descriptor and the receive memory it can reach + /// included, and what it calls on the reactor after that fails or reports the connection closed. + /// Join the reactor thread after calling this to await the teardown. /// public void Stop() { diff --git a/src/ioxide/Reactor/Reactor.cs b/src/ioxide/Reactor/Reactor.cs index 37b93bea..5a6c3633 100644 --- a/src/ioxide/Reactor/Reactor.cs +++ b/src/ioxide/Reactor/Reactor.cs @@ -78,6 +78,9 @@ private static ulong Tag(byte kind, ushort gen, int fd) private readonly int _poolMax; private readonly Stack _pool; + // Accepted and not yet recycled, in the table or not: a stop runs on until none is left. + private int _tcpLive; + // Every TcpConnection life on this reactor takes the next value, so two lives never share a generation (#416). private int _tcpGeneration; @@ -98,7 +101,6 @@ private static ulong Tag(byte kind, ushort gen, int fd) private const int EAGAIN = 11; private const int EBUSY = 16; private const int ETIME = 62; - private const int ENOENT = 2; public Reactor(int id, ServerConfig config) { diff --git a/src/ioxide/Reactor/Transport/Tcp/Reactor.Tcp.cs b/src/ioxide/Reactor/Transport/Tcp/Reactor.Tcp.cs index cf94b53d..ec79ab6d 100644 --- a/src/ioxide/Reactor/Transport/Tcp/Reactor.Tcp.cs +++ b/src/ioxide/Reactor/Transport/Tcp/Reactor.Tcp.cs @@ -218,6 +218,16 @@ private void OnTcpRecvCompletionIncremental(int fd, ushort gen, int res, uint fl // for the reactor's lifetime, so it predicts perfectly. private void OnTcpAcceptCompletion(int listenFd, int res, bool more) { + // From the stop on nothing is accepted, nor a listener armed again: the stop closed them. + if (_stopRequested) + { + if (res >= 0) + { + close(res); + } + return; + } + if (res >= 0) { int clientFd = res; @@ -252,6 +262,7 @@ private void OnTcpAcceptCompletion(int listenFd, int res, bool more) conn.UseZc = _zeroCopySend; // config default; kTLS overrides to plain on handshake Track(clientFd, conn); conn.InitRefs(); + _tcpLive++; conn.ListenerPort = PortOf(listenFd); conn.BindSendOpFlags(_sendOpFlags); // TcpOptions.SendWaitAll; the kTLS handoff clears it SubmitRecvMultishot(clientFd, (ushort)conn.Generation, bgid); @@ -400,86 +411,35 @@ private void OpenTcpListeners() private void CloseTcpListeners() { - foreach (int listenFd in _listenFds) + for (int i = 0; i < _listenFds.Length; i++) { - if (listenFd >= 0) + if (_listenFds[i] >= 0) { // Shut down first: the armed multishot accept holds the socket until the ring's deferred // cancel, about 50 ms after the ring goes, and the port stays bound and accepting until then. - shutdown(listenFd, SHUT_RDWR); - close(listenFd); + shutdown(_listenFds[i], SHUT_RDWR); + close(_listenFds[i]); + _listenFds[i] = -1; } } + _acceptPaused.Clear(); // the sweep would arm them again on numbers the stop closed } - // Before any socket closes (#531): a parked read, or a flush the kernel doesn't have, resumes while the number is - // still its own, and a flush started after it, on any connection, sends nothing. - private void MarkAcceptedTcpConnectionsClosed() + // As the sweep ends one it condemns, then aborted: connect to AF_UNSPEC is tcp_disconnect, which resets the peer and + // drops the send queue, so every send on it completes now, zero-copy notifications included. A kTLS socket refuses + // the abort and ends by the shutdown alone, which is enough: it never sends zero-copy. + private void CloseTcpConnections() { + sockaddr_in unspec = default; for (int fd = 0; fd < _connections.Length; fd++) { if (_connections[fd] is { } conn) { - // A handler letting go later must not shut down whatever socket gets the number next. - conn.SuppressFin(); - conn.MarkClosed(); - } - } - } - - // Close any still-open accepted sockets (the connection table is indexed by fd) so they don't - // leak when a host is disposed and recreated many times in one process - e.g. across a test run. - private void CloseAcceptedTcpSockets() - { - for (int fd = 0; fd < _connections.Length; fd++) - { - if (_connections[fd] != null) - { - close(fd); - } - } - } - - // A flush the kernel had ends only once the kernel is done with its send (#221). A zero-copy one may still have its - // pages held until a notification nothing will read, so that connection leaves the table here and keeps its slab. - private void ReleaseAcceptedTcpConnections(bool canCancel) - { - for (int fd = 0; fd < _connections.Length; fd++) - { - if (_connections[fd] is { } conn && canCancel && conn.FlushSubmitted - && CancelAndWait(Tag(KindTcpSend, (ushort)conn.Generation, fd))) - { - conn.CompleteFlush(); - if (conn.UseZc) + if (!conn.SweepClosed) { - _connections[fd] = null; + TcpSweepClose(conn); } - } - } - } - - // Once every wake is done, while the ring is open: a connection its handler let go of goes back to the pool as a - // recycle would, and the teardown's last step frees it. One a handler still holds, or whose send the kernel may - // still be reading, keeps its memory, as a write into it would land in freed memory (#223). - private void ReturnReleasedTcpConnections() - { - for (int fd = 0; fd < _connections.Length; fd++) - { - if (_connections[fd] is not { } conn) - { - continue; - } - - _connections[fd] = null; - if (conn.HandlerReleased && !conn.FlushSubmitted) - { - if (_incremental) - { - TeardownConnectionBufRing(conn); - } - - conn.Clear(); - ReturnToPool(conn); + connect(fd, &unspec, (uint)sizeof(sockaddr_in)); } } } diff --git a/src/ioxide/Reactor/Transport/Udp/Reactor.Udp.cs b/src/ioxide/Reactor/Transport/Udp/Reactor.Udp.cs index b6df48f4..d5ee3502 100644 --- a/src/ioxide/Reactor/Transport/Udp/Reactor.Udp.cs +++ b/src/ioxide/Reactor/Transport/Udp/Reactor.Udp.cs @@ -550,7 +550,7 @@ public void UdpSendTo(int socketFd, nint peerAddr, int peerAddrLen, ReadOnlySpan m->msg_controllen = 0; } - // Once a stop is requested no loop pass may submit an SQE: send now, or on a full socket not at all. + // The stop closes its sockets before a queued SQE would reach them: send now, or on a full socket not at all. if (_stopRequested) { long sent = sendmsg(socketFd, m, MSG_DONTWAIT); @@ -605,11 +605,12 @@ private void GrowUdpSendSlots() // closed, once the kernel holds no references into them (same discipline as the buffer slab). private void CloseUdpFds() { - foreach (int fd in _udpFds) + for (int i = 0; i < _udpFds.Length; i++) { - if (fd >= 0) + if (_udpFds[i] >= 0) { - close(fd); + close(_udpFds[i]); + _udpFds[i] = -1; // a recv still armed then completes as a released pin's does, and is not armed again } } } diff --git a/src/ioxide/io_uring/Ring.cs b/src/ioxide/io_uring/Ring.cs index 27bfa003..e452a1a3 100644 --- a/src/ioxide/io_uring/Ring.cs +++ b/src/ioxide/io_uring/Ring.cs @@ -227,9 +227,6 @@ private void RegisterFd() return &_sqes[slot]; } - /// SQEs handed out and not yet taken by the kernel. - public uint SqPending => _sqeTail - Volatile.Read(ref *_sqHead); - public int SubmitAndWait(uint waitFor) => SubmitAndWait(waitFor, -1); /// diff --git a/tests/Ioxide.Tests.E2E/Core/StopDrainTests.cs b/tests/Ioxide.Tests.E2E/Core/StopDrainTests.cs new file mode 100644 index 00000000..be04e8d3 --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Core/StopDrainTests.cs @@ -0,0 +1,885 @@ +using System.Buffers; +using System.Diagnostics; +using System.IO.Pipelines; +using System.Net; +using System.Net.Sockets; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ioxide; +using ioxide.timer; +using ioxide.utils; + +namespace Ioxide.Tests; + +/// +/// The stop as one path: it closes every listener and connection and cancels every ring op, runs on until every +/// connection has been let go and every op has completed, or its deadline passes, and only then frees what the +/// reactor owns. What a handler still holds after the deadline stays allocated. +/// +internal static class StopDrainTests +{ + // Generous against a healthy return, which takes no time at all: the assertion is that it returns. + private const int ReturnsWithinMs = 3_000; + + // The stop's deadline (Reactor.StopDrainMs). + private const int DrainMs = 1_000; + + private const int ECANCELED = 125; + + public static void Register(Runner runner) + { + foreach (bool incremental in new[] { false, true }) + { + string mode = incremental ? "incremental" : "shared ring"; + + runner.Test($"stop: a handler holding its connection on another thread is waited for, and only then is everything freed ({mode})", () => + { + ServerConfig config = Config(incremental); + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var letGo = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + holding.TrySetResult(conn); + await release.Task.ConfigureAwait(false); // on the thread pool, busy elsewhere when the stop comes + bool closed = (await conn.ReadAsync()).IsClosed; + conn.DecRef(); + letGo.TrySetResult(closed); + }, config); + + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + TcpConnection conn = holding.Task.Result; + + reactor.Stop(); + Assert.True(!thread.Join(200), "the stop ended while a handler still held its connection"); + Assert.True(HasWriteBuffer(conn) && HasReceiveMemory(reactor, conn, incremental), + "the stop freed a connection while its handler still held it"); + + release.TrySetResult(); + Assert.True(thread.Join(ReturnsWithinMs), "the stop never ended once its last handler let go"); + Assert.True(letGo.Task.Result, "the handler's read during the stop found its connection open"); + Assert.True(!HasWriteBuffer(conn), "the write slab of the connection let go of during the stop outlived it"); + Assert.True(!HasReceiveMemory(reactor, conn, incremental), "the receive memory outlived a stop whose handlers all let go"); + } + finally + { + release.TrySetResult(); + } + }, skip: incremental && !TestServer.KernelAtLeast(6, 12)); + + runner.Test($"stop: a handler that never lets go ends the stop at its deadline, with its connection and the receive memory it can reach left allocated, and what it calls after fails fast ({mode})", () => + { + ServerConfig config = Config(incremental); + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + holding.TrySetResult(conn); + await release.Task.ConfigureAwait(false); + conn.DecRef(); + }, config); + + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + TcpConnection conn = holding.Task.Result; + client.Send(Payload); + Assert.True(SpinWait.SpinUntil(() => !RecvOf(conn).IsEmpty(), 5_000), "the client's bytes were never queued"); + string socket = SocketOf(conn.ClientFd); + + var watch = Stopwatch.StartNew(); + reactor.Stop(); + Assert.True(SpinWait.SpinUntil(() => conn.IsClosed, 5_000), "the stop never closed the connection"); + Assert.Throws(() => _ = new RingTimer(reactor).DelayAsync(600_000), nameof(Reactor)); // during the drain too + Assert.True(thread.Join(5 * DrainMs), "the stop never ended, with a handler that never lets go"); + Assert.True(watch.ElapsedMilliseconds >= DrainMs - 100, $"the stop ended after {watch.ElapsedMilliseconds} ms, before its deadline"); + Assert.True(SocketOf(conn.ClientFd) == socket, $"after the stop fd {conn.ClientFd} names {SocketOf(conn.ClientFd)}, not the {socket} its handler still holds"); + Assert.True(HasWriteBuffer(conn), "the stop freed the write slab of a connection its handler still holds"); + Assert.True(HasReceiveMemory(reactor, conn, incremental), "the stop freed the receive memory a connection its handler still holds points into"); + + Task calls = Task.Run(() => + { + conn.Write("late"u8); + Assert.True(conn.FlushAsync().IsCompleted, "a flush after the stop waited"); + + ValueTask read = conn.ReadAsync(); + Assert.True(read.IsCompleted && read.Result.IsClosed, "a read after the stop waited, or found the connection open"); + int returned = 0; + while (conn.TryGetItem(read.Result, out SpscRecvRing.Item item)) + { + if (item.HasBuffer) + { + conn.ReturnBuffer(in item); + returned++; + } + } + Assert.True(returned > 0, "nothing queued before the stop was left to give back"); + + Assert.Throws(() => _ = new RingTimer(reactor).DelayAsync(600_000), nameof(Reactor)); + Assert.Throws(() => SwitchTo(reactor).GetAwaiter().GetResult(), nameof(Reactor)); + + conn.DecRef(); + }); + Assert.True(((IAsyncResult)calls).AsyncWaitHandle.WaitOne(ReturnsWithinMs), "a call into the stopped reactor never returned"); + calls.GetAwaiter().GetResult(); + } + finally + { + release.TrySetResult(); + } + }, skip: incremental && !TestServer.KernelAtLeast(6, 12)); + } + + foreach (bool peerClosed in new[] { false, true }) + { + string state = peerClosed ? "its client closed before the stop" : "open at the stop"; + + runner.Test($"stop: reads from another thread during the stop and after it get the bytes queued before it, intact, from receive memory still allocated ({state})", () => + { + ServerConfig config = SmallBuffers; + var running = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (await IsThisTestsConnection(conn)) + { + running.TrySetResult(conn); + await release.Task; // waiting on something else, and not on the connection + } + + conn.DecRef(); + }, config); + + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(running.Task.Wait(5_000), "the handler never ran"); + TcpConnection conn = running.Task.Result; + + client.Send(Payload); + if (peerClosed) + { + client.Shutdown(SocketShutdown.Send); // the reactor lets go of it itself, before the stop + } + Assert.True(SpinWait.SpinUntil(() => peerClosed ? conn.IsClosed : CountOf(conn) * config.RecvBufferSize >= Payload.Length, 5_000), + peerClosed ? "the reactor never saw its client close" : "the client's bytes were never queued"); + Thread.Sleep(50); // a segment still on its way is queued too + + reactor.Stop(); + Assert.True(SpinWait.SpinUntil(() => conn.IsClosed, 5_000), "the stop never closed the connection"); + Assert.True(thread.IsAlive, "the stop ended with a handler still holding its connection"); + + var received = new List(); + ValueTask read = conn.ReadAsync(); + Assert.True(read.IsCompleted, "a read during the stop of what was queued before it waited"); + RecvSnapshot snapshot = read.Result; + TakeFromAllocated(reactor, conn, config, snapshot, Payload.Length / 2, received); + + Assert.True(thread.Join(5 * DrainMs), "the reactor did not stop"); + TakeFromAllocated(reactor, conn, config, snapshot, int.MaxValue, received); + + Assert.True(received.ToArray().AsSpan().SequenceEqual(Payload), + $"the reads across the stop got {received.Count} bytes, not the {Payload.Length} queued before it"); + } + finally + { + release.TrySetResult(); + } + }); + } + + runner.Test("stop: what a pipe reader took before the stop and still holds stays intact, in receive memory still allocated", () => + { + var holding = new TaskCompletionSource<(TcpConnection, ReadOnlySequence)>(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + var reader = new TcpConnectionPipeReader(conn); + try + { + while (true) + { + ReadResult result = await reader.ReadAsync(); + if (result.Buffer.Length >= Payload.Length) + { + holding.TrySetResult((conn, result.Buffer)); // neither consumed nor completed + await release.Task; + return; + } + if (result.IsCompleted) + { + return; // the harness's listen probe + } + reader.AdvanceTo(result.Buffer.Start, result.Buffer.End); + } + } + finally + { + reader.Complete(); + conn.DecRef(); + } + }, SmallBuffers); + + try + { + using Socket client = Connect(port); + client.Send(Payload); + Assert.True(holding.Task.Wait(5_000), "the handler never read what its client sent"); + + reactor.Stop(); + Assert.True(thread.Join(5 * DrainMs), "the reactor did not stop"); + + (TcpConnection conn, ReadOnlySequence held) = holding.Task.Result; + foreach (ReadOnlyMemory segment in held) + { + AssertInAllocated(reactor, conn, SmallBuffers, segment); + } + Assert.True(held.ToArray().AsSpan().SequenceEqual(Payload), "what the reader held changed across the stop"); + } + finally + { + release.TrySetResult(); + } + }); + + runner.Test("stop: a ring op parked at the stop ends at once, and one its completion submits again throws ObjectDisposedException", () => + { + var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var handler = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var background = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using var stopNow = new ManualResetEventSlim(); + long stoppedAt = 0, firstEndedAt = 0; + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (r, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + try + { + var timer = new RingTimer(r); + ValueTask first = timer.DelayAsync(600_000); + parked.TrySetResult(); + int a = await first; + Volatile.Write(ref firstEndedAt, Stopwatch.GetTimestamp()); + + // Inline in the completion the stop gives, as a retry would. + handler.TrySetResult($"{a}, then {await WaitAgain(timer)}"); + } + finally + { + conn.DecRef(); + } + }, new ServerConfig { Tcp = new TcpOptions() }, r => + { + _ = WaitTwice(new RingTimer(r), background); // a wait no connection holds + + // Stopped from a tick, so a cancel left to a later tick would be a whole tick away. + r.AddTicker(() => + { + if (stopNow.IsSet && Volatile.Read(ref stoppedAt) == 0) + { + Volatile.Write(ref stoppedAt, Stopwatch.GetTimestamp()); + r.Stop(); + } + }); + }); + + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(parked.Task.Wait(5_000), "the handler never parked"); + + stopNow.Set(); + Assert.True(handler.Task.Wait(ReturnsWithinMs), "the handler's wait parked at the stop, or the one after it, never ended"); + Assert.True(background.Task.Wait(ReturnsWithinMs), "the wait no connection holds, or the one after it, never ended"); + Assert.True(thread.Join(ReturnsWithinMs), "the reactor did not stop"); + Assert.Equal($"{-ECANCELED}, then ObjectDisposedException", handler.Task.Result); + Assert.Equal($"{-ECANCELED}, then ObjectDisposedException", background.Task.Result); + + double ms = Stopwatch.GetElapsedTime(Volatile.Read(ref stoppedAt), Volatile.Read(ref firstEndedAt)).TotalMilliseconds; + Assert.True(ms < Reactor.TickMs / 2, $"the wait parked at the stop ended {ms:F0} ms after it, at a tick rather than at the stop"); + }); + + runner.Test("stop: a ring op handed over from another thread before the stop, and taken after its cancel, ends cancelled", () => + { + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + holding.TrySetResult(); + await release.Task.ConfigureAwait(false); // held, so the stop runs on past its first pass + conn.DecRef(); + }, new ServerConfig { Tcp = new TcpOptions() }); + + using var paused = new ManualResetEventSlim(); + using var resume = new ManualResetEventSlim(); + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + + // Held in this pass's posts, past its ops: what is handed over now is taken in the next pass. + reactor.ScheduleOnReactor(_ => + { + paused.Set(); + resume.Wait(5_000); + }, null); + Assert.True(paused.Wait(5_000), "the reactor never ran the post"); + Task wait = new RingTimer(reactor).DelayAsync(600_000).AsTask(); + reactor.Stop(); + resume.Set(); + + Assert.True(wait.Wait(ReturnsWithinMs), "a wait handed over before the stop, and taken after its cancel, never ended"); + Assert.Equal(-ECANCELED, wait.Result); + } + finally + { + resume.Set(); + release.TrySetResult(); + } + Assert.True(thread.Join(ReturnsWithinMs), "the reactor did not stop"); + }); + + runner.Test("stop: buffers a handler gives back from another thread during the stop are taken", () => + { + const int Held = 32; + var holding = new TaskCompletionSource<(TcpConnection, int)>(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + var held = new List(); + try + { + while (held.Count < Held) + { + 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.TrySetResult((conn, held.Count)); + await release.Task.ConfigureAwait(false); // on the thread pool, during the stop + foreach (SpscRecvRing.Item item in held) + { + conn.ReturnBuffer(in item); + } + } + finally + { + conn.DecRef(); + } + }, SmallBuffers); + + try + { + using Socket client = Connect(port); + client.Send(new byte[Held * SmallBuffers.RecvBufferSize]); + Assert.True(holding.Task.Wait(5_000), "the handler never came to hold its buffers"); + (TcpConnection conn, int held) = holding.Task.Result; + + reactor.Stop(); + Assert.True(SpinWait.SpinUntil(() => conn.IsClosed, 5_000), "the stop never closed the connection"); + ushort before = TailOf(reactor); + release.TrySetResult(); + Assert.True(thread.Join(ReturnsWithinMs), "the reactor did not stop"); + + int taken = (ushort)(TailOf(reactor) - before); + Assert.True(taken >= held, $"{taken} of the {held} buffers given back during the stop were taken"); + } + finally + { + release.TrySetResult(); + } + }); + + runner.Test("stop: a zero-copy response its client never reads ends at the stop: the flush returns, the handler lets go, the stop ends at once and the write slab is freed", () => + { + var flushing = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + byte[] response = new byte[8 << 20]; // more than both sockets' buffers hold + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + try + { + conn.Write(response); + ValueTask flush = conn.FlushAsync(); + flushing.TrySetResult(conn); + await flush; + } + finally + { + finished.TrySetResult(); + conn.DecRef(); + } + }, new ServerConfig { Tcp = new TcpOptions { ZeroCopySend = true } }); + + using var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + client.ReceiveBufferSize = 4096; // takes little, and never reads + client.Connect(IPAddress.Loopback, port); + client.Send("hi"u8); + Assert.True(flushing.Task.Wait(5_000), "the handler never flushed"); + TcpConnection conn = flushing.Task.Result; + Thread.Sleep(200); // the kernel has the send, parked on the client's closed window + Assert.True(!finished.Task.IsCompleted, "the flush ended before the stop, so nothing held it"); + + var watch = Stopwatch.StartNew(); + reactor.Stop(); + bool stopped = thread.Join(5 * DrainMs); + long ms = watch.ElapsedMilliseconds; + Assert.True(stopped, "the reactor did not stop"); + Assert.True(finished.Task.IsCompleted, "the handler's flush never returned"); + Assert.True(ms < Reactor.TickMs / 2, $"the stop took {ms} ms, held by a send its client never reads"); + Assert.True(!HasWriteBuffer(conn), "the write slab of the connection outlived the stop"); + }); + + runner.Test("stop: the stoppers run on the reactor thread before its connections close, and one that throws leaves the rest to run", () => + { + var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + bool closed = false; + var ran = new List(); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + try + { + parked.TrySetResult(); + await conn.ReadAsync(); + closed = true; + } + finally + { + conn.DecRef(); + } + }, new ServerConfig { Tcp = new TcpOptions() }, r => + { + r.AddStopper(() => ran.Add($"on the reactor {Reactor.Current == r}, its connection closed {closed}")); + r.AddStopper(() => throw new InvalidOperationException("a stopper that throws")); + r.AddStopper(() => ran.Add("the next one")); + }); + + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(parked.Task.Wait(5_000), "the handler never parked"); + + reactor.Stop(); + Assert.True(thread.Join(ReturnsWithinMs), "the reactor did not stop"); + Assert.Equal("on the reactor True, its connection closed False; the next one", string.Join("; ", ran)); + Assert.True(closed, "the stop never closed the connection to its handler"); + }); + + runner.Test("stop: from the stop on its listener refuses connections, while a handler still holds one", () => + { + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + int handlers = 0; + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + Interlocked.Increment(ref handlers); + holding.TrySetResult(); + await release.Task.ConfigureAwait(false); + conn.DecRef(); + }, new ServerConfig { Tcp = new TcpOptions() }); + + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + + reactor.Stop(); + bool refused = SpinWait.SpinUntil(() => Refuses(port), DrainMs / 2); + Assert.True(thread.IsAlive, "the stop ended with a handler still holding its connection"); + Assert.True(refused, "a connect during the stop was taken"); + Assert.Equal(1, Volatile.Read(ref handlers)); + } + finally + { + release.TrySetResult(); + } + }); + + foreach (bool paused in new[] { false, true }) + { + string how = paused ? "by the sweep, as an accept paused before the stop" : "by its accept's last completion"; + + runner.Test($"stop: no accept is armed again on the number of a listener the stop closed ({how})", () => + { + using var listenerClosed = new ManualResetEventSlim(); + using var numberTaken = new ManualResetEventSlim(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + holding.TrySetResult(); + await conn.ReadAsync(); // ends inside the stop's close, on the reactor, after its listeners closed + + // Holds the reactor until the test has put a socket of its own on the listener's number. + listenerClosed.Set(); + numberTaken.Wait(5_000); + await release.Task; // held, so the stop runs on + conn.DecRef(); + }, new ServerConfig { Tcp = new TcpOptions() }); + + using var own = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + int number = -1; + try + { + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + + int listenFd = ListenFdsOf(reactor)[0]; + Action stop = paused + ? () => { PausedOf(reactor).Add(listenFd); reactor.Stop(); } // in one pass, before a sweep can take it + : reactor.Stop; + reactor.ScheduleOnReactor(static s => ((Action)s!)(), stop); + Assert.True(listenerClosed.Wait(5_000), "the stop never closed the connection"); + + own.Bind(new IPEndPoint(IPAddress.Loopback, 0)); + own.Listen(8); + Assert.True(!IsOpen(listenFd), $"fd {listenFd} was taken before the test could take it"); + Assert.Equal(listenFd, dup2((int)own.Handle, listenFd)); + number = listenFd; + numberTaken.Set(); + + Thread.Sleep(paused ? 2 * Reactor.TickMs : 50); // a sweep, or the accept's last completion + + // An accept armed on the number takes the connection out of the backlog first, given the time. + using var probe = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + probe.Connect(own.LocalEndPoint!); + Thread.Sleep(200); + Assert.True(own.Poll(0, SelectMode.SelectRead), + "the stopped reactor armed an accept on its closed listener's number and took the connection of the socket that took it"); + } + finally + { + numberTaken.Set(); + release.TrySetResult(); + if (number >= 0) + { + close(number); + } + } + }); + } + + runner.Test("stop: a datagram that reaches a socket the stop closed is dropped, not handed to OnDatagram", () => + { + int udpPort = TestServer.NextPort(); + int datagrams = 0; + var holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + holding.TrySetResult(); + await release.Task.ConfigureAwait(false); // held, so the stop runs on + conn.DecRef(); + }, + new ServerConfig { Tcp = new TcpOptions(), Udp = new UdpOptions { Ports = [(ushort)udpPort] } }, + r => r.OnDatagram = (Reactor _, in UdpDatagram _) => Interlocked.Increment(ref datagrams)); + + try + { + using var udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + var target = new IPEndPoint(IPAddress.Loopback, udpPort); + udp.SendTo("before"u8, target); + Assert.True(SpinWait.SpinUntil(() => Volatile.Read(ref datagrams) == 1, 5_000), "the datagram before the stop never reached OnDatagram"); + + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(holding.Task.Wait(5_000), "the handler never ran"); + + reactor.Stop(); + Assert.True(SpinWait.SpinUntil(() => Refuses(port), DrainMs / 2), "the stop never closed its sockets"); + for (int i = 0; i < 8; i++) + { + udp.SendTo("after"u8, target); + } + Thread.Sleep(200); + + Assert.True(thread.IsAlive, "the stop ended with a handler still holding its connection"); + Assert.True(Volatile.Read(ref datagrams) == 1, $"{Volatile.Read(ref datagrams) - 1} datagrams reached OnDatagram after the stop closed their socket"); + } + finally + { + release.TrySetResult(); + } + }); + + runner.Test("stop: a datagram queued in the pass that stops the reactor is sent before the stop closes its socket", () => + { + int udpPort = TestServer.NextPort(); + (_, _, Thread thread) = TestServer.StartConfigured( + (_, conn) => { conn.DecRef(); return Task.CompletedTask; }, + new ServerConfig { Tcp = new TcpOptions(), Udp = new UdpOptions { Ports = [(ushort)udpPort] } }, + r => r.OnDatagram = static (Reactor reactor, in UdpDatagram d) => + { + reactor.UdpSendTo(d.SocketFd, d.PeerAddr, d.PeerAddrLen, d.Payload); + reactor.Stop(); // in the pass that queued the reply, where a stop from another thread can land too + }); + + using var udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + udp.Connect(IPAddress.Loopback, udpPort); // takes a reply from the reactor's own socket only + udp.Send("last"u8); + var reply = new byte[16]; + Assert.True(udp.Poll(5_000_000, SelectMode.SelectRead) && udp.Receive(reply) == 4, "the reply queued in the pass that stopped the reactor was never sent"); + Assert.True(thread.Join(ReturnsWithinMs), "the reactor did not stop"); + }); + } + + private static readonly byte[] Payload = [.. Enumerable.Range(0, 1000).Select(i => (byte)('a' + i % 26))]; + + // 64-byte buffers, so the payload spans many items. + private static readonly ServerConfig SmallBuffers = new() { RecvBufferSize = 64, RecvSlots = 256, Tcp = new TcpOptions() }; + + private static ServerConfig Config(bool incremental) => incremental + ? new ServerConfig { Incremental = new IncrementalOptions { MaxConnections = 16 }, Tcp = new TcpOptions() } + : new ServerConfig { RecvBufferSize = 4096, RecvSlots = 256, Tcp = new TcpOptions() }; + + private static async Task WaitTwice(RingTimer timer, TaskCompletionSource results) + { + int a = await timer.DelayAsync(600_000); + results.TrySetResult($"{a}, then {await WaitAgain(timer)}"); + } + + // What a second wait came to: its result, or the type of what it threw. + private static async Task WaitAgain(RingTimer timer) + { + try + { + return (await timer.DelayAsync(600_000)).ToString(); + } + catch (Exception e) + { + return e.GetType().Name; + } + } + + // Each item checked to lie in receive memory still allocated before a byte of it is read: freed, it can fault. + private static void TakeFromAllocated(Reactor reactor, TcpConnection conn, ServerConfig config, RecvSnapshot snapshot, int bytes, List into) + { + int taken = 0; + while (taken < bytes && conn.TryGetItem(snapshot, out SpscRecvRing.Item item)) + { + if (item.HasBuffer) + { + AssertInAllocated(reactor, conn, config, in item); + into.AddRange(item.AsSpan()); + taken += item.Len; + conn.ReturnBuffer(in item); + } + } + } + + private static unsafe void AssertInAllocated(Reactor reactor, TcpConnection conn, ServerConfig config, in SpscRecvRing.Item item) + => AssertInAllocated(reactor, conn, config, item.Ptr, item.Len); + + private static unsafe void AssertInAllocated(Reactor reactor, TcpConnection conn, ServerConfig config, ReadOnlyMemory segment) + { + fixed (byte* ptr = segment.Span) + { + AssertInAllocated(reactor, conn, config, ptr, segment.Length); + } + } + + private static unsafe void AssertInAllocated(Reactor reactor, TcpConnection conn, ServerConfig config, byte* ptr, int length) + { + byte* slab; + nuint size; + if (config.Incremental is { } incremental) + { + slab = BufSlabOf(conn); + size = (nuint)incremental.RecvSlots * (nuint)incremental.RecvBufferSize; + } + else + { + slab = SlabOf(reactor); + size = (nuint)config.RecvSlots * (nuint)config.RecvBufferSize; + } + + Assert.True(slab != null, "the receive memory the read points into was freed at the stop, under a connection its handler still holds"); + Assert.True(ptr >= slab && ptr + length <= slab + size, "the read handed out bytes from outside the receive memory"); + } + + private static unsafe bool HasWriteBuffer(TcpConnection connection) => WriteBufferOf(connection) != null; + + private static unsafe bool HasReceiveMemory(Reactor reactor, TcpConnection connection, bool incremental) => incremental + ? BufRingOf(connection) != null && BufSlabOf(connection) != null + : SlabOf(reactor) != null; + + private static int CountOf(TcpConnection connection) => RecvOf(connection).CountUntil(RecvOf(connection).SnapshotTail()); + + private static async Task SwitchTo(Reactor reactor) => await new ReactorSwitch(reactor); + + // A connect completes into a listener's backlog without anyone accepting, so it succeeds exactly while the + // listening socket is open; one that landed there as the listener closed is reset instead. + private static bool Refuses(int port) + { + using var probe = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + try + { + probe.Connect(IPAddress.Loopback, port); + return false; + } + catch (SocketException e) when (e.SocketErrorCode == SocketError.ConnectionReset) + { + return false; + } + catch (SocketException e) when (e.SocketErrorCode == SocketError.ConnectionRefused) + { + return true; + } + } + + // The harness's listen probe connects and closes without sending anything. + private static async Task IsThisTestsConnection(TcpConnection conn) + { + while (true) + { + RecvSnapshot snapshot = await conn.ReadAsync(); + + bool received = false; + while (conn.TryGetItem(snapshot, out SpscRecvRing.Item item)) + { + if (item.HasBuffer) + { + received = true; + conn.ReturnBuffer(in item); + } + } + + if (received) + { + return true; + } + if (snapshot.IsClosed) + { + return false; + } + } + } + + private static Socket Connect(int port) + { + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + socket.Connect(IPAddress.Loopback, port); + return socket; + } + + private const int FGetFd = 1; + + private static bool IsOpen(int fd) => fcntl(fd, FGetFd) != -1; + + // What a descriptor number names, as socket:[inode], or "nothing" once it is closed. + private static string SocketOf(int fd) => new FileInfo($"/proc/self/fd/{fd}").LinkTarget ?? "nothing"; + + [DllImport("libc", SetLastError = true)] + private static extern int fcntl(int fd, int command); + + [DllImport("libc", SetLastError = true)] + private static extern int dup2(int oldFd, int newFd); + + [DllImport("libc")] + private static extern int close(int fd); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "WriteBuffer")] + private static extern unsafe ref byte* WriteBufferOf(TcpConnection connection); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "BufRing")] + private static extern unsafe ref byte* BufRingOf(TcpConnection connection); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "BufSlab")] + private static extern unsafe ref byte* BufSlabOf(TcpConnection connection); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_recv")] + private static extern ref SpscRecvRing RecvOf(TcpConnection connection); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_bufSlab")] + private static extern unsafe ref byte* SlabOf(Reactor reactor); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_bufRingTail")] + private static extern ref ushort TailOf(Reactor reactor); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_listenFds")] + private static extern ref int[] ListenFdsOf(Reactor reactor); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_acceptPaused")] + private static extern ref List PausedOf(Reactor reactor); +} diff --git a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs index 008675b3..c36b77b3 100644 --- a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs +++ b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs @@ -1,7 +1,5 @@ using System.Net; using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; using ioxide; using ioxide.timer; using ioxide.utils; @@ -9,8 +7,7 @@ namespace Ioxide.Tests; /// -/// What a stop does to what is waiting on the reactor. Teardown closes a live connection's socket and leaves the -/// object, without marking it closed, and nothing drains the handoff queues afterwards. +/// What a stop does to what is waiting on the reactor, and to what is called on it after. /// internal static class StopParkedTests { @@ -122,103 +119,6 @@ public static void Register(Runner runner) Assert.True(finished.Task.Wait(ReturnsWithinMs), "the handler is still parked in its flush, its finally not run"); }); - runner.Test("stop: a send the last pass left queued goes to its own client, not to the socket that takes its number", () => - { - using var taker = new NumberTaker(); - var resumed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (r, conn) => - { - if (!await IsThisTestsConnection(conn)) - { - conn.DecRef(); - return; - } - - // On the reactor thread the send is queued in this pass, and the stop ends the loop before it is submitted. - int fd = conn.ClientFd; - conn.Write(Queued); - ValueTask flush = conn.FlushAsync(); - r.Stop(); - await flush; - - // Resumed by the stop with the socket closed: another socket takes the number, and the ring is entered. - taker.Take(fd); - EnterTheRing(r); - resumed.TrySetResult(); - conn.DecRef(); - }, new ServerConfig { Tcp = new TcpOptions(), OpSlots = 4 }); - - using Socket client = Connect(port); - client.Send("hi"u8); - - // The handler stops the reactor itself, in the pass that queued its send. - bool stopped = thread.Join(TimeSpan.FromSeconds(10)); - if (!stopped) - { - StopAndJoin(reactor, thread); - } - Assert.True(stopped, "the handler never stopped the reactor"); - Assert.True(resumed.Task.Wait(ReturnsWithinMs), "the handler never resumed from its flush"); - - taker.AssertNothingReceived(); - byte[] received = ReceiveAll(client); - Assert.True(received.AsSpan().SequenceEqual(Queued), $"the client received '{Encoding.ASCII.GetString(received)}'"); - }); - - runner.Test("stop: a send a handler woken by the stop queues on another connection goes to that client, not to the socket that takes its number", () => - { - using var taker = new NumberTaker(); - var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var resumed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var conns = new List(); // reactor thread only - - (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (r, conn) => - { - if (!await IsThisTestsConnection(conn)) - { - conn.DecRef(); - return; - } - - conns.Add(conn); - if (conns.Count == 2) - { - parked.TrySetResult(); - } - - await conn.ReadAsync(); // until the stop closes it - - // The stop goes through its connections by descriptor: the lower one wakes with the other still open. - TcpConnection other = conns[0] == conn ? conns[1] : conns[0]; - if (conn.ClientFd < other.ClientFd) - { - int fd = other.ClientFd; - other.Write(Queued); - await other.FlushAsync(); - - taker.Take(fd); - EnterTheRing(r); - resumed.TrySetResult(); - } - - conn.DecRef(); - }, new ServerConfig { Tcp = new TcpOptions(), OpSlots = 4 }); - - using Socket first = Connect(port); - first.Send("hi"u8); - using Socket second = Connect(port); - second.Send("hi"u8); - Assert.True(parked.Task.Wait(5_000), "the handlers never parked"); - - StopAndJoin(reactor, thread); - Assert.True(resumed.Task.Wait(ReturnsWithinMs), "the woken handler never resumed from its flush on the other connection"); - - taker.AssertNothingReceived(); - byte[] received = [.. ReceiveAll(first), .. ReceiveAll(second)]; - Assert.True(received.AsSpan().SequenceEqual(Queued), $"the clients received '{Encoding.ASCII.GetString(received)}'"); - }); - foreach (bool incremental in new[] { false, true }) { string mode = incremental ? "incremental" : "shared ring"; @@ -355,92 +255,6 @@ public static void Register(Runner runner) }); } - private static readonly byte[] Queued = "QUEUED-FOR-THE-CLOSED-SOCKET"u8.ToArray(); - - // With OpSlots = 4 the fifth wait grows the op table, which first submits whatever is queued. - private static void EnterTheRing(Reactor reactor) - { - for (int i = 0; i < 8; i++) - { - _ = new RingTimer(reactor).DelayAsync(600_000); - } - } - - // Until the server's side is gone, which the ring's teardown ends. - private static byte[] ReceiveAll(Socket socket) - { - socket.ReceiveTimeout = 3_000; - var received = new List(); - byte[] buffer = new byte[256]; - try - { - int n; - while ((n = socket.Receive(buffer)) > 0) - { - received.AddRange(buffer.AsSpan(0, n)); - } - } - catch (SocketException) - { - // a reset or the timeout: what arrived before it is the answer - } - - return [.. received]; - } - - // A socket put on a descriptor number the stop freed, as the next accept or open anywhere in the process would - // be: what the kernel then sends on that number reaches this socket's peer. - private sealed class NumberTaker : IDisposable - { - private readonly Socket _listener = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - private readonly Socket _peer = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - private readonly Socket _accepted; - private int _number = -1; - private int _dup2Result; - - public NumberTaker() - { - _listener.Bind(new IPEndPoint(IPAddress.Loopback, 0)); - _listener.Listen(1); - _peer.Connect(_listener.LocalEndPoint!); - _accepted = _listener.Accept(); - } - - // From the handler, on the reactor thread, so it throws nothing. - public void Take(int number) - { - _number = number; - _dup2Result = dup2((int)_accepted.SafeHandle.DangerousGetHandle(), number); - } - - public void AssertNothingReceived() - { - Assert.True(_dup2Result == _number, $"dup2 onto {_number} returned {_dup2Result}"); - - bool readable = _peer.Poll(200_000, SelectMode.SelectRead); - int bytes = readable ? _peer.Available : 0; - Assert.True(bytes == 0, $"the socket that took number {_number} received {bytes} bytes queued for the one the stop closed"); - } - - public void Dispose() - { - if (_number >= 0 && _dup2Result == _number) - { - close(_number); - } - - _accepted.Dispose(); - _peer.Dispose(); - _listener.Dispose(); - } - - [DllImport("libc", SetLastError = true)] - private static extern int dup2(int oldFd, int newFd); - - [DllImport("libc")] - private static extern int close(int fd); - } - // A read, a flush its client holds up, a RingTimer wait, a handler that let go, one held outside ioxide. private const string Roles = "rftlh"; diff --git a/tests/Ioxide.Tests.E2E/Core/TcpTimeoutTests.cs b/tests/Ioxide.Tests.E2E/Core/TcpTimeoutTests.cs index 55a6886c..488f203f 100644 --- a/tests/Ioxide.Tests.E2E/Core/TcpTimeoutTests.cs +++ b/tests/Ioxide.Tests.E2E/Core/TcpTimeoutTests.cs @@ -358,16 +358,6 @@ public static void Register(Runner runner) Assert.Equal((long)body.Length, total); }); - runner.Test("tcp/exit: a handler that lets go after its reactor stopped leaves a reused fd alone", - () => ReleaseAfterStop(static conn => conn.DecRef())); - - runner.Test("tcp/half-close: a ShutdownWrite after its reactor stopped leaves a reused fd alone", - () => ReleaseAfterStop(static conn => - { - conn.ShutdownWrite(); - conn.DecRef(); - })); - runner.Test("tcp/half-close: ShutdownWrite ends the response now, and the read side stays open", () => { // HTTP's Connection: close. The handler keeps the connection after its last response, so @@ -722,87 +712,6 @@ private static void StalledResponse(bool zeroCopy) private static bool IsOpen(int fd) => fcntl(fd, FGetFd) != -1; - // Teardown closes every fd without releasing the reactor's ref, so a later release still sees a - // live connection. Its FIN must not reach whichever socket took the number since. - private static void ReleaseAfterStop(Action release) - { - var captured = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var released = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => - { - if (!await IsThisTestsConnection(conn)) - { - conn.DecRef(); - return; - } - - captured.TrySetResult(conn); - await gate.Task.ConfigureAwait(false); // released off the reactor, after it stopped - release(conn); - released.TrySetResult(); - }, new ServerConfig - { - RecvBufferSize = 4096, - RecvSlots = 256, - Tcp = new TcpOptions { WriteSlabSize = 16 * 1024, PoolMax = 64, RecvQueueEntries = 64 }, - }); - - var listener = new TcpListener(IPAddress.Loopback, 0); // opened first, so not the number under test - listener.Start(); - - using var client = new TcpClient(); - client.Connect("127.0.0.1", port); - client.GetStream().Write("hi"u8); - Assert.True(captured.Task.Wait(5_000), "the handler never saw the test's connection"); - int fd = captured.Task.Result.ClientFd; - - reactor.Stop(); - Assert.True(thread.Join(5_000), "the reactor did not stop"); - - // Linux hands out the lowest free number, so fd is reused by one of the next sockets. - var keep = new List(); - Socket? victim = null, peer = null; - for (int i = 0; i < 64 && victim is null; i++) - { - var c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - c.Connect((IPEndPoint)listener.LocalEndpoint); - Socket a = listener.AcceptSocket(); - if ((int)c.Handle == fd) { victim = c; peer = a; } - else if ((int)a.Handle == fd) { victim = a; peer = c; } - else { keep.Add(c); keep.Add(a); } - } - - try - { - Assert.True(victim is not null, $"fd {fd} was not handed out again within 64 sockets"); - peer!.ReceiveTimeout = 2_000; - - gate.TrySetResult(); - Assert.True(released.Task.Wait(5_000), "the handler never released"); - - string outcome; - try - { - victim!.Send("b"u8.ToArray()); - outcome = peer.Receive(new byte[1]) == 1 ? "ok" : "eof on the unrelated socket"; - } - catch (SocketException e) - { - outcome = $"the unrelated socket failed: {e.SocketErrorCode}"; - } - Assert.Equal("ok", outcome); - } - finally - { - foreach (Socket k in keep) k.Dispose(); - victim?.Dispose(); - peer?.Dispose(); - listener.Stop(); - } - } - /// /// Reads until this connection delivers actual bytes, and says whether it ever did. /// diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index a471a316..f93fc566 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -37,6 +37,7 @@ private static int Main() GcTests.Register(runner); RingTimerTests.Register(runner); StopParkedTests.Register(runner); + StopDrainTests.Register(runner); UdpTests.Register(runner); QuicTests.Register(runner); QuicEngineTests.Register(runner); diff --git a/tests/Ioxide.Tests.File/FileTests.cs b/tests/Ioxide.Tests.File/FileTests.cs index b2d1a2c1..baeffa6c 100644 --- a/tests/Ioxide.Tests.File/FileTests.cs +++ b/tests/Ioxide.Tests.File/FileTests.cs @@ -1,4 +1,6 @@ +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ioxide; using ioxide.file; namespace Ioxide.Tests; @@ -56,6 +58,36 @@ public static void Register(Runner runner) Assert.Equal(BigAsset, body); }); + runner.Test("file: a reactor's stop disposes its reader pools: an idle reader is freed, a renter waiting and a rent made after fail with ObjectDisposedException, and a reader given back after is freed", () => + { + AssetReader idle = null!, held = null!; + RingPool busy = null!; + Task waiting = null!; + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + static (_, conn) => { conn.DecRef(); return Task.CompletedTask; }, + new ServerConfig { Tcp = new TcpOptions() }, + r => + { + RingPool quiet = AssetReader.CreatePool(r, readers: 1, bufferBytes: 4096); + idle = quiet.RentAsync().Result; // idle readers are handed out at once + quiet.Return(idle); + + busy = AssetReader.CreatePool(r, readers: 1, bufferBytes: 4096); + held = busy.RentAsync().Result; + waiting = EndOf(busy.RentAsync); + }); + + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + string waited = waiting.Wait(5_000) ? waiting.Result : "still waiting"; + busy.Return(held); + Task late = EndOf(busy.RentAsync); + string after = late.Wait(5_000) ? late.Result : "still waiting"; + + Assert.Equal("idle freed: True, waiting: ObjectDisposedException, given back after the stop freed: True, rent after: ObjectDisposedException", + $"idle freed: {DisposedOf(idle) != 0}, waiting: {waited}, given back after the stop freed: {DisposedOf(held) != 0}, rent after: {after}"); + }); + runner.Test("file: a child process does not inherit a RingFile's descriptor", () => { string dir = SampleAssets(); @@ -85,6 +117,23 @@ public static void Register(Runner runner) }); } + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_disposed")] + private static extern ref int DisposedOf(AssetReader reader); + + // How a rent ended: the type of what it threw, or that it got a reader. + private static async Task EndOf(Func> rent) + { + try + { + await rent(); + return "rented"; + } + catch (Exception e) + { + return e.GetType().Name; + } + } + [DllImport("libc")] private static extern int open([MarshalAs(UnmanagedType.LPUTF8Str)] string path, int flags, int mode); diff --git a/tests/Ioxide.Tests.Harness/TestServer.cs b/tests/Ioxide.Tests.Harness/TestServer.cs index d1279417..35f59d35 100644 --- a/tests/Ioxide.Tests.Harness/TestServer.cs +++ b/tests/Ioxide.Tests.Harness/TestServer.cs @@ -464,7 +464,7 @@ public static (int Port, Reactor[] Reactors) StartQuicSharded(int reactorCount, bool held = holdLast is not null && shard == reactorCount - 1; var reactor = new Reactor(shard, config) { - TcpHandle = static (_, _) => Task.CompletedTask, + TcpHandle = static (_, conn) => { conn.DecRef(); return Task.CompletedTask; }, QuicHandle = quicHandle, OnStart = held ? _ => lastStarted?.Set() : _ => ready.Signal(), }; @@ -555,7 +555,7 @@ public static (int TcpPort, int UdpPort) StartDatagramConfigured( var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var reactor = new Reactor(0, config) { - TcpHandle = static (_, _) => Task.CompletedTask, + TcpHandle = static (_, conn) => { conn.DecRef(); return Task.CompletedTask; }, QuicHandle = quicHandle, OnDatagram = onDatagram, OnStart = _ => started.TrySetResult(), diff --git a/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs b/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs index 923ca21b..2d9101c2 100644 --- a/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs +++ b/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs @@ -89,6 +89,43 @@ public static void Register(Runner runner) } }); + runner.Test("httpclient h1: a connect its reactor's stop cancels, and the one to the next address it goes on to, both end with the stop", () => + { + // Two addresses whose full accept queues drop the SYN, so neither connect ends unless the stop ends it. + using var first = new TcpListener(IPAddress.Parse("127.0.0.3"), 0); + first.Start(0); + int port = ((IPEndPoint)first.LocalEndpoint).Port; + using var second = new TcpListener(IPAddress.Parse("127.0.0.4"), port); + second.Start(0); + Socket[] queued = [.. FillAcceptQueue((IPEndPoint)first.LocalEndpoint), .. FillAcceptQueue((IPEndPoint)second.LocalEndpoint)]; + + try + { + var started = new TaskCompletionSource(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, [IPAddress.Parse("127.0.0.3"), IPAddress.Parse("127.0.0.4")], + (ushort)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(((IAsyncResult)connect).AsyncWaitHandle.WaitOne(3_000), "the connect is still waiting 3 s after its reactor was stopped"); + Assert.True(thread.Join(3_000), "the reactor did not stop"); + Assert.True(connect.IsFaulted, $"the connect ended {connect.Status}, connected to an address that never answers"); + } + 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; diff --git a/tests/Ioxide.Tests.Http/HttpClientStopTests.cs b/tests/Ioxide.Tests.Http/HttpClientStopTests.cs new file mode 100644 index 00000000..4217db38 --- /dev/null +++ b/tests/Ioxide.Tests.Http/HttpClientStopTests.cs @@ -0,0 +1,363 @@ +using System.Net; +using System.Net.Sockets; +using System.Runtime.CompilerServices; +using System.Text; +using ioxide; +using ioxide.httpclient; +using ioxide.timer; +using ioxide.utils; + +namespace Ioxide.Tests; + +/// +/// The clients as their reactor stops. Its first pass disposes every pool and client started on it, before it closes +/// its connections: their idle connections close, their TLS context is freed, and the requests waiting on them fail +/// with ObjectDisposedException while the reactor still runs what that wakes. +/// +internal static class HttpClientStopTests +{ + // Generous against a close or an end that takes no time at all: the assertion is that it comes. + private const int WithinMs = 5_000; + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_tls")] + private static extern ref TlsClientContext? TlsOf(RingHttpClient client); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_disposed")] + private static extern ref bool DisposedOf(TlsClientContext context); + + public static void Register(Runner runner) + { + runner.Test("httpclient h1: a connection handed to a waiting request as its reactor stops is let go, and the requests queued behind it end", () => + { + using var origin = new CountingOrigin(); + HttpClientPool pool = null!; + (_, Reactor reactor, Thread thread) = StartReactor(r => pool = HttpClientPool.Start(r, + new HttpClientOptions { Port = (ushort)origin.Port, PoolSize = 1, AcquireTimeoutMs = 30_000 })); + + // The first answer hands the one connection to the second request, whose continuation is posted, and the + // stop comes in that same pass: the second takes the connection and its send is refused. + (Task second, Task third) = RunOnReactor(reactor, async () => + { + ValueTask first = pool.GetAsync("/"); + Task second = EndOf(pool.GetAsync("/")); + Task third = EndOf(pool.GetAsync("/")); + (await first).Dispose(); + reactor.Stop(); + return (second, third); + }); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + Assert.Equal("second: ObjectDisposedException, third: ObjectDisposedException, origin connections closed: 1 of 1", + $"second: {Ended(second)}, third: {Ended(third)}, origin connections closed: {origin.ClosedOf(1)}"); + }); + + runner.Test("httpclient h1: a pool's idle connections are closed by its reactor's stop", () => + { + using var origin = new CountingOrigin(); + HttpClientPool pool = null!; + (_, Reactor reactor, Thread thread) = StartReactor(r => pool = HttpClientPool.Start(r, + new HttpClientOptions { Port = (ushort)origin.Port, PoolSize = 2 })); + + int idle = RunOnReactor(reactor, async () => + { + ValueTask a = pool.GetAsync("/"), b = pool.GetAsync("/"); + (await a).Dispose(); + (await b).Dispose(); + return pool.IdleCount; + }); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + Assert.Equal("idle 2, origin connections closed: 2 of 2", $"idle {idle}, origin connections closed: {origin.ClosedOf(2)}"); + }); + + runner.Test("httpclient h1: a request a handler makes as its reactor stops is refused, and never reaches the origin", () => + { + // The stop resumes a handler by closing its connection, after it has disposed the pool. + using var origin = new CountingOrigin(); + var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Task? asked = null; + HttpClientPool pool = null!; + (int port, Reactor reactor, Thread thread) = StartReactor( + r => pool = HttpClientPool.Start(r, new HttpClientOptions { Port = (ushort)origin.Port, PoolSize = 1 }), + async (_, connection) => + { + try + { + if (await SentSomething(connection)) + { + parked.TrySetResult(); + await connection.ReadAsync(); // until the stop closes it + asked = FailureOf(pool); + } + } + finally + { + connection.DecRef(); + } + }); + + RunOnReactor(reactor, async () => + { + (await pool.GetAsync("/")).Dispose(); // the pool's one connection, idle + return 0; + }); + using var client = new TcpClient("127.0.0.1", port); + client.GetStream().Write("hi"u8); + Assert.True(parked.Task.Wait(WithinMs), "the handler never parked"); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + string closed = origin.ClosedOf(1); + Assert.Equal($"ObjectDisposedException: pool for 127.0.0.1:{origin.Port} is disposed Object name: 'HttpClientPool'.; origin requests 1, connections closed: 1 of 1", + $"{(asked is null ? "never asked" : Ended(asked))}; origin requests {origin.Requests}, connections closed: {closed}"); + }); + + runner.Test("ring client: its reactor's stop closes its origins' idle connections and frees its TLS context", () => + { + using var origin = new CountingOrigin(); + using TlsTestOrigin secure = TlsTestOrigin.Start("http/1.1"); + (string certPath, _) = TestCert.Ensure(); + RingHttpClient http = null!; + (_, Reactor reactor, Thread thread) = StartReactor(r => http = RingHttpClient.Start(r, new RingHttpClientOptions + { + Tls = new TlsClientOptions { CaFile = certPath, AlpnProtocols = ["http/1.1"] }, + })); + + string answered = RunOnReactor(reactor, async () => + { + using HttpClientResponse plain = await http.GetAsync($"http://127.0.0.1:{origin.Port}/"); + using HttpClientResponse tls = await http.GetAsync($"https://localhost:{secure.Port}/"); + return $"{plain.Status} {tls.Status}, origins {http.OriginCount}"; + }, timeoutMs: 20_000); + TlsClientContext context = TlsOf(http)!; + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + Assert.Equal("200 200, origins 2; origin connections closed: 1 of 1, TLS context freed: True", + $"{answered}; origin connections closed: {origin.ClosedOf(1)}, TLS context freed: {DisposedOf(context)}"); + }); + + runner.Test("ring client: a request made once it is disposed fails with ObjectDisposedException", () => + { + string outcome = OnReactor.Run(async reactor => + { + RingHttpClient http = RingHttpClient.Start(reactor); + http.Dispose(); + return await EndOf(http.GetAsync($"http://127.0.0.1:{TestServer.DeadPort()}/")); + }); + + Assert.Equal("ObjectDisposedException", outcome); + }); + + runner.Test("httpclient h1: a request waiting out the reconnect backoff ends when its reactor stops", () => + { + string outcome = EndedByTheStop(reactor => HttpClientPool.Start(reactor, new HttpClientOptions + { Port = (ushort)TestServer.DeadPort(), PoolSize = 1, AcquireTimeoutMs = 30_000 }).GetAsync("/")); + + Assert.True(outcome.StartsWith("ObjectDisposedException: pool for 127.0.0.1:"), $"expected the stop to dispose the pool under the request, got: {outcome}"); + }); + + runner.Test("ring client: a request waiting out the reconnect backoff ends when its reactor stops", () => + { + string outcome = EndedByTheStop(reactor => RingHttpClient.Start(reactor, new RingHttpClientOptions + { Pool = new HttpClientOptions { PoolSize = 1, AcquireTimeoutMs = 30_000 } }) + .GetAsync($"http://127.0.0.1:{TestServer.DeadPort()}/")); + + Assert.True(outcome.StartsWith("ObjectDisposedException: pool for 127.0.0.1:"), $"expected the stop to dispose the pool under the request, got: {outcome}"); + }); + + runner.Test("tls client: a connect that completes after its TLS context was freed is refused, and closes its socket", () => + { + // A RingHttpClient its reactor's stop disposes frees the context while an open of its can still be connecting. + using var origin = new CountingOrigin(); + TlsClientContext tls = TlsClientContext.Create(new TlsClientOptions { ServerName = "localhost" }); + tls.Dispose(); + + string outcome = OnReactor.Run(async reactor => + { + try + { + await ClientTransport.ConnectAsync(reactor, [IPAddress.Loopback], (ushort)origin.Port, tls, "localhost", connectTimeoutMs: 0); + return "connected"; + } + catch (Exception e) + { + return e.GetType().Name; + } + }); + + Assert.Equal("ObjectDisposedException, origin connections closed: 1 of 1", $"{outcome}, origin connections closed: {origin.ClosedOf(1)}"); + }); + } + + private static (int Port, Reactor Reactor, Thread Thread) StartReactor(Action onStart, + Func? handle = null) + => TestServer.StartConfigured( + handle ?? ((_, connection) => { connection.DecRef(); return Task.CompletedTask; }), + new ServerConfig + { + RecvBufferSize = 4096, + RecvSlots = 64, + Tcp = new TcpOptions { WriteSlabSize = 16 * 1024, PoolMax = 8, RecvQueueEntries = 64 }, + }, + onStart); + + // From the reactor's post queue rather than OnStart, so a body that stops the reactor cannot do it before the + // harness has seen it listen. + private static T RunOnReactor(Reactor reactor, Func> body, int timeoutMs = 10_000) + { + var outcome = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + reactor.ScheduleOnReactor(_ => _ = RunAsync(), null); + Assert.True(outcome.Task.Wait(timeoutMs), $"the code on the reactor did not finish within {timeoutMs} ms"); + return outcome.Task.Result; + + async Task RunAsync() + { + try + { + outcome.TrySetResult(await body()); + } + catch (Exception e) + { + outcome.TrySetException(e); + } + } + } + + // Makes the request on a reactor of its own and stops that reactor once 50 ms have passed: the refused connect is + // long done, and the backoff after it, 200 ms at least, has the request waiting. How the request ended, once stopped. + private static string EndedByTheStop(Func> request) + { + var made = new TaskCompletionSource>(TaskCreationOptions.RunContinuationsAsynchronously); + (_, Reactor reactor, Thread thread) = StartReactor(r => _ = MakeAsync(r)); + + Assert.True(made.Task.Wait(10_000), "the request was never made"); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + Task call = made.Task.Result; + try + { + return call.Wait(2_000) ? $"answered {call.Result.Status}" : "still waiting 2 s after its reactor stopped"; + } + catch (AggregateException e) + { + return $"{e.InnerException!.GetType().Name}: {e.InnerException.Message.ReplaceLineEndings(" ")}"; + } + + async Task MakeAsync(Reactor r) + { + Task pending = request(r).AsTask(); + await new RingTimer(r).DelayAsync(50); + made.SetResult(pending); + } + } + + // How a request ended: the status it was answered with, or the type of what it threw. + private static async Task EndOf(ValueTask request) + { + try + { + using HttpClientResponse response = await request; + return $"answered {response.Status}"; + } + catch (Exception e) + { + return e.GetType().Name; + } + } + + private static string Ended(Task request) => request.Wait(WithinMs) ? request.Result : "never ended"; + + // Why a request made now fails, or the status it was answered with. + private static async Task FailureOf(HttpClientPool pool) + { + try + { + using HttpClientResponse response = await pool.GetAsync("/"); + return $"answered {response.Status}"; + } + catch (Exception e) + { + return $"{e.GetType().Name}: {e.Message.ReplaceLineEndings(" ")}"; + } + } + + // The harness's listen probe connects and closes without sending anything. + private static async Task SentSomething(TcpConnection connection) + { + while (true) + { + RecvSnapshot snapshot = await connection.ReadAsync(); + + bool received = false; + while (connection.TryGetItem(snapshot, out SpscRecvRing.Item item)) + { + if (item.HasBuffer) + { + received = true; + connection.ReturnBuffer(in item); + } + } + + if (received || snapshot.IsClosed) + { + return received; + } + } + } + + // Answers every request with 200, and counts them and the connections its client closed. + private sealed class CountingOrigin : IDisposable + { + private readonly FakeServer _server; + private int _requests; + private int _closed; + + public CountingOrigin() => _server = new FakeServer((_, socket) => + { + try + { + var buffer = new byte[4096]; + var received = new StringBuilder(); + int n; + while ((n = socket.Receive(buffer)) > 0) + { + received.Append(Encoding.Latin1.GetString(buffer, 0, n)); + for (int end; (end = received.ToString().IndexOf("\r\n\r\n", StringComparison.Ordinal)) >= 0;) + { + received.Remove(0, end + 4); + Interlocked.Increment(ref _requests); + socket.Send("HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok"u8); + } + } + } + catch (SocketException) + { + // reset by its client: closed too + } + finally + { + Interlocked.Increment(ref _closed); + } + }); + + public int Port => _server.Port; + + public int Requests => Volatile.Read(ref _requests); + + // How many of `expected` connections it saw closed, given until WithinMs to see them all. + public string ClosedOf(int expected) + { + long deadline = Environment.TickCount64 + WithinMs; + while (Volatile.Read(ref _closed) < expected && Environment.TickCount64 < deadline) + { + Thread.Sleep(20); + } + return $"{Volatile.Read(ref _closed)} of {expected}"; + } + + public void Dispose() => _server.Dispose(); + } +} diff --git a/tests/Ioxide.Tests.Http/Program.cs b/tests/Ioxide.Tests.Http/Program.cs index a44780b0..75936e03 100644 --- a/tests/Ioxide.Tests.Http/Program.cs +++ b/tests/Ioxide.Tests.Http/Program.cs @@ -20,6 +20,7 @@ private static int Main() HttpClientRequestHeadTests.Register(runner); HttpClientConnectTests.Register(runner); RingHttpClientTests.Register(runner); + HttpClientStopTests.Register(runner); HttpClientChunkedTests.Register(runner); HttpClientResponseLimitTests.Register(runner); HttpClientHttp10Tests.Register(runner); diff --git a/tests/Ioxide.Tests.Pg/PgStopTests.cs b/tests/Ioxide.Tests.Pg/PgStopTests.cs new file mode 100644 index 00000000..b68a193a --- /dev/null +++ b/tests/Ioxide.Tests.Pg/PgStopTests.cs @@ -0,0 +1,163 @@ +using System.Buffers.Binary; +using System.Net.Sockets; +using ioxide; +using ioxide.pg; +using ioxide.timer; + +namespace Ioxide.Tests; + +/// +/// The pools as their reactor stops: its first pass disposes each pool started on it, so their connections close and +/// the queries waiting for one fail with ObjectDisposedException. Fake servers, so no postgres is needed. +/// +internal static class PgStopTests +{ + // AuthenticationOk, then ReadyForQuery (idle): a startup that connects. + private static readonly byte[] Connected = [(byte)'R', 0, 0, 0, 8, 0, 0, 0, 0, (byte)'Z', 0, 0, 0, 5, (byte)'I']; + + public static void Register(Runner runner) + { + runner.Test("pg: a disposed pool closes a connection whose open completes after, and opens no more", () => + { + int accepted = 0, closed = 0; + using var answer = new ManualResetEventSlim(); + using var slow = new FakeServer((_, socket) => + { + Interlocked.Increment(ref accepted); + try + { + answer.Wait(10_000); + AnswerStartup(socket); + while (socket.Receive(new byte[256]) > 0) + { + } + } + catch (Exception e) when (e is SocketException or IOException) + { + // reset: closed too + } + Interlocked.Increment(ref closed); + }); + + string outcome = OnReactor.Run(r => DisposedWhileOpening(r, () => Volatile.Read(ref accepted), () => Volatile.Read(ref closed), answer, slow.Port)); + Assert.Equal("accepted 1, closed 1, connections 0", outcome); + }); + + runner.Test("pg: a reactor's stop closes each of its pools' connections, and fails the queries waiting for one with ObjectDisposedException", () => + { + int closed = 0; + using var answering = new FakeServer((_, socket) => + { + try + { + AnswerStartup(socket); + while (socket.Receive(new byte[256]) > 0) + { + } + } + catch (SocketException) + { + // reset: closed too + } + Interlocked.Increment(ref closed); + }); + using var silent = new FakeServer((_, _) => { }); // takes the connection and never answers its startup + + PgPool idle = null!; + Task waiting = null!; + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + (_, connection) => { connection.DecRef(); return Task.CompletedTask; }, + new ServerConfig { Tcp = new TcpOptions() }, + r => + { + idle = PgPool.Start(r, Options(answering.Port)); + PgPool stuck = PgPool.Start(r, Options(silent.Port)); + waiting = EndOf(stuck.QueryAsync("SELECT 1")); + }); + + Assert.True(SpinWait.SpinUntil(() => idle.ConnectionCount == 1, 5_000), "the pool never connected to the server that answers"); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + Assert.Equal("waiting: ObjectDisposedException, connections closed: 1", + $"waiting: {(waiting.Wait(5_000) ? waiting.Result : "still waiting")}, connections closed: {ClosedWithin(ref closed, 1)}"); + }); + + runner.Test("pg: a connection opened on a stopping reactor fails with ObjectDisposedException and leaves no socket open", () => + { + string outcome = "the stopper never ran"; + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + (_, connection) => { connection.DecRef(); return Task.CompletedTask; }, + new ServerConfig { Tcp = new TcpOptions() }, + r => r.AddStopper(() => + { + // On the reactor once its stop has begun, as a handler the stop wakes would open one. + int before = FdCount.Stable(); + Task[] opens = [.. Enumerable.Range(0, 8).Select(_ => PgConnection.ConnectAsync(r, Options(TestServer.DeadPort()) with { CommandTimeoutMs = 5_000 }))]; + outcome = $"{opens[^1].Exception?.InnerException?.GetType().Name ?? opens[^1].Status.ToString()}, sockets left open: {FdCount.Stable() - before}"; + })); + + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + Assert.Equal("ObjectDisposedException, sockets left open: 0", outcome); + }); + } + + private static async Task DisposedWhileOpening(Reactor r, Func accepted, Func closed, ManualResetEventSlim answer, int port) + { + var timer = new RingTimer(r); + PgPool pool = PgPool.Start(r, Options(port)); + for (long deadline = Native.MonotonicMs + 5_000; accepted() == 0 && Native.MonotonicMs < deadline;) + { + await timer.DelayAsync(10); + } + + pool.Dispose(); + answer.Set(); // the open completes now, on a disposed pool + for (long deadline = Native.MonotonicMs + 2_000; closed() == 0 && Native.MonotonicMs < deadline;) + { + await timer.DelayAsync(10); + } + await timer.DelayAsync(3 * Reactor.TickMs); // sweeps that would open another + return $"accepted {accepted()}, closed {closed()}, connections {pool.ConnectionCount}"; + } + + private static PgOptions Options(int port) => new() + { + Port = (ushort)port, + User = "fake", + Database = "fake", + PoolSize = 1, + CommandTimeoutMs = 0, // a startup never answered waits until the stop + }; + + private static void AnswerStartup(Socket socket) + { + int length = BinaryPrimitives.ReadInt32BigEndian(FakeServer.Receive(socket, 4)); + FakeServer.Receive(socket, length - 4); // protocol version and parameters + socket.Send(Connected); + } + + // How a query ended: its value, or the type of what it threw. + private static async Task EndOf(ValueTask query) + { + try + { + return $"answered {(await query).Value}"; + } + catch (Exception e) + { + return e.GetType().Name; + } + } + + private static int ClosedWithin(ref int closed, int expected) + { + long deadline = Environment.TickCount64 + 5_000; + while (Volatile.Read(ref closed) < expected && Environment.TickCount64 < deadline) + { + Thread.Sleep(20); + } + return Volatile.Read(ref closed); + } +} diff --git a/tests/Ioxide.Tests.Pg/Program.cs b/tests/Ioxide.Tests.Pg/Program.cs index adeb08cb..01f1329a 100644 --- a/tests/Ioxide.Tests.Pg/Program.cs +++ b/tests/Ioxide.Tests.Pg/Program.cs @@ -26,6 +26,7 @@ private static int Main() PgPreparedTests.Register(runner, pg, up); PgThreadTests.Register(runner, pg, up); PgFramingTests.Register(runner); + PgStopTests.Register(runner); return runner.Summary(); } } diff --git a/tests/Ioxide.Tests.Redis/StackExchangeTransportTests.cs b/tests/Ioxide.Tests.Redis/StackExchangeTransportTests.cs index 97ec0b3c..79d79fe3 100644 --- a/tests/Ioxide.Tests.Redis/StackExchangeTransportTests.cs +++ b/tests/Ioxide.Tests.Redis/StackExchangeTransportTests.cs @@ -49,6 +49,58 @@ public static void Register(Runner runner, (string Host, int Port) redis, bool r Assert.Equal("RedisConnectionException", Get(redis, run, "/stranded")); }, because: "StackExchange.Redis reports transport closes only for Stream-backed connections"); + // No stopper: each transport always has a receive on the ring, which the stop cancels. + runner.Test("redis over SE.Redis: a reactor's stop closes the connections its multiplexer has on the ring", () => + { + ConfigurationOptions options = Options(redis); + options.ClientName = $"ioxide-stop-{run}"; + Task connecting = null!; + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + (_, connection) => { connection.DecRef(); return Task.CompletedTask; }, new ServerConfig { Tcp = new TcpOptions() }, + r => connecting = IoxideRedis.ConnectAsync(r, options)); + Assert.True(connecting.Wait(10_000), "the multiplexer never connected"); + using ConnectionMultiplexer mux = connecting.Result; + using ConnectionMultiplexer observer = ConnectionMultiplexer.Connect(Options(redis)); // on its own sockets + int Connected() => observer.GetServer(observer.GetEndPoints()[0]).ClientList().Count(c => c.Name == options.ClientName); + + int before = Connected(); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + SpinWait.SpinUntil(() => Connected() == 0, 5_000); // the server sees the closes a little after + Assert.Equal("connections before the stop 2, after it 0", $"connections before the stop {before}, after it {Connected()}"); + }, skip: !redisUp); + + // StackExchange.Redis reconnects through the tunnel while its multiplexer lives, its reactor stopped or not. + runner.Test("redis over SE.Redis: a connect through the tunnel of a stopped reactor is refused with ObjectDisposedException and leaves no socket open", () => + { + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + (_, connection) => { connection.DecRef(); return Task.CompletedTask; }, new ServerConfig { Tcp = new TcpOptions() }); + reactor.Stop(); + Assert.True(thread.Join(10_000), "the reactor did not stop"); + + var tunnel = new IoxideTunnel(reactor); + var endpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, TestServer.DeadPort()); +#pragma warning disable SER009 // the transport seam, which StackExchange.Redis drives on a reconnect + var tls = new StackExchange.Redis.Configuration.TlsOptions(new ConfigurationOptions()); + int before = FdCount.Stable(); + string outcome = ""; + for (int i = 0; i < 8; i++) + { + try + { + tunnel.ConnectTransportAsync(endpoint, ConnectionType.Interactive, tls, CancellationToken.None).AsTask().Wait(); + outcome = "connected"; + } + catch (AggregateException e) + { + outcome = e.InnerException!.GetType().Name; + } + } +#pragma warning restore SER009 + + Assert.Equal("ObjectDisposedException, sockets left open: 0", $"{outcome}, sockets left open: {FdCount.Stable() - before}"); + }); + runner.Test("redis over SE.Redis: TLS is refused with the transport's reason, not a timeout", () => { string body = Get(redis, run, "/tls"); diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..2138ef0d 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -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(); diff --git a/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs b/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs new file mode 100644 index 00000000..cfee834b --- /dev/null +++ b/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs @@ -0,0 +1,72 @@ +using System.Runtime.CompilerServices; +using ioxide; + +namespace Ioxide.Tests; + +/// +/// 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. +/// +/// +/// The real returns, on a reactor that never runs, with this test setting the flag the end of Teardown sets. +/// +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); + } + } +}