From 7e9a241fe87884c9dc441efb4ef533aa9b636221 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Mon, 19 Oct 2026 19:18:28 +0200 Subject: [PATCH 1/3] Clear AUTHINFO command buffers before pool return Zero credential-bearing command buffers before returning them to the array pool, including when writes fail or are cancelled. Clear the separate encoded credential copy and add regression coverage for buffer cleanup, wire framing, and log redaction. --- .../Nntp/NntpProviderSession.cs | 39 ++- .../Nntp/NntpProviderSessionTests.cs | 262 ++++++++++++++++++ .../TestDoubles/ScriptedNntpTransport.cs | 49 +++- 3 files changed, 346 insertions(+), 4 deletions(-) diff --git a/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderSession.cs b/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderSession.cs index 7b4e7ce8..056c7e21 100644 --- a/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderSession.cs +++ b/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderSession.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Net.Sockets; using System.Security.Authentication; +using System.Security.Cryptography; using System.Runtime.ExceptionServices; using System.Net.Security; using VectorNNTPD.Common.Articles; @@ -103,6 +104,12 @@ internal sealed class NntpProviderSession : IAsyncDisposable /// private Func? _beforeArticleBusyWait; + /// + /// Pool for command buffers. + /// Production uses . + /// + private ArrayPool _prefixedCommandPool = ArrayPool.Shared; + /// Set when a command failure must keep the session out of the idle pool. Sticky for the session lifetime. private bool _unhealthy; @@ -180,6 +187,25 @@ internal Func? BeforeArticleBusyWait set => _beforeArticleBusyWait = value; } + /// + /// Pool that rents the buffer for AUTHINFO USER and AUTHINFO PASS. + /// Production leaves . + /// + /// + /// A test replaces this before connect so it can observe the buffer that returns. + /// The shared pool stays the default. The argument bytes are credential material and are not logged. + /// + /// The assigned pool is null. + internal ArrayPool PrefixedCommandPool + { + get => _prefixedCommandPool; + set + { + ArgumentNullException.ThrowIfNull(value); + _prefixedCommandPool = value; + } + } + /// /// Pauses STARTTLS after the transport is captured and before uses it. /// Null unless a test is holding that window. @@ -1453,9 +1479,14 @@ private ArticleRetrievalResult ClassifyAuthFailure(int code, string text) /// Writes , , and CRLF, logging instead of the argument. /// Command prefix, such as . - /// ASCII field. Not written to the log. + /// ASCII credential field. Cleared after it is copied into the rented command. Not written to the log. /// Debug TX text. Callers pass a redacted command for credentials. /// Forwarded to . + /// + /// The rented buffer stays alive until completes, faults, or is cancelled. + /// The populated prefix, argument, and CRLF are then cleared, and the whole rental is returned with clearing enabled. + /// A pool that honors clearArray therefore cannot retain the credential in the slack past that prefix. + /// private async Task WritePrefixedAsync( byte[] prefix, byte[] argument, @@ -1463,17 +1494,19 @@ private async Task WritePrefixedAsync( CancellationToken cancellationToken) { var length = prefix.Length + argument.Length + NntpProtocolIo.Crlf.Length; - var rented = ArrayPool.Shared.Rent(length); + var rented = _prefixedCommandPool.Rent(length); try { prefix.CopyTo(rented.AsSpan()); argument.CopyTo(rented.AsSpan(prefix.Length)); NntpProtocolIo.Crlf.CopyTo(rented.AsSpan(prefix.Length + argument.Length)); + CryptographicOperations.ZeroMemory(argument); await WriteCommandAsync(wireCommand, rented.AsMemory(0, length), cancellationToken).ConfigureAwait(false); } finally { - ArrayPool.Shared.Return(rented); + CryptographicOperations.ZeroMemory(rented.AsSpan(0, length)); + _prefixedCommandPool.Return(rented, clearArray: true); } } diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpProviderSessionTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpProviderSessionTests.cs index ec8ea047..fd2bc449 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpProviderSessionTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/NntpProviderSessionTests.cs @@ -1,3 +1,4 @@ +using System.Buffers; using System.Diagnostics; using System.Net.Security; using System.Net.Sockets; @@ -148,6 +149,146 @@ public async Task Authentication_success_and_failure() await fail.DisposeAsync(); } + /// + /// AUTHINFO PASS is the expected CRLF command on the wire, and the rented buffer is clear when it is returned. + /// Debug logs keep the redacted command. + /// + [Fact] + public async Task Authinfo_pass_buffer_is_clear_when_returned() + { + const string Password = "pool-clear-secret"; + var pool = new CredentialCommandPool(); + var logger = new CollectingLogger(); + var factory = new ScriptedNntpTransportFactory(); + var server = new ScriptedNntpServer(); + server.Respond(static command => command.StartsWith("AUTHINFO USER", StringComparison.Ordinal) + ? "381 password required\r\n" + : "281 ok\r\n"); + factory.Enqueue(server); + var session = CreateSession(user: "acct", password: Password, logger: logger); + session.PrefixedCommandPool = pool; + try + { + Assert.Null(await session.ConnectAsync(factory, CancellationToken.None)); + Assert.Equal( + ["CAPABILITIES", "AUTHINFO USER acct", "AUTHINFO PASS " + Password], + server.Commands); + Assert.True(server.LastCommandUsedCrlf); + Assert.Equal(2, pool.RentCount); + Assert.Equal(2, pool.ReturnCount); + AssertClearedCredential(pool, 0, Password); + AssertClearedCredential(pool, 1, Password); + Assert.Equal( + NntpProtocolIo.AuthInfoPassPrefix.Length + Password.Length + NntpProtocolIo.Crlf.Length, + pool.RequestedLength(1)); + Assert.Contains(logger.Messages, static message => message.Contains("TX: AUTHINFO PASS ***", StringComparison.Ordinal)); + Assert.DoesNotContain(logger.Messages, message => message.Contains(Password, StringComparison.Ordinal)); + } + finally + { + await session.DisposeAsync(); + } + } + + /// A failed AUTHINFO PASS write still clears and returns the rented buffer once. + [Fact] + public async Task Authinfo_pass_write_fault_still_clears_the_buffer() + { + const string Password = "pool-clear-secret"; + var pool = new CredentialCommandPool(); + var factory = new ScriptedNntpTransportFactory(); + var server = new ScriptedNntpServer + { + FaultClientAuthInfoPassWrite = true, + }; + server.Respond(static command => command.StartsWith("AUTHINFO USER", StringComparison.Ordinal) + ? "381 password required\r\n" + : "281 ok\r\n"); + factory.Enqueue(server); + var session = CreateSession(user: "acct", password: Password); + session.PrefixedCommandPool = pool; + try + { + var result = await session.ConnectAsync(factory, CancellationToken.None); + Assert.Equal(ArticleRetrievalKind.ProviderFailure, result!.Kind); + Assert.Equal(nameof(IOException), result.Reason); + Assert.DoesNotContain(Password, result.Reason, StringComparison.Ordinal); + Assert.Equal("AUTHINFO USER acct", server.Commands[1]); + Assert.DoesNotContain( + server.Commands, + static command => command.StartsWith("AUTHINFO PASS", StringComparison.Ordinal)); + Assert.Equal(2, pool.RentCount); + Assert.Equal(2, pool.ReturnCount); + AssertClearedCredential(pool, 0, Password); + AssertClearedCredential(pool, 1, Password); + } + finally + { + await session.DisposeAsync(); + } + } + + /// + /// Cancellation during AUTHINFO PASS returns the buffer only after the write has left it, and the bytes are then clear. + /// + [Fact] + public async Task Authinfo_pass_cancellation_clears_the_buffer_after_the_write() + { + const string Password = "pool-clear-secret"; + var pool = new CredentialCommandPool(); + var factory = new ScriptedNntpTransportFactory(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var server = new ScriptedNntpServer + { + HoldAuthInfoPassWrite = release, + AuthInfoPassWriteEntered = entered, + }; + server.Respond(static command => command.StartsWith("AUTHINFO USER", StringComparison.Ordinal) + ? "381 password required\r\n" + : "281 ok\r\n"); + factory.Enqueue(server); + var session = CreateSession(user: "acct", password: Password); + session.PrefixedCommandPool = pool; + using var cancel = new CancellationTokenSource(); + try + { + var connect = session.ConnectAsync(factory, cancel.Token); + await entered.Task.WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Equal(2, pool.RentCount); + Assert.Equal(1, pool.ReturnCount); + AssertClearedCredential(pool, 0, Password); + Assert.True(pool.Rented(1).AsSpan().IndexOf(Encoding.ASCII.GetBytes(Password)) >= 0); + cancel.Cancel(); + var result = await connect.WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Equal(ArticleRetrievalKind.Cancelled, result!.Kind); + Assert.Equal(2, pool.ReturnCount); + AssertClearedCredential(pool, 1, Password); + Assert.DoesNotContain( + server.Commands, + static command => command.StartsWith("AUTHINFO PASS", StringComparison.Ordinal)); + } + finally + { + release.TrySetResult(); + await session.DisposeAsync(); + } + } + + /// + /// with clearing zeroes the whole rented array, including slack past the request. + /// + [Fact] + public void ArrayPool_return_with_clearArray_zeroes_the_entire_rental() + { + var pool = ArrayPool.Create(); + var rented = pool.Rent(8); + rented.AsSpan().Fill(0x5A); + Assert.True(rented.Length >= 8); + pool.Return(rented, clearArray: true); + Assert.All(rented, static value => Assert.Equal(0, value)); + } + /// /// User without password fails AuthenticationFailure without AUTHINFO on the wire. /// @@ -1880,6 +2021,127 @@ public override ValueTask DisposeAsync() /// Plaintext transport whose first handshake read waits until that read is cancelled. /// Later writes pass through so the client hello can leave the socket. /// + /// Asserts one returned command buffer no longer contains . + /// Pool that observed the return. + /// Return order. Zero is AUTHINFO USER. + /// Credential that must be absent. Not written to the assertion message. + private static void AssertClearedCredential(CredentialCommandPool pool, int index, string password) + { + Assert.True(pool.ClearArray(index)); + Assert.All(pool.PrefixAtReturn(index), static value => Assert.Equal(0, value)); + var secret = Encoding.ASCII.GetBytes(password); + Assert.True(pool.Rented(index).AsSpan().IndexOf(secret) < 0); + } + + /// + /// Records the buffers rents and the bytes present when they are returned. + /// Does not consult . + /// + private sealed class CredentialCommandPool : ArrayPool + { + /// Guards the rental lists. + private readonly Lock _gate = new(); + + /// Arrays rented, in rent order. + private readonly List _rented = []; + + /// Requested lengths, in rent order. + private readonly List _requested = []; + + /// Arrays still rented. + private readonly HashSet _outstanding = new(ReferenceEqualityComparer.Instance); + + /// clearArray values in return order. + private readonly List _clearArray = []; + + /// Copies of each populated prefix at the moment of return, in return order. + private readonly List _prefixAtReturn = []; + + /// Calls to . + internal int RentCount { get; private set; } + + /// Calls to . + internal int ReturnCount { get; private set; } + + /// The array from rent . + /// Zero-based rent order. + /// The rented array. + internal byte[] Rented(int index) + { + lock (_gate) + { + return _rented[index]; + } + } + + /// The length requested for rent . + /// Zero-based rent order. + /// The minimum length passed to . + internal int RequestedLength(int index) + { + lock (_gate) + { + return _requested[index]; + } + } + + /// Whether return asked for the array to be cleared. + /// Zero-based return order. + /// when clearArray was set. + internal bool ClearArray(int index) + { + lock (_gate) + { + return _clearArray[index]; + } + } + + /// The populated prefix copied when return was called. + /// Zero-based return order. + /// A copy taken before this pool mutates the array. + internal byte[] PrefixAtReturn(int index) + { + lock (_gate) + { + return _prefixAtReturn[index]; + } + } + + /// + public override byte[] Rent(int minimumLength) + { + var array = new byte[minimumLength + 8]; + Array.Fill(array, (byte)0xA5); + lock (_gate) + { + RentCount++; + _rented.Add(array); + _requested.Add(minimumLength); + _outstanding.Add(array); + } + + return array; + } + + /// + public override void Return(byte[] array, bool clearArray = false) + { + ArgumentNullException.ThrowIfNull(array); + lock (_gate) + { + if (!_outstanding.Remove(array)) + { + throw new InvalidOperationException("Returned a command buffer that was not outstanding."); + } + + var index = _rented.IndexOf(array); + ReturnCount++; + _clearArray.Add(clearArray); + _prefixAtReturn.Add(array.AsSpan(0, _requested[index]).ToArray()); + } + } + } + private sealed class StalledPlaintextStream : Stream { /// Transport wrapped for the scripted server. diff --git a/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ScriptedNntpTransport.cs b/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ScriptedNntpTransport.cs index 0b022053..60d9cb90 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ScriptedNntpTransport.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ScriptedNntpTransport.cs @@ -250,6 +250,24 @@ internal ScriptedNntpServer(string greeting = "200 posting allowed") /// public bool FaultClientDateWrite { get; set; } + /// + /// When set, the client stream throws on AUTHINFO PASS before any of those bytes are written. + /// The exception message does not include the password. + /// + public bool FaultClientAuthInfoPassWrite { get; set; } + + /// + /// When set, the first AUTHINFO PASS write waits here after the command bytes are still only in the caller buffer. + /// The wait observes the write cancellation token. + /// + public TaskCompletionSource? HoldAuthInfoPassWrite { get; set; } + + /// Set when an AUTHINFO PASS write has entered . + public TaskCompletionSource? AuthInfoPassWriteEntered { get; set; } + + /// Zero until accepts the first AUTHINFO PASS write. + private int _authInfoPassWriteHoldStarted; + /// /// When set, the client stream throws on a QUIT write. /// Connect and other commands are unaffected. @@ -335,6 +353,20 @@ internal bool TryConsumeDisposeFault() return FaultClientDispose && Interlocked.Exchange(ref _disposeFaultUsed, 1) == 0; } + /// + /// Accepts the first AUTHINFO PASS write while is set. + /// + /// when this write must wait before copying bytes to the server pipe. + internal bool TryBeginAuthInfoPassWriteHold() + { + if (HoldAuthInfoPassWrite is null) + { + return false; + } + + return Interlocked.Exchange(ref _authInfoPassWriteHoldStarted, 1) == 0; + } + /// /// Accepts the first non-QUIT client write while is set. /// @@ -696,7 +728,8 @@ public override void Write(byte[] buffer, int offset, int count) => /// /// - /// Honors , , and + /// Honors , , + /// , , and /// on the optional owner. /// A held QUIT waits on that gate and on before any bytes are written. /// A QUIT fault throws before any bytes are written. @@ -722,6 +755,20 @@ public override async ValueTask WriteAsync(ReadOnlyMemory buffer, Cancella throw new NotSupportedException("DATE write fault"); } + if (owner is not null + && buffer.Span.StartsWith("AUTHINFO PASS "u8) + && owner.TryBeginAuthInfoPassWriteHold()) + { + owner.AuthInfoPassWriteEntered?.TrySetResult(); + await owner.HoldAuthInfoPassWrite!.Task.WaitAsync(cancellationToken).ConfigureAwait(false); + } + + if (owner?.FaultClientAuthInfoPassWrite == true + && buffer.Span.StartsWith("AUTHINFO PASS "u8)) + { + throw new IOException("AUTHINFO PASS write fault"); + } + writer.Write(buffer.Span); await writer.FlushAsync(cancellationToken).ConfigureAwait(false); if (owner is not null From d39186cbc752fca7be2e2f8b758c99bc11dd605e Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Mon, 19 Oct 2026 20:05:45 +0200 Subject: [PATCH 2/3] Add diagnostics for provider session establishment failures Capture structured per-attempt establishment outcomes, failure reasons, elapsed times, and pool counts when concurrency assertions fail. Preserve existing assertions and production behavior to help diagnose intermittent Linux CI failures. --- .../ProviderSessionEstablishmentGateTests.cs | 330 +++++++++++++++++- 1 file changed, 325 insertions(+), 5 deletions(-) diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs index ff541ce7..8d34ea26 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs @@ -1,7 +1,13 @@ +using System.Diagnostics; +using System.Globalization; +using System.Text; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.BackFillerService.Accounts; using VectorNNTPD.BackFillerService.Nntp; using VectorNNTPD.BackFillerService.Tests.TestDoubles; +using Xunit.Abstractions; +using Xunit.Sdk; namespace VectorNNTPD.BackFillerService.Tests.Nntp { @@ -11,6 +17,16 @@ namespace VectorNNTPD.BackFillerService.Tests.Nntp public sealed class ProviderSessionEstablishmentGateTests { + /// Receives the establishment trace when a steady-state count assertion fails. + private readonly ITestOutputHelper _output; + + /// Captures for the steady-state failure trace. + /// xUnit output written only when that assertion fails. + public ProviderSessionEstablishmentGateTests(ITestOutputHelper output) + { + _output = output; + } + /// /// Shared gate limits concurrent connects across three pools during warm-up. /// @@ -72,24 +88,63 @@ public async Task Per_provider_MaxSessions_still_limits_steady_state_capacity() /// /// After warm-up, total sessions can exceed gate limit sequentially. /// + /// + /// A short active count still fails . + /// The original assertion is rethrown. Its xUnit output then lists each attempt's connection, + /// terminal result, failure reason, and elapsed time, plus the final pool counts. + /// [Fact] public async Task Steady_state_is_not_globally_capped_after_establishment() { var factory = new ScriptedNntpTransportFactory(); + var logA = new EstablishmentAttemptLog(); + var logB = new EstablishmentAttemptLog(); NntpSessionPoolTests.EnqueueReadyServers(factory, count: 6); using var gate = new ProviderSessionEstablishmentGate(1); - await using var poolA = NntpSessionPoolTests.CreatePool(factory, max: 3, establishment: gate, backbone: "A"); - await using var poolB = NntpSessionPoolTests.CreatePool(factory, max: 3, establishment: gate, backbone: "B"); + await using var poolA = NntpSessionPoolTests.CreatePool(factory, max: 3, establishment: gate, backbone: "A", logger: logA); + await using var poolB = NntpSessionPoolTests.CreatePool(factory, max: 3, establishment: gate, backbone: "B", logger: logB); await Task.WhenAll( poolA.EnsureDesiredSessionsAsync(CancellationToken.None), poolB.EnsureDesiredSessionsAsync(CancellationToken.None)); - Assert.Equal(3, poolA.ActiveSessionCount); - Assert.Equal(3, poolB.ActiveSessionCount); - Assert.Equal(1, factory.MaxObservedConcurrentConnects); + try + { + Assert.Equal(3, poolA.ActiveSessionCount); + Assert.Equal(3, poolB.ActiveSessionCount); + Assert.Equal(1, factory.MaxObservedConcurrentConnects); + } + catch (XunitException) + { + _output.WriteLine(DescribeFailure(factory, poolA, logA, poolB, logB)); + throw; + } + } + + /// + /// Failure text names the attempt and its reason, and does not keep an AUTHINFO argument. + /// + [Fact] + public void Establishment_diagnostic_names_the_failed_attempt_and_redacts_credentials() + { + var log = new EstablishmentAttemptLog(); + NntpLogMessages.SessionEstablishmentWaiting(log, "A", 1); + NntpLogMessages.SessionEstablishmentStarted(log, "A", 1); + NntpLogMessages.SessionEstablishmentCompleted(log, "A", 1); + NntpLogMessages.SessionEstablishmentWaiting(log, "A", 2); + NntpLogMessages.SessionEstablishmentStarted(log, "A", 2); + NntpLogMessages.SessionEstablishmentFailed(log, "A", 2, "NNTP connect timed out."); + NntpLogMessages.SessionEstablishmentFailed(log, "A", 3, "AUTHINFO PASS secret-value"); + + var text = log.Describe(); + + Assert.Contains("connection=1 result=ready", text, StringComparison.Ordinal); + Assert.Contains("connection=2 result=failed reason=NNTP connect timed out.", text, StringComparison.Ordinal); + Assert.Contains("connection=3 result=failed reason=AUTHINFO PASS ***", text, StringComparison.Ordinal); + Assert.Contains("elapsedMs=", text, StringComparison.Ordinal); + Assert.DoesNotContain("secret-value", text, StringComparison.Ordinal); } /// @@ -306,5 +361,270 @@ private static async Task WaitForAsync(Func condition, TimeSpan timeout) await Task.Yield(); } } + + /// Formats pool counts and per-attempt establishment results after a failed assertion. + /// Shared scripted transport. Connects are counted per backbone. + /// First warm-up pool. + /// Establishment log for . + /// Second warm-up pool. + /// Establishment log for . + /// Failure output. AUTHINFO arguments are redacted. + private static string DescribeFailure( + ScriptedNntpTransportFactory factory, + NntpSessionPool poolA, + EstablishmentAttemptLog logA, + NntpSessionPool poolB, + EstablishmentAttemptLog logB) + { + var builder = new StringBuilder(); + AppendPool(builder, factory, poolA, logA); + AppendPool(builder, factory, poolB, logB); + builder.Append("maxConcurrentConnects="); + builder.AppendLine(factory.MaxObservedConcurrentConnects.ToString(CultureInfo.InvariantCulture)); + return builder.ToString(); + } + + /// Appends one pool's final counts and its attempt lines. + /// Failure text being built. + /// Transport whose connects are counted for this backbone. + /// Pool observed after warm-up. + /// Structured establishment log for . + private static void AppendPool( + StringBuilder builder, + ScriptedNntpTransportFactory factory, + NntpSessionPool pool, + EstablishmentAttemptLog log) + { + var connects = 0; + foreach (var attempt in factory.ConnectAttempts) + { + if (string.Equals(attempt.Backbone, pool.Backbone, StringComparison.Ordinal)) + { + connects++; + } + } + + builder.Append("pool=").Append(pool.Backbone); + builder.Append(" desired=").Append(pool.Provider.MaxSessions.ToString(CultureInfo.InvariantCulture)); + builder.Append(" active=").Append(pool.ActiveSessionCount.ToString(CultureInfo.InvariantCulture)); + builder.Append(" live=").Append(pool.LiveSessionCount.ToString(CultureInfo.InvariantCulture)); + builder.Append(" idle=").Append(pool.IdleSessionCount.ToString(CultureInfo.InvariantCulture)); + builder.Append(" leases=").Append(pool.ActiveLeaseCount.ToString(CultureInfo.InvariantCulture)); + builder.Append(" transportConnects=").AppendLine(connects.ToString(CultureInfo.InvariantCulture)); + builder.Append(log.Describe()); + } + + /// + /// Records structured establishment events for one pool. + /// Wire traffic is not stored. still returns true for Debug because + /// is a Debug event, and that same check + /// also lets the session copy status lines before it logs them. + /// + private sealed class EstablishmentAttemptLog : ILogger + { + private const int WaitingEventId = 5412; + private const int StartedEventId = 5413; + private const int CompletedEventId = 5414; + private const int CancelledEventId = 5415; + private const int FailedEventId = 5416; + + private readonly object _gate = new(); + private readonly Stopwatch _clock = Stopwatch.StartNew(); + private readonly List _events = []; + + /// + public IDisposable BeginScope(TState state) + where TState : notnull => NoScope.Instance; + + /// + public bool IsEnabled(LogLevel logLevel) => logLevel == LogLevel.Debug; + + /// + public void Log( + LogLevel logLevel, + EventId eventId, + TState state, + Exception? exception, + Func formatter) + { + if (eventId.Id is not (WaitingEventId or StartedEventId or CompletedEventId or CancelledEventId or FailedEventId)) + { + return; + } + + if (!TryRead(state, "ConnectionNumber", out var connectionValue) || connectionValue is not int connection) + { + return; + } + + string? reason = null; + if (eventId.Id == FailedEventId + && TryRead(state, "Reason", out var reasonValue) + && reasonValue is string reasonText) + { + reason = Scrub(reasonText); + } + + var observation = new Observation(_clock.Elapsed, eventId.Id, connection, reason); + lock (_gate) + { + _events.Add(observation); + } + } + + /// Formats one line per connection from the structured establishment events. + /// Attempt identity, terminal result, reason, and elapsed milliseconds. + internal string Describe() + { + Observation[] events; + var now = _clock.Elapsed; + lock (_gate) + { + events = [.. _events]; + } + + var attempts = new SortedDictionary(); + foreach (var observation in events) + { + if (!attempts.TryGetValue(observation.Connection, out var attempt)) + { + attempt = new Attempt(observation.Connection); + attempts.Add(observation.Connection, attempt); + } + + attempt.Apply(observation); + } + + var builder = new StringBuilder(); + foreach (var attempt in attempts.Values) + { + builder.AppendLine(attempt.Summarize(now)); + } + + return builder.ToString(); + } + + private static bool TryRead(object? state, string name, out object? value) + { + if (state is IEnumerable> pairs) + { + foreach (var pair in pairs) + { + if (string.Equals(pair.Key, name, StringComparison.Ordinal)) + { + value = pair.Value; + return true; + } + } + } + + value = null; + return false; + } + + private static string Scrub(string reason) + { + reason = ScrubCommand(reason, "AUTHINFO USER "); + return ScrubCommand(reason, "AUTHINFO PASS "); + } + + private static string ScrubCommand(string reason, string marker) + { + var index = reason.IndexOf(marker, StringComparison.Ordinal); + if (index < 0) + { + return reason; + } + + var valueStart = index + marker.Length; + if (reason.AsSpan(valueStart).StartsWith("***", StringComparison.Ordinal)) + { + return reason; + } + + var valueEnd = valueStart; + while (valueEnd < reason.Length && !char.IsWhiteSpace(reason[valueEnd])) + { + valueEnd++; + } + + return string.Concat(reason.AsSpan(0, valueStart), "***", reason.AsSpan(valueEnd)); + } + + private readonly record struct Observation(TimeSpan Elapsed, int EventId, int Connection, string? Reason); + + private sealed class Attempt(int connection) + { + private TimeSpan? _origin; + private TimeSpan? _terminal; + private string _result = "incomplete"; + private string? _reason; + + internal string Summarize(TimeSpan now) + { + var end = _terminal ?? now; + var elapsed = _origin is { } origin ? end - origin : TimeSpan.Zero; + var text = new StringBuilder(); + text.Append("attempt connection=").Append(connection.ToString(CultureInfo.InvariantCulture)); + text.Append(" result=").Append(_result); + if (!string.IsNullOrEmpty(_reason)) + { + text.Append(" reason=").Append(_reason); + } + + text.Append(" elapsedMs=").Append(FormatMilliseconds(elapsed)); + return text.ToString(); + } + + internal void Apply(Observation observation) + { + _origin ??= observation.Elapsed; + switch (observation.EventId) + { + case CompletedEventId: + SetTerminal("ready", observation.Elapsed, reason: null); + break; + case CancelledEventId: + SetTerminal("cancelled", observation.Elapsed, reason: null); + break; + case FailedEventId: + SetTerminal("failed", observation.Elapsed, observation.Reason); + break; + } + } + + private void SetTerminal(string result, TimeSpan elapsed, string? reason) + { + if (_terminal is not null) + { + return; + } + + _terminal = elapsed; + _result = result; + _reason = reason; + } + } + + private static string FormatMilliseconds(TimeSpan elapsed) + { + var milliseconds = elapsed.TotalMilliseconds; + if (milliseconds < 0) + { + milliseconds = 0; + } + + return Math.Round(milliseconds).ToString("0", CultureInfo.InvariantCulture); + } + + private sealed class NoScope : IDisposable + { + internal static NoScope Instance { get; } = new(); + + public void Dispose() + { + } + } + } } } From cecd3e84a40ee51aada3437509657d2437d2861f Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Mon, 19 Oct 2026 20:21:12 +0200 Subject: [PATCH 3/3] Simplify provider establishment connect counting Replace the manually filtered loop with predicate-based Count while preserving ordinal backbone matching and failure diagnostics. --- .../Nntp/ProviderSessionEstablishmentGateTests.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs index 8d34ea26..3f5ecb44 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/ProviderSessionEstablishmentGateTests.cs @@ -395,14 +395,8 @@ private static void AppendPool( NntpSessionPool pool, EstablishmentAttemptLog log) { - var connects = 0; - foreach (var attempt in factory.ConnectAttempts) - { - if (string.Equals(attempt.Backbone, pool.Backbone, StringComparison.Ordinal)) - { - connects++; - } - } + var connects = factory.ConnectAttempts.Count(attempt => + string.Equals(attempt.Backbone, pool.Backbone, StringComparison.Ordinal)); builder.Append("pool=").Append(pool.Backbone); builder.Append(" desired=").Append(pool.Provider.MaxSessions.ToString(CultureInfo.InvariantCulture));