fix(host): leave the cluster before Kestrel drains and end open streams on stop (#906)
v2-ci / unit-tests (push) Waiting to run

A planned stop of the Primary took ~30 s on QA: WebApplication stops the web
server before Akka.Hosting's service, and Kestrel's graceful drain waited the
whole ShutdownTimeout for the peer's open LocalDb sync stream (the library's
LocalDbSyncService only observes the call token, which fires at the END of the
drain). Only then did CoordinatedShutdown run, so the stopping node held the
Primary role and the peer sat at ServiceLevel 240 for the whole window.

- GracefulClusterLeaveService (IHostedLifecycleService): StoppingAsync runs
  before any hosted service's StopAsync; it raises ApplicationStopping, then
  runs and awaits CoordinatedShutdown with ClrExitReason (not Cluster.Leave,
  whose ClusterLeavingReason makes Akka.Hosting set ExitCode -1). The cluster
  is left while Kestrel is still up.
- ShutdownStreamCancellationInterceptor (innermost on the shared gRPC
  pipeline): every streaming call gets a context whose token is linked with
  ApplicationStopping; streams arriving once stopping are refused Unavailable.
  Host-side mitigation until LocalDb 0.3.8 is published and pinned
  (scadaproj 3ddb083, scadaproj#57); it also covers the mesh streams.

Test GracefulStopWithOpenSyncStreamTests (real WebApplication + Kestrel h2c +
MapZbLocalDbSync + single-node Akka cluster + real peer initiator, 8 s drain
budget): stop 895 ms, cluster left at 892 ms, drain began at 894 ms. Positive
control without the fix: stop 8863 ms, cluster left at 8841 ms after the drain
began at 0 ms. Mutations: no CoordinatedShutdown in StoppingAsync -> order
assertion fails (drain at 1 ms, removed at 893 ms); interceptor passes the
original context -> stop 8006 ms, fails.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-13 03:32:13 -05:00
parent 2688bd5cfd
commit dcff5ae994
4 changed files with 629 additions and 0 deletions
@@ -549,6 +549,14 @@ builder.Services.AddAkka("otopcua", (ab, sp) =>
}
});
// lmxopcua#906: a graceful stop leaves the cluster FIRST. Kestrel's hosted service stops before Akka's
// (WebApplication starts the server last), and Kestrel drains open requests for up to ShutdownTimeout,
// so without this the peer's long-lived LocalDb sync stream held the Primary role for ~30 s on every
// planned stop. Its StoppingAsync runs before ANY hosted service's StopAsync. Registered BEFORE the
// watchdog below only for readability — it raises ApplicationStopping itself, which is what keeps the
// watchdog quiet, so it does not depend on lifecycle-callback order.
builder.Services.AddOtOpcUaGracefulClusterLeave();
// Simultaneous-cold-start split-brain guard (dark switch Cluster:BootstrapGuard:Enabled, default off).
// Registered AFTER AddAkka so its StartAsync runs once Akka's hosted service has built (and, when the
// guard is off, already auto-joined) the ActorSystem. When on, the node started with no config seeds
@@ -651,6 +659,10 @@ if (hasDriver || hasAdmin)
o.Interceptors.Add<ConfigServeAuthInterceptor>();
o.Interceptors.Add<CentralCommandAuthInterceptor>();
}
// Innermost (after every auth gate): ends open streaming calls — the peer's LocalDb sync stream
// above all — on ApplicationStopping instead of letting Kestrel's drain wait out ShutdownTimeout
// (lmxopcua#906), and refuses new streams once stopping has begun.
o.Interceptors.Add<ShutdownStreamCancellationInterceptor>();
});
}