fix(sessions): drop per-session event metrics after the session dispose (WRK-008, #253)
This commit is contained in:
@@ -746,9 +746,20 @@ public sealed class SessionManager : ISessionManager
|
||||
return;
|
||||
}
|
||||
|
||||
_metrics.RemoveSessionEvents(session.SessionId);
|
||||
ReleaseSessionSlot();
|
||||
await removedSession.DisposeAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await removedSession.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// After the dispose, not before (WRK-008): on the kill path the worker client's read
|
||||
// loop is cancelled but not awaited, so it can still be inside DispatchEnvelope for a
|
||||
// frame it already read, and its EventReceived would re-create the per-session entry
|
||||
// that nothing removes again. Disposing the session disposes its worker client, which
|
||||
// waits (bounded) for that loop to stop.
|
||||
_metrics.RemoveSessionEvents(session.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseSessionSlot()
|
||||
|
||||
@@ -1939,6 +1939,28 @@ public sealed class SessionManagerTests
|
||||
Assert.Equal(0, metrics.GetSnapshot().OpenSessions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WRK-008: a worker read loop that records one more event while the killed session's
|
||||
/// worker client is being disposed must not leave a per-session metrics entry behind; the
|
||||
/// entry is dropped after the dispose.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task KillWorkerAsync_EventRecordedDuringWorkerDispose_LeavesNoPerSessionEntry()
|
||||
{
|
||||
using GatewayMetrics metrics = new();
|
||||
string? sessionId = null;
|
||||
FakeWorkerClient workerClient = new() { OnDispose = () => metrics.EventReceived(sessionId!, "OnDataChange") };
|
||||
SessionManager manager = CreateManager(new FakeSessionWorkerClientFactory(workerClient), metrics: metrics);
|
||||
GatewaySession session = await manager.OpenSessionAsync(CreateOpenRequest(), "client-1", ownerKeyId: null, CancellationToken.None);
|
||||
sessionId = session.SessionId;
|
||||
|
||||
await manager.KillWorkerAsync(session.SessionId, "test-kill", CancellationToken.None);
|
||||
|
||||
Assert.Equal(1, workerClient.DisposeCount);
|
||||
Assert.False(metrics.GetSnapshot().EventsBySession.ContainsKey(session.SessionId));
|
||||
}
|
||||
|
||||
/// <summary>Creates a session manager for testing.</summary>
|
||||
/// <param name="factory">Worker client factory.</param>
|
||||
/// <param name="registry">Session registry; defaults to a new registry.</param>
|
||||
@@ -2467,9 +2489,16 @@ public sealed class SessionManagerTests
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
DisposeCount++;
|
||||
OnDispose?.Invoke();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a hook run inside <see cref="DisposeAsync"/>, standing in for a worker read loop
|
||||
/// that is still finishing a frame while the client is disposed.
|
||||
/// </summary>
|
||||
public Action? OnDispose { get; init; }
|
||||
|
||||
/// <summary>Waits for shutdown to start on the fake worker client.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public Task WaitForShutdownStartAsync()
|
||||
|
||||
Reference in New Issue
Block a user