From 938adbf3a86baec6b2f6ecc86937a79fe058a3b3 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Fri, 6 Nov 2026 20:55:07 +0100 Subject: [PATCH] fix(web): avoid blocking SignalR disposal Fixes blocking SignalR client shutdown; see issue #233. Co-authored-by: Arnaud Dartois --- skills/signalr-client-lifecycle/SKILL.md | 25 +++++++++++ .../Services/NotificationsHubClient.cs | 45 +++++++++++++------ 2 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 skills/signalr-client-lifecycle/SKILL.md diff --git a/skills/signalr-client-lifecycle/SKILL.md b/skills/signalr-client-lifecycle/SKILL.md new file mode 100644 index 0000000..cc61675 --- /dev/null +++ b/skills/signalr-client-lifecycle/SKILL.md @@ -0,0 +1,25 @@ +--- +name: signalr-client-lifecycle +description: Avoid blocking SignalR client teardown in Heimdall Blazor components and services. +--- + +# Heimdall SignalR client lifecycle + +`NotificationsHubClient` is scoped to a Blazor circuit and owns a `HubConnection`. Components that use it should implement `IAsyncDisposable`, unsubscribe their event handlers, then await `Hub.DisposeAsync()`. + +## Implementation + +- Do not call `.Wait()`, `.Result`, or `GetAwaiter().GetResult()` on SignalR stop/dispose operations. Network shutdown can block a renderer or request thread. +- Centralize teardown in one asynchronous helper. Atomically detach the owned connection before awaiting shutdown so repeated disposal cannot dispose it twice. +- `DisposeAsync()` awaits `StopAsync` and `DisposeAsync`, handling shutdown failures independently so failed stop still attempts resource disposal. +- If compatibility requires synchronous `IDisposable`, it must return without blocking and start best-effort asynchronous cleanup. Keep `GC.SuppressFinalize(this)` on both dispose paths. +- Log cleanup failures at debug level without access tokens, URLs containing credentials, or message payloads. + +## Acceptance + +1. Build `src/Heimdall.Web/Heimdall.Web.csproj` with the SDK pinned by `global.json`; do not suppress analyzer warnings for blocking calls. +2. Verify the component unsubscribes handlers and awaits async disposal. +3. Run the disposable Central + PostgreSQL Compose recipe from `skills/compose-disposable-recipe/SKILL.md` with unique generated credentials, ports, project, and image tag; check `/healthz` and verify cleanup of project resources and that exact tag. +4. Treat production GlitchTip recurrence as external evidence: local build and Compose do not prove the deployed client has stopped emitting events. + +See Gitea issue [#233](https://sources.tordais.cc/arnaud.dartois/Heimdall/issues/233) for the current incident evidence. diff --git a/src/Heimdall.Web/Services/NotificationsHubClient.cs b/src/Heimdall.Web/Services/NotificationsHubClient.cs index 177296d..2f9c879 100644 --- a/src/Heimdall.Web/Services/NotificationsHubClient.cs +++ b/src/Heimdall.Web/Services/NotificationsHubClient.cs @@ -104,25 +104,42 @@ public class NotificationsHubClient : IAsyncDisposable, IDisposable public void Dispose() { - // Dispose synchrone (requis par certains conteneurs / bUnit) : - // déconnexion best-effort sans attendre la fermeture réseau. - if (_connection is not null) - { - try - { - _connection.StopAsync(CancellationToken.None).Wait(TimeSpan.FromMilliseconds(500)); - _connection.DisposeAsync().AsTask().GetAwaiter().GetResult(); - } - catch { /* best effort */ } - } - _started = false; + // Synchronous disposal cannot safely wait for SignalR network shutdown. + // Start the asynchronous cleanup and return immediately; async consumers + // should use DisposeAsync to observe its completion. + _ = DisposeConnectionAsync(); GC.SuppressFinalize(this); } public async ValueTask DisposeAsync() { - Dispose(); + await DisposeConnectionAsync().ConfigureAwait(false); GC.SuppressFinalize(this); - await Task.CompletedTask; + } + + private async Task DisposeConnectionAsync() + { + var connection = Interlocked.Exchange(ref _connection, null); + _started = false; + if (connection is null) + return; + + try + { + await connection.StopAsync(CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogDebug(ex, "NotificationsHubClient: arrêt de la connexion impossible"); + } + + try + { + await connection.DisposeAsync().ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogDebug(ex, "NotificationsHubClient: libération de la connexion impossible"); + } } }