diff --git a/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionPool.cs b/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionPool.cs index 3b5bfbdd..e8e7061f 100644 --- a/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionPool.cs +++ b/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionPool.cs @@ -52,6 +52,8 @@ internal sealed class NntpSessionPool : IAsyncDisposable /// /// Wakeup signal for waiters. The count is not the pool capacity. /// releases one count only when is positive. + /// A cancelled waiter that drains that count posts it again while another waiter is still counted, + /// so an available permit cannot be left with no wakeup. /// // codeql[cs/missed-using-statement] Owned for the pool lifetime. A using at this declaration would release it when the constructor returns. Shutdown disposes it once, after retirement, and a second dispose returns before that finally. private readonly SemaphoreSlim _leases = new(0); @@ -169,6 +171,18 @@ internal sealed class NntpSessionPool : IAsyncDisposable /// private Func? _beforePermitWaiterDrain; + /// + /// Test gate invoked after a permit waiter is counted and before it waits on . + /// Null in production. The delegate runs outside . + /// + private Func? _beforePermitWait; + + /// + /// Test gate invoked after a permit wait has failed and before that waiter drains a raced release. + /// Null in production. The waiter is still counted. The delegate runs outside . + /// + private Func? _beforeCancelledPermitDrain; + /// Permit waiters still inside when semaphores are about to be disposed. Zero when the drain finished. private int _permitWaitersAtSemaphoreDispose; @@ -586,6 +600,27 @@ internal Func? BeforePermitWaiterDrain set => _beforePermitWaiterDrain = value; } + /// + /// Pauses a permit waiter after it is counted and before . + /// Null unless a test is holding that window. The delegate runs outside . + /// + internal Func? BeforePermitWait + { + get => _beforePermitWait; + set => _beforePermitWait = value; + } + + /// + /// Pauses a failed permit wait after that waiter has left the semaphore and before it drains a raced count. + /// The waiter is still included in . Null unless a test is holding that window. + /// The delegate runs outside . + /// + internal Func? BeforeCancelledPermitDrain + { + get => _beforeCancelledPermitDrain; + set => _beforeCancelledPermitDrain = value; + } + /// /// Permit waiters remaining when dispose is about to drop . /// Zero after the drain has seen every waiter leave . @@ -1327,7 +1362,10 @@ private bool TryTakeAvailablePermit() /// is cancelled while waiting. /// /// The fast path takes only long enough to decrement and increment . - /// The wait itself does not hold that lock. A signal that arrives after cancellation is consumed so it cannot grant a later waiter twice. + /// The wait itself does not hold that lock. + /// A release is paired with a counted waiter. If that waiter fails and drains the release, + /// the same count is posted again while is still positive. It is not posted when no waiter remains, + /// because the permit stays in for the next fast path. /// private async Task AcquirePermitAsync(CancellationToken cancellationToken) { @@ -1348,20 +1386,48 @@ private async Task AcquirePermitAsync(CancellationToken cancellationToken) try { + if (_beforePermitWait is { } beforeWait) + { + await beforeWait().ConfigureAwait(false); + } + await _leases.WaitAsync(cancellationToken).ConfigureAwait(false); } catch (Exception) { - lock (_permitGate) + try { - _permitWaiters--; - try + if (_beforeCancelledPermitDrain is { } beforeDrain) { - _ = _leases.Wait(0); + await beforeDrain().ConfigureAwait(false); } - catch (ObjectDisposedException) + } + finally + { + lock (_permitGate) { - ShutdownObservations.AlreadyDisposed(_logger, "lease permit wait"); + _permitWaiters--; + var consumedRelease = false; + try + { + consumedRelease = _leases.Wait(0); + } + catch (ObjectDisposedException) + { + ShutdownObservations.AlreadyDisposed(_logger, "lease permit wait"); + } + + if (consumedRelease && _permitWaiters > 0) + { + try + { + _leases.Release(); + } + catch (ObjectDisposedException) + { + ShutdownObservations.AlreadyDisposed(_logger, "lease permit release"); + } + } } } diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpSessionPoolTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpSessionPoolTests.cs index b44a0b50..852abafa 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpSessionPoolTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpSessionPoolTests.cs @@ -318,6 +318,123 @@ public async Task Cancelled_permit_wait_and_dispose_do_not_strand_or_leak_permit } } + /// + /// A cancelled permit wait that drains the only wakeup restores it for a waiter already counted. + /// The survivor acquires without a second release, and repeated cycles do not drift the permit counts. + /// + [Fact] + public async Task Cancelled_permit_wait_does_not_consume_the_only_wakeup() + { + var factory = new ScriptedNntpTransportFactory(); + EnqueueReadyServers(factory, count: 1); + var pool = CreatePool(factory, max: 1); + NntpSessionLease? held = null; + Task? cancelled = null; + Task? survivor = null; + Task? follower = null; + var releaseCancelledDrain = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var releaseSurvivorWait = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + try + { + await pool.EnsureDesiredSessionsAsync(CancellationToken.None); + + for (var cycle = 0; cycle < 8; cycle++) + { + held = await pool.AcquireAsync(CancellationToken.None); + Assert.Equal(0, pool.AvailablePermitCount); + Assert.Equal(1, pool.ActiveLeaseCount); + + var waitsEntered = 0; + var survivorWaiting = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var drainEntered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + releaseCancelledDrain = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + releaseSurvivorWait = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using var cancel = new CancellationTokenSource(); + try + { + pool.BeforePermitWait = () => + { + if (Interlocked.Increment(ref waitsEntered) == 2) + { + survivorWaiting.TrySetResult(); + return releaseSurvivorWait.Task; + } + + return Task.CompletedTask; + }; + pool.BeforeCancelledPermitDrain = () => + { + drainEntered.TrySetResult(); + return releaseCancelledDrain.Task; + }; + + cancelled = pool.AcquireAsync(cancel.Token); + await WaitUntilAsync(() => pool.PermitWaiterCount == 1); + survivor = pool.AcquireAsync(CancellationToken.None); + await survivorWaiting.Task.WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Equal(2, pool.PermitWaiterCount); + + cancel.Cancel(); + await drainEntered.Task.WaitAsync(TimeSpan.FromSeconds(2)); + await held.DisposeAsync(); + held = null; + Assert.Equal(1, pool.AvailablePermitCount); + Assert.Equal(0, pool.ActiveLeaseCount); + + releaseCancelledDrain.TrySetResult(); + await Assert.ThrowsAnyAsync(() => cancelled); + Assert.Equal(1, pool.AvailablePermitCount); + Assert.Equal(0, pool.PermitReservationCount); + Assert.Equal(0, pool.ActiveLeaseCount); + pool.BeforeCancelledPermitDrain = null; + pool.BeforePermitWait = null; + + releaseSurvivorWait.TrySetResult(); + var gained = await survivor.WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Equal(1, pool.ActiveLeaseCount); + Assert.Equal(0, pool.AvailablePermitCount); + Assert.Equal(1, pool.PermitReservationCount); + + follower = pool.AcquireAsync(CancellationToken.None); + await WaitUntilAsync(() => pool.PermitWaiterCount == 1); + Assert.False(follower.IsCompleted); + + await gained.DisposeAsync(); + var followed = await follower.WaitAsync(TimeSpan.FromSeconds(2)); + await followed.DisposeAsync(); + follower = null; + survivor = null; + cancelled = null; + Assert.Equal(1, pool.AvailablePermitCount); + Assert.Equal(0, pool.PermitReservationCount); + Assert.Equal(0, pool.ActiveLeaseCount); + Assert.Equal(0, pool.PermitWaiterCount); + } + finally + { + releaseCancelledDrain.TrySetResult(); + releaseSurvivorWait.TrySetResult(); + } + } + } + finally + { + releaseCancelledDrain.TrySetResult(); + releaseSurvivorWait.TrySetResult(); + pool.BeforePermitWait = null; + pool.BeforeCancelledPermitDrain = null; + if (held is not null) + { + await held.DisposeAsync(); + } + + await SettleAcquireAsync(cancelled); + await SettleAcquireAsync(survivor); + await SettleAcquireAsync(follower); + await pool.DisposeAsync(); + } + } + /// /// Released healthy lease reuses the same connection when max is one. ///