fix(gateway): GatewaySession implements IAsyncDisposable; drop dead KillWorker(string) (mxaccessgw#191)

R21-O12: GatewaySession already owned an async DisposeAsync teardown but did not
declare IAsyncDisposable, so anything holding it as IAsyncDisposable (or a DI
container) could not find the teardown. Declaring it changes no call site;
the existing await-using sites bound to the same method by pattern.

R21-O13: KillWorker(string) had no callers and was an ungated kill that
bypassed _closeLock. Deleted it; docs/Sessions.md and the test comments now
name KillWorkerWithCloseGateAsync, the real kill path.

Regression test: GatewaySessionTests.GatewaySession_ImplementsIAsyncDisposable
(fails before the interface is declared).

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 12:29:15 -05:00
parent d56353bf08
commit 11d8f76c5f
4 changed files with 26 additions and 16 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ Tags gate **visibility only**: which sessions' event metadata a dashboard user m
`CloseSessionAsync` and `KillWorkerAsync` are both end-of-life paths but differ in what they offer the worker: `CloseSessionAsync` and `KillWorkerAsync` are both end-of-life paths but differ in what they offer the worker:
- `CloseSessionAsync` is the graceful path: it calls `GatewaySession.CloseAsync`, which asks the worker to shut down via `IWorkerClient.ShutdownAsync` and only kills the process as a fallback if shutdown fails. - `CloseSessionAsync` is the graceful path: it calls `GatewaySession.CloseAsync`, which asks the worker to shut down via `IWorkerClient.ShutdownAsync` and only kills the process as a fallback if shutdown fails.
- `KillWorkerAsync` is the forceful path used by the dashboard's admin Kill button: it calls `GatewaySession.KillWorker` directly, which kills the worker process immediately with no graceful-shutdown attempt and transitions the session to `Closed`. - `KillWorkerAsync` is the forceful path used by the dashboard's admin Kill button: it calls `GatewaySession.KillWorkerWithCloseGateAsync`, which takes the per-session close gate (so it serializes with a concurrent `CloseAsync`), kills the worker process immediately with no graceful-shutdown attempt, and transitions the session to `Closed`. There is no ungated kill primitive on `GatewaySession`.
Both paths converge on the same registry/metrics cleanup, so the open-session slot is released and `mxgateway.sessions.closed` is incremented either way. Both paths converge on the same registry/metrics cleanup, so the open-session slot is released and `mxgateway.sessions.closed` is incremented either way.
@@ -11,7 +11,7 @@ using ZB.MOM.WW.MxGateway.Server.Workers;
namespace ZB.MOM.WW.MxGateway.Server.Sessions; namespace ZB.MOM.WW.MxGateway.Server.Sessions;
public sealed class GatewaySession public sealed class GatewaySession : IAsyncDisposable
{ {
// Shared untagged sentinel: most sessions carry no dashboard tags. Frozen so the exposed set // Shared untagged sentinel: most sessions carry no dashboard tags. Frozen so the exposed set
// cannot be mutated by a cast — the tag set is a visibility grant, not a scratch collection. // cannot be mutated by a cast — the tag set is a visibility grant, not a scratch collection.
@@ -2050,16 +2050,6 @@ public sealed class GatewaySession
} }
} }
/// <summary>
/// Terminates the worker process immediately.
/// </summary>
/// <param name="reason">Reason for killing the worker.</param>
public void KillWorker(string reason)
{
_workerClient?.Kill(reason);
TransitionTo(SessionState.Closed);
}
/// <summary> /// <summary>
/// Terminates the worker process immediately while holding the per-session /// Terminates the worker process immediately while holding the per-session
/// close lock so concurrent close/kill callers serialize. Returns the /// close lock so concurrent close/kill callers serialize. Returns the
@@ -19,6 +19,26 @@ namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Sessions;
/// </summary> /// </summary>
public sealed class GatewaySessionTests public sealed class GatewaySessionTests
{ {
/// <summary>
/// <see cref="GatewaySession"/> owns async teardown (<c>DisposeAsync</c>) and must
/// advertise it through <see cref="IAsyncDisposable"/> (mxaccessgw#191): a holder that
/// stores it as <see cref="IAsyncDisposable"/>, or a DI container disposing what it
/// tracks, only finds the teardown through the interface — pattern-based
/// <c>await using</c> alone hides that from them.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task GatewaySession_ImplementsIAsyncDisposable()
{
FakeWorkerClient workerClient = new();
object session = CreateReadySession(workerClient);
IAsyncDisposable disposable = Assert.IsAssignableFrom<IAsyncDisposable>(session);
await disposable.DisposeAsync();
Assert.Equal(1, workerClient.DisposeCount);
}
/// <summary> /// <summary>
/// A <c>TransitionTo(Ready)</c> issued after /// A <c>TransitionTo(Ready)</c> issued after
/// <see cref="GatewaySession.CloseAsync"/> has set <see cref="SessionState.Closing"/> /// <see cref="GatewaySession.CloseAsync"/> has set <see cref="SessionState.Closing"/>
@@ -814,7 +814,7 @@ public sealed class SessionManagerTests
/// <summary> /// <summary>
/// Verifies that killing a worker removes the session from the registry without calling shutdown. /// Verifies that killing a worker removes the session from the registry without calling shutdown.
/// Also pins the <c>reason</c> argument propagating through /// Also pins the <c>reason</c> argument propagating through
/// <c>SessionManager.KillWorkerAsync</c> → <c>session.KillWorker(reason)</c> /// <c>SessionManager.KillWorkerAsync</c> → <c>session.KillWorkerWithCloseGateAsync(reason, ct)</c>
/// → <c>IWorkerClient.Kill(reason)</c>. /// → <c>IWorkerClient.Kill(reason)</c>.
/// </summary> /// </summary>
/// <returns>A task that represents the asynchronous operation.</returns> /// <returns>A task that represents the asynchronous operation.</returns>
@@ -896,7 +896,7 @@ public sealed class SessionManagerTests
} }
/// <summary> /// <summary>
/// When <c>session.KillWorker</c> throws, the catch path must still /// When <c>session.KillWorkerWithCloseGateAsync</c> throws, the catch path must still
/// decrement <c>mxgateway.sessions.open</c>. Without the fix the gauge leaks one open session per failed kill. /// decrement <c>mxgateway.sessions.open</c>. Without the fix the gauge leaks one open session per failed kill.
/// </summary> /// </summary>
/// <returns>A task that represents the asynchronous operation.</returns> /// <returns>A task that represents the asynchronous operation.</returns>
@@ -1035,7 +1035,7 @@ public sealed class SessionManagerTests
/// <summary> /// <summary>
/// <c>ShutdownAsync</c>'s graceful-close fallback (which calls /// <c>ShutdownAsync</c>'s graceful-close fallback (which calls
/// <c>KillWorker</c> + <c>RemoveSessionAsync</c> when <c>CloseSessionCoreAsync</c> throws) /// <c>KillWorkerAsync</c> + <c>RemoveSessionAsync</c> when <c>CloseSessionCoreAsync</c> throws)
/// must still account a successful close: both the open-session gauge must drop to zero AND /// must still account a successful close: both the open-session gauge must drop to zero AND
/// the <c>mxgateway.sessions.closed</c> counter must increment. Without the fix, the /// the <c>mxgateway.sessions.closed</c> counter must increment. Without the fix, the
/// graceful-close failure path under-counts the closed counter. Both the shutdown and the /// graceful-close failure path under-counts the closed counter. Both the shutdown and the
@@ -2235,7 +2235,7 @@ public sealed class SessionManagerTests
/// <summary> /// <summary>
/// Gets the last reason argument observed by <see cref="Kill"/>. Pins the /// Gets the last reason argument observed by <see cref="Kill"/>. Pins the
/// reason-string propagation through <c>SessionManager.KillWorkerAsync</c> /// reason-string propagation through <c>SessionManager.KillWorkerAsync</c>
/// → <c>session.KillWorker(reason)</c> → <c>IWorkerClient.Kill(reason)</c>; /// → <c>session.KillWorkerWithCloseGateAsync(reason, ct)</c> → <c>IWorkerClient.Kill(reason)</c>;
/// without this, the chain could silently drop or substitute the reason /// without this, the chain could silently drop or substitute the reason
/// argument and existing tests would still pass. /// argument and existing tests would still pass.
/// </summary> /// </summary>