fix(web): make dashboard refresh disposal safe #299

Merged
arnaud.dartois merged 1 commits from auto-heimdall-auto-fix-issues-run-11-20261108T0300 into main 2026-11-08 07:02:54 +01:00
3 changed files with 87 additions and 8 deletions

No files matched your search

@@ -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.
@@ -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(); }
}
}
@@ -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<Dashboard>();
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<Dashboard>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agents en ligne"));
cut.Dispose();
}
}