fix(localdb): end open sync streams on ApplicationStopping (lmxopcua#906)
LocalDbSyncService observed only the call token, which ASP.NET Core cancels at the END of Kestrel's graceful drain. A peer's sync stream never ends on its own, so every graceful stop of the passive node waited out the host's whole ShutdownTimeout (30 s on OtOpcUa QA). The service now takes an optional IHostApplicationLifetime (DI supplies it in any generic host): ApplicationStopping is linked into the stream's token and ends the session cleanly, and a stream arriving once stopping has begun is refused Unavailable. Wire-compatible; no proto/schema change. Tests: open stream ends within 5 s of StopApplication, new stream refused Unavailable, control without a lifetime still running; both halves mutation-killed. 262/262. Source only, NOT published (still 0.3.7). Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Channels;
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.LocalDb.Contracts;
|
||||
@@ -35,7 +36,7 @@ public sealed class SyncStreamGuardTests : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private LocalDbSyncService NewService()
|
||||
private LocalDbSyncService NewService(IHostApplicationLifetime? lifetime = null)
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".db");
|
||||
_paths.Add(path);
|
||||
@@ -47,7 +48,8 @@ public sealed class SyncStreamGuardTests : IDisposable
|
||||
return new LocalDbSyncService(
|
||||
db,
|
||||
Options.Create(new ReplicationOptions { FlushInterval = TimeSpan.FromMilliseconds(20) }),
|
||||
NullLoggerFactory.Instance);
|
||||
NullLoggerFactory.Instance,
|
||||
lifetime: lifetime);
|
||||
}
|
||||
|
||||
private static ServerCallContext ContextFor(CancellationToken ct) => new FakeCallContext(ct);
|
||||
@@ -115,6 +117,67 @@ public sealed class SyncStreamGuardTests : IDisposable
|
||||
await SwallowAsync(third);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HostStopping_EndsTheOpenStream_Cleanly()
|
||||
{
|
||||
// lmxopcua#906: the call token fires only at the END of Kestrel's graceful drain, so a stream
|
||||
// that observed nothing else held every graceful stop of the passive node for the whole
|
||||
// host ShutdownTimeout (30 s on QA). ApplicationStopping must end it now — and as an orderly
|
||||
// end of the call, not a fault.
|
||||
var lifetime = new FakeLifetime();
|
||||
var service = NewService(lifetime);
|
||||
|
||||
using var callCts = new CancellationTokenSource(); // never cancelled: the peer is still there
|
||||
var output = new RecordingWriter();
|
||||
var call = service.Sync(new SilentReader(), output, ContextFor(callCts.Token));
|
||||
await output.NextAsync(Timeout);
|
||||
Assert.False(call.IsCompleted);
|
||||
|
||||
lifetime.StopApplication();
|
||||
|
||||
await call.WaitAsync(TimeSpan.FromSeconds(5)); // completes, and without throwing
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HostStopping_RefusesANewStream_Unavailable()
|
||||
{
|
||||
// The peer redials the moment its stream ends; a node already stopping must not admit it.
|
||||
var lifetime = new FakeLifetime();
|
||||
var service = NewService(lifetime);
|
||||
lifetime.StopApplication();
|
||||
|
||||
var ex = await Assert.ThrowsAsync<RpcException>(() =>
|
||||
service.Sync(new SilentReader(), new RecordingWriter(), ContextFor(CancellationToken.None)));
|
||||
Assert.Equal(StatusCode.Unavailable, ex.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithoutALifetime_TheStreamRunsUntilItsCallEnds()
|
||||
{
|
||||
// Control for the two tests above: the same open stream, with no lifetime, is still running
|
||||
// after the same wait — so their prompt end is the lifetime's doing, not the harness's.
|
||||
var service = NewService();
|
||||
using var callCts = new CancellationTokenSource();
|
||||
var output = new RecordingWriter();
|
||||
var call = service.Sync(new SilentReader(), output, ContextFor(callCts.Token));
|
||||
await output.NextAsync(Timeout);
|
||||
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(500));
|
||||
Assert.False(call.IsCompleted);
|
||||
|
||||
callCts.Cancel();
|
||||
await call.WaitAsync(Timeout);
|
||||
}
|
||||
|
||||
private sealed class FakeLifetime : IHostApplicationLifetime
|
||||
{
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
public CancellationToken ApplicationStarted => CancellationToken.None;
|
||||
public CancellationToken ApplicationStopping => _stopping.Token;
|
||||
public CancellationToken ApplicationStopped => CancellationToken.None;
|
||||
public void StopApplication() => _stopping.Cancel();
|
||||
}
|
||||
|
||||
private static async Task SwallowAsync(Task task)
|
||||
{
|
||||
try { await task.WaitAsync(Timeout); }
|
||||
|
||||
Reference in New Issue
Block a user