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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/clients/ioxide.file/AssetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public ValueTask<int> ReadAsync(int fd, long offset)
/// <summary>
/// Build a per-reactor reader pool and register it as a service
/// (<c>GetService&lt;RingPool&lt;AssetReader&gt;&gt;()</c>). <paramref name="readers"/> bounds
/// concurrent reads; size <paramref name="bufferBytes"/> for the largest asset.
/// concurrent reads; size <paramref name="bufferBytes"/> for the largest asset. The reactor's stop
/// disposes the pool, and with it each reader once it is back.
/// </summary>
public static RingPool<AssetReader> CreatePool(Reactor reactor, int readers, int bufferBytes)
{
Expand All @@ -52,6 +53,7 @@ public static RingPool<AssetReader> CreatePool(Reactor reactor, int readers, int
}

reactor.AddService(pool);
reactor.AddStopper(pool.Dispose);
return pool;
}

Expand Down
10 changes: 6 additions & 4 deletions src/clients/ioxide.httpclient/Http1/HttpClientPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -145,8 +146,7 @@ private async ValueTask<HttpClientConnection> 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
Expand Down Expand Up @@ -332,8 +332,10 @@ private string AcquireTimeoutMessage()
/// <summary>
/// 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
/// <see cref="Release"/> when it finishes, because a disposed pool takes nothing back. Called
/// from another thread, it is queued to the reactor and returns.
/// <see cref="Release"/> when it finishes, because a disposed pool takes nothing back. The requests
/// waiting for a connection, and any made after, fail with <see cref="ObjectDisposedException"/>.
/// Called from another thread, it is queued to the reactor and returns. A pool from
/// <see cref="Start"/> is disposed by its reactor's stop.
/// </summary>
public void Dispose()
{
Expand Down
11 changes: 5 additions & 6 deletions src/clients/ioxide.httpclient/Http1/RingHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -160,8 +158,9 @@ private void Sweep()
}
}

/// <summary>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.</summary>
/// <summary>Close every origin's idle connections and free the TLS context; requests in flight finish
/// first, and those waiting fail with <see cref="ObjectDisposedException"/>, as with the pool. Called
/// from another thread, it is queued to the reactor and returns. Its reactor's stop disposes it.</summary>
public void Dispose()
{
// The origins and their pools are the reactor's.
Expand Down
2 changes: 2 additions & 0 deletions src/clients/ioxide.httpclient/Tls/TlsClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/clients/ioxide.httpclient/Tls/TlsClientStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ internal static async ValueTask<TlsClientStream> 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)
{
Expand Down
11 changes: 6 additions & 5 deletions src/clients/ioxide.pg/PgConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public static async Task<PgConnection> 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)
{
Expand Down
62 changes: 52 additions & 10 deletions src/clients/ioxide.pg/PgPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </remarks>
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.
Expand All @@ -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.
Expand All @@ -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;
}

Expand All @@ -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
}
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -157,6 +169,7 @@ private async ValueTask<PgConnection> WaitForConnectionAsync()
await new ReactorSwitch(_host);
for (int attempt = 1; ; attempt++)
{
ObjectDisposedException.ThrowIf(_disposed, this);
PgConnection? c = Pick();
if (c != null)
{
Expand All @@ -183,6 +196,11 @@ private async ValueTask<PgConnection> 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;
Expand Down Expand Up @@ -210,4 +228,28 @@ private void Sweep()
}

private static int BackoffMs() => 200 + Random.Shared.Next(400); // jittered ~200-600 ms

/// <summary>
/// Close the connections and stop replenishing: queries in flight fail with the connection, and those
/// waiting for one, and any made after, with <see cref="ObjectDisposedException"/>. Called from another
/// thread, it is queued to the reactor and returns. A pool from <see cref="Start"/> is disposed by its
/// reactor's stop.
/// </summary>
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ internal static async ValueTask<IoxideTransport> ConnectAsync(
foreach (IPAddress address in addresses)
{
RingSocket ringSocket = RingSocket.CreateTcp(reactor, address.AddressFamily);
Task<int> connect = ringSocket.ConnectAsync(address, (ushort)port).AsTask();
Task<int> 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
{
Expand Down
41 changes: 38 additions & 3 deletions src/ioxide/Client/RingPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
public sealed class RingPool<T> where T : class
public sealed class RingPool<T> : IDisposable where T : class
{
private readonly Stack<T> _idle = new();
private readonly Queue<TaskCompletionSource<T>> _waiters = new();
private readonly object _gate = new();
private bool _disposed; // under _gate

/// <summary>
/// Rent a resource, waiting (without blocking a thread) if none is idle.
Expand All @@ -21,6 +22,7 @@ public ValueTask<T> RentAsync()
TaskCompletionSource<T> waiter;
lock (_gate)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_idle.TryPop(out T? item))
{
return new ValueTask<T>(item);
Expand All @@ -45,16 +47,49 @@ private static async ValueTask<T> WaitAsync(Task<T> waiting, Reactor? reactor)
/// </summary>
public void Return(T item)
{
TaskCompletionSource<T>? waiter;
TaskCompletionSource<T>? 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
}

/// <summary>
/// Dispose the idle resources, and each one returned from now on; the renters waiting, and any
/// rent made after, fail with <see cref="ObjectDisposedException"/>.
/// </summary>
public void Dispose()
{
T[] idle;
TaskCompletionSource<T>[] waiters;
lock (_gate)
{
_disposed = true;
idle = [.. _idle];
_idle.Clear();
waiters = [.. _waiters];
_waiters.Clear();
}

foreach (TaskCompletionSource<T> waiter in waiters)
{
waiter.SetException(new ObjectDisposedException(GetType().Name));
}
foreach (T item in idle)
{
(item as IDisposable)?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions src/ioxide/Connection/Tcp/TcpConnection.FileRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public sealed unsafe partial class TcpConnection
/// looks for -EPIPE.
/// </returns>
/// <exception cref="InvalidOperationException">A flush is in flight, or another file read is.</exception>
/// <exception cref="ObjectDisposedException">The reactor is stopping.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="len"/> is negative, or so large that the write position plus <paramref name="len"/>
/// passes <see cref="int.MaxValue"/>.
Expand Down
3 changes: 2 additions & 1 deletion src/ioxide/IRingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace ioxide;
///
/// Buffers cross as <see cref="nint"/> 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 <see cref="ObjectDisposedException"/>,
/// and the stop cancels each one it already had.
/// </summary>
public interface IRingHost
{
Expand Down
11 changes: 0 additions & 11 deletions src/ioxide/Native/Native.IoUring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down
Loading
Loading