fix(galaxy): GalaxyMxSession.CloseAsync(ct) bounds the gateway CloseSession

The SDK's DisposeAsync closes with no token, i.e. under the 30 s DefaultCallTimeout, so a
blackholed gateway parked the driver's teardown for the whole of it. CloseAsync(ct) runs the
close under the caller's token, drops the channel on a cancelled/failed close (the gateway
reaps the session on its idle lease) and leaves the session re-openable. A close followed by
the final dispose closes the gateway session once.

Refs #735

Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 13:29:11 -04:00
parent 6dedf5158f
commit 05a0e51fc1
2 changed files with 140 additions and 10 deletions
@@ -60,6 +60,12 @@ public sealed class GalaxyMxSession : IAsyncDisposable
/// </summary>
internal Func<CancellationToken, Task>? OpenAndRegisterOverrideForTests { get; set; }
/// <summary>
/// Test seam — replaces the gateway <c>CloseSession</c> round-trip in <see cref="CloseAsync"/>
/// so the bounded-close behaviour can be exercised against a never-answering fake.
/// </summary>
internal Func<CancellationToken, Task>? CloseOverrideForTests { get; set; }
/// <summary>
/// Connect the underlying gateway client + open an MXAccess session + register the
/// configured client name. Idempotent — second calls are no-ops while a connect has
@@ -96,7 +102,7 @@ public sealed class GalaxyMxSession : IAsyncDisposable
{
// A partial open (e.g. OpenSession succeeded but Register threw) must not leave a half-open
// handle that blocks the next reconnect. Tear the partial state down so a retry rebuilds cleanly.
await TeardownSessionAsync().ConfigureAwait(false);
await TeardownSessionAsync(CancellationToken.None).ConfigureAwait(false);
throw;
}
}
@@ -112,10 +118,23 @@ public sealed class GalaxyMxSession : IAsyncDisposable
public async Task RecreateAsync(MxGatewayClientOptions clientOptions, CancellationToken cancellationToken)
{
ObjectDisposedException.ThrowIf(_disposed, this);
await TeardownSessionAsync().ConfigureAwait(false);
await TeardownSessionAsync(cancellationToken).ConfigureAwait(false);
await ConnectAsync(clientOptions, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Close the gateway session and release the owned client under
/// <paramref name="cancellationToken"/>. The SDK's <c>DisposeAsync</c> closes with NO token,
/// i.e. under the 30 s <c>DefaultCallTimeout</c> — against a blackholed gateway that parks
/// the driver's <c>ShutdownAsync</c> (and the host's <c>PostStop</c>) for the full timeout.
/// A close that is cancelled or fails is NOT retried through the SDK dispose (which would
/// park again); the channel is dropped and the gateway reaps the session on its idle lease.
/// The session can be re-opened afterwards (this does not set <c>_disposed</c>).
/// </summary>
/// <param name="cancellationToken">Bounds the gateway <c>CloseSession</c> round-trip.</param>
/// <returns>A task that completes once the session and client are released.</returns>
public Task CloseAsync(CancellationToken cancellationToken) => TeardownSessionAsync(cancellationToken);
/// <summary>
/// Test seam — attach a session opened externally (e.g. against an in-process gw
/// fake). Skips the gateway-client construction so tests can drive the session
@@ -143,24 +162,45 @@ public sealed class GalaxyMxSession : IAsyncDisposable
{
if (_disposed) return;
_disposed = true;
await TeardownSessionAsync().ConfigureAwait(false);
await TeardownSessionAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Resets the connect flag and best-effort disposes + nulls the owned session and client.
/// Shared by <see cref="DisposeAsync"/> (final teardown) and <see cref="RecreateAsync"/> /
/// the partial-open recovery path (rebuild teardown) — it does NOT set <c>_disposed</c>, so
/// a torn-down session can be re-opened.
/// Resets the connect flag and best-effort closes + nulls the owned session and client.
/// Shared by <see cref="DisposeAsync"/> (final teardown), <see cref="CloseAsync"/> (bounded
/// teardown) and <see cref="RecreateAsync"/> / the partial-open recovery path (rebuild
/// teardown) — it does NOT set <c>_disposed</c>, so a torn-down session can be re-opened.
/// </summary>
private async Task TeardownSessionAsync()
private async Task TeardownSessionAsync(CancellationToken cancellationToken)
{
var wasConnected = _connected;
_connected = false;
// The override models the gateway round-trip, so it runs only when there was a live
// session to close — a second teardown (CloseAsync then DisposeAsync) must not close twice.
var closeOverride = CloseOverrideForTests;
if (closeOverride is not null && wasConnected)
{
try { await closeOverride(cancellationToken).ConfigureAwait(false); }
catch (Exception ex) { _logger.LogWarning(ex, "GalaxyMxSession close failed or timed out (best-effort)"); }
}
if (_session is not null)
{
try { await _session.DisposeAsync().ConfigureAwait(false); }
catch (Exception ex) { _logger.LogWarning(ex, "GalaxyMxSession session dispose failed (best-effort)"); }
var session = _session;
_session = null;
try
{
// CloseAsync(ct) is the bounded gateway round-trip; DisposeAsync only re-runs an
// un-replied close (with no token), so it is reached only after a successful one.
await session.CloseAsync(cancellationToken).ConfigureAwait(false);
await session.DisposeAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogWarning(ex,
"GalaxyMxSession close failed or timed out (best-effort) — dropping the channel; the gateway reaps the session on its idle lease.");
}
}
if (_ownedClient is not null)