fix(web): avoid blocking SignalR disposal #272

Merged
arnaud.dartois merged 1 commits from fix/glitchtip-7426-notifications-dispose into main 2026-11-06 20:55:07 +01:00
2 changed files with 56 additions and 14 deletions

No files matched your search

+25
View File
@@ -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.
@@ -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");
}
}
}