From 43e6752a31d32a4dc9b2f918417d9f0f802adfa8 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sun, 8 Nov 2026 07:02:54 +0100 Subject: [PATCH] fix(web): make dashboard refresh disposal safe Refs #298 Co-authored-by: Arnaud Dartois --- skills/dashboard-refresh-lifecycle/SKILL.md | 17 +++++++ .../Components/Pages/Dashboard.razor | 49 ++++++++++++++++--- .../Components/Pages/DashboardTests.cs | 29 +++++++++++ 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 skills/dashboard-refresh-lifecycle/SKILL.md diff --git a/skills/dashboard-refresh-lifecycle/SKILL.md b/skills/dashboard-refresh-lifecycle/SKILL.md new file mode 100644 index 0000000..a1599cf --- /dev/null +++ b/skills/dashboard-refresh-lifecycle/SKILL.md @@ -0,0 +1,17 @@ +--- +name: dashboard-refresh-lifecycle +description: Maintain the Heimdall Web dashboard's post-render agent refresh loop and component shutdown behavior. +--- + +# Dashboard refresh lifecycle + +The dashboard starts its 15-second agent refresh only after the first interactive render. Keep the loop task and its `CancellationTokenSource` owned by the component. Capture the token once when starting the loop; do not read the mutable source inside the loop because disposal or a restart can replace it. + +When the component is disposed, mark it disposed under the same synchronization used to start the loop, cancel the source, and let the loop finish before disposing that source. Handle a request cancellation as a normal shutdown, keep transient/401 load failures from escaping the renderer, and do not schedule `StateHasChanged` after disposal. + +## Acceptance + +- Exercise component disposal while a refresh is pending and confirm no renderer exception escapes. +- Exercise an agent request failure (including unauthorized) and confirm dashboard rendering remains stable and no periodic work survives component disposal. +- Run `dotnet test tests/Heimdall.Web.Tests/Heimdall.Web.Tests.csproj`. +- For behavior changes, run the disposable Central + PostgreSQL Compose recipe from `skills/compose-disposable-recipe/SKILL.md`, then verify project resource and exact locally built image tag cleanup. diff --git a/src/Heimdall.Web/Components/Pages/Dashboard.razor b/src/Heimdall.Web/Components/Pages/Dashboard.razor index 186799a..d4a96eb 100644 --- a/src/Heimdall.Web/Components/Pages/Dashboard.razor +++ b/src/Heimdall.Web/Components/Pages/Dashboard.razor @@ -282,6 +282,8 @@ // infinie awaitée dans OnInitializedAsync bloque le rendu initial // (SSR/prerender attend la fin du cycle de vie) → page lente. private CancellationTokenSource? _refreshCts; + private Task? _refreshTask; + private readonly object _refreshLock = new(); private bool _disposed; private bool _autoRefreshStarted; @@ -290,18 +292,24 @@ if (firstRender && !_autoRefreshStarted) { _autoRefreshStarted = true; - await StartAutoRefresh(); + lock (_refreshLock) + { + if (!_disposed) + { + _refreshCts = new CancellationTokenSource(); + _refreshTask = RunAutoRefreshAsync(_refreshCts.Token); + } + } } } - private async Task StartAutoRefresh() + private async Task RunAutoRefreshAsync(CancellationToken cancellationToken) { - _refreshCts = new CancellationTokenSource(); try { - while (!_refreshCts.Token.IsCancellationRequested) + while (!cancellationToken.IsCancellationRequested) { - await Task.Delay(TimeSpan.FromSeconds(15), _refreshCts.Token); + await Task.Delay(TimeSpan.FromSeconds(15), cancellationToken); if (_disposed) return; // Rafraîchit uniquement la section agents (léger) : les stats // globales restent au refresh manuel pour éviter le flash. @@ -311,6 +319,10 @@ var agentsPage = await Api.GetAllAgentsAsync(1, 5); _agents = agentsPage?.Items?.ToList(); } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + return; + } catch { /* keep last state */ } finally { @@ -320,6 +332,7 @@ } } catch (OperationCanceledException) { /* arrêt volontaire */ } + catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested) { /* arrêt concurrent */ } } private async Task LoadData() @@ -401,9 +414,29 @@ public void Dispose() { - _disposed = true; + CancellationTokenSource? refreshCts; + lock (_refreshLock) + { + _disposed = true; + refreshCts = _refreshCts; + _refreshCts = null; + } Language.LanguageChanged -= OnLanguageChanged; - _refreshCts?.Cancel(); - _refreshCts?.Dispose(); + if (refreshCts is not null) + { + try { refreshCts.Cancel(); } + catch (ObjectDisposedException) { /* shutdown already completed */ } + _ = DisposeRefreshAsync(refreshCts, _refreshTask); + } + } + + private static async Task DisposeRefreshAsync(CancellationTokenSource refreshCts, Task? refreshTask) + { + try + { + if (refreshTask is not null) await refreshTask; + } + catch (OperationCanceledException) { } + finally { refreshCts.Dispose(); } } } diff --git a/tests/Heimdall.Web.Tests/Components/Pages/DashboardTests.cs b/tests/Heimdall.Web.Tests/Components/Pages/DashboardTests.cs index dee3117..57136bf 100644 --- a/tests/Heimdall.Web.Tests/Components/Pages/DashboardTests.cs +++ b/tests/Heimdall.Web.Tests/Components/Pages/DashboardTests.cs @@ -173,4 +173,33 @@ public class DashboardTests : PageTestBase (snackbar.ShownSnackbars.First().Message?.ToString() ?? "").ShouldContain("Erreur"); }); } + + [Fact] + public async Task DashboardPage_DisposePendantRafraichissementNePropagePasException() + { + SetupHappyPath(); + LoginAs(); + + var cut = RenderPage(); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agent-1")); + + // Le composant doit tolérer un 401 lors de son rafraîchissement périodique, + // puis annuler et attendre proprement sa boucle à la destruction. + Handler.OnGet("api/agents", System.Net.HttpStatusCode.Unauthorized); + cut.Dispose(); + await Task.Delay(50); + } + + [Fact] + public void DashboardPage_ErreurAgentsNeFaitPasEchouerLeRendu() + { + SetupHappyPath(); + Handler.OnGet("api/agents", System.Net.HttpStatusCode.InternalServerError); + LoginAs(); + + var cut = RenderPage(); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agents en ligne")); + cut.Dispose(); + } }