fix(dcl): bind the reconnect re-subscribe loop to its session; shared supervisory advise ignores the first caller's token (DCL-4, DCL-9, WP3 task 3.3)

This commit is contained in:
Joseph Doherty
2026-12-03 09:57:13 -05:00
parent 2e8bede3b2
commit d52aff1bee
7 changed files with 317 additions and 14 deletions
@@ -47,6 +47,20 @@ public sealed class RealMxGatewayClient : IMxGatewayClient
// completed. A faulted advise is evicted so the next write retries.
private readonly ConcurrentDictionary<int, Lazy<Task>> _supervisoryAdvised = new();
/// <summary>
/// Cancelled on <see cref="DisposeAsync"/>. The SHARED supervisory-advise task runs under
/// this (plus <see cref="SharedSupervisoryAdviseTimeout"/>), never under the token of
/// whichever caller happened to create it (DCL-9).
/// </summary>
private readonly CancellationTokenSource _lifetime = new();
/// <summary>
/// Upper bound on one shared supervisory advise. The gateway session has no command
/// deadline of its own yet, so without a bound a hung advise would wedge every later
/// caller for that handle; a timed-out advise is evicted and the next caller retries.
/// </summary>
internal static readonly TimeSpan SharedSupervisoryAdviseTimeout = TimeSpan.FromSeconds(60);
// With no configured MXAccess write-user this connection behaves as a
// supervisory client: every advise defaults to AdviseSupervisory so writes are
// accepted. A configured write-user keeps the prior plain-advise behaviour.
@@ -517,6 +531,7 @@ public sealed class RealMxGatewayClient : IMxGatewayClient
/// <returns>A task that represents the asynchronous operation.</returns>
public async ValueTask DisposeAsync()
{
_lifetime.Cancel();
if (_session is not null) await _session.DisposeAsync().ConfigureAwait(false);
if (_client is not null) await _client.DisposeAsync().ConfigureAwait(false);
if (_galaxy is not null) await _galaxy.DisposeAsync().ConfigureAwait(false);
@@ -633,27 +648,46 @@ public sealed class RealMxGatewayClient : IMxGatewayClient
/// builds every other command). The result is remembered per handle; a failure is
/// not cached so the next attempt retries rather than wedging the item permanently.
/// </summary>
private async Task EnsureSupervisoryAdvisedAsync(int handle, CancellationToken ct)
internal async Task EnsureSupervisoryAdvisedAsync(int handle, CancellationToken ct)
{
// GetOrAdd + Lazy runs the advise at most once per handle; concurrent
// callers await the same Task and so never reach WriteBulk before it
// completes (default Lazy is thread-safe, factory runs once).
//
// DCL-9: the shared task must NOT capture the creating caller's token — a write's 30 s
// deadline firing would cancel the advise a concurrent, deadline-free subscribe is also
// awaiting, failing that subscribe as a tag-resolution error. The shared advise runs under
// the client lifetime (bounded by SharedSupervisoryAdviseTimeout); each caller applies its
// OWN token to its wait only.
var advise = _supervisoryAdvised.GetOrAdd(
handle,
h => new Lazy<Task>(() => AdviseSupervisoryCoreAsync(h, ct)));
h => new Lazy<Task>(() => RunSharedSupervisoryAdviseAsync(h)));
try
{
await advise.Value.ConfigureAwait(false);
await advise.Value.WaitAsync(ct).ConfigureAwait(false);
}
catch
{
// Evict the faulted entry (only if it's still the one we awaited) so the
// next write retries rather than caching a transient advise failure.
_supervisoryAdvised.TryRemove(new KeyValuePair<int, Lazy<Task>>(handle, advise));
// Evict a faulted/cancelled SHARED entry (only if it's still the one we awaited) so
// the next call retries rather than caching a transient advise failure. A caller that
// merely stopped waiting on its own token leaves the shared advise running for the
// others.
if (advise.Value.IsFaulted || advise.Value.IsCanceled)
_supervisoryAdvised.TryRemove(new KeyValuePair<int, Lazy<Task>>(handle, advise));
throw;
}
}
private async Task RunSharedSupervisoryAdviseAsync(int handle)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_lifetime.Token);
cts.CancelAfter(SharedSupervisoryAdviseTimeout);
await (AdviseCoreOverride ?? AdviseSupervisoryCoreAsync)(handle, cts.Token).ConfigureAwait(false);
}
/// <summary>Test seam: replaces the gateway round trip of one supervisory advise.</summary>
internal Func<int, CancellationToken, Task>? AdviseCoreOverride { get; set; }
private async Task AdviseSupervisoryCoreAsync(int handle, CancellationToken ct)
{
var reply = await _session!.InvokeAsync(