Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95c73c3052 | ||
|
|
938adbf3a8 |
No files matched your search
@@ -28,6 +28,8 @@ dotnet restore tests/Heimdall.Agent.Tests/Heimdall.Agent.Tests.csproj --locked-m
|
||||
dotnet test tests/Heimdall.Agent.Tests/Heimdall.Agent.Tests.csproj --no-restore
|
||||
```
|
||||
|
||||
For `NetworkCameraScanner` TCP probes, bound connects with a linked cancellation token and await the connect task on both success and timeout. Do not race a live `TcpClient.ConnectAsync` against `Task.Delay` and return while the connect task is still running: closing the client can surface a later socket cancellation as an unobserved task exception. Keep a focused test proving the timeout cancels and observes the pending connect, and that caller cancellation still propagates.
|
||||
|
||||
For the focused Kestrel ACK acceptance, copy only the test project's source dependencies into a temporary directory outside the repository. Use a uniquely named Compose project with the pinned .NET SDK image, the temporary source mounted at `/workspace`, a project-scoped NuGet/result volume, a project-scoped bridge, a generated fixture API key, and a loopback-only port. Put the two locked restore/test commands in `/workspace/run-ack-test.sh` and set `entrypoint: ["/bin/bash", "/workspace/run-ack-test.sh"]`; avoid a scalar `command` with `bash -lc`, which Compose passes as multiple arguments and can run `dotnet` without its subcommand. The test itself proves Central returns a transient 503 and then persists the exact digest once. This SDK-only recipe builds no local images; keep the shared upstream SDK image. Install an EXIT cleanup trap before starting, tear down only that project with `down --volumes --remove-orphans`, verify its containers, volumes, and network are gone, and remove the temporary source even on failure. The acceptance record must state `local image tags: none`; if future changes add a local image build, record its unique tag and remove/verify that exact tag in the same trap. Serialize Docker acceptance when another source build is using the shared daemon.
|
||||
|
||||
This SDK-only recipe does not build a local Heimdall image; keep the shared upstream SDK image. If the recipe is extended to build an app image, assign a project-unique tag and remove/verify that exact tag after teardown per `skills/compose-disposable-recipe/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.
|
||||
@@ -614,17 +614,39 @@ public NetworkCameraScanner(
|
||||
var tasks = CameraPorts.Select(async port =>
|
||||
{
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(IPAddress.Parse(ip), port, ct).AsTask();
|
||||
var completed = await Task.WhenAny(connectTask, Task.Delay(PortTimeout, ct)).ConfigureAwait(false);
|
||||
if (completed == connectTask && client.Connected)
|
||||
if (await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(IPAddress.Parse(ip), port, token), PortTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
open.Add(port);
|
||||
client.Close();
|
||||
});
|
||||
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
return [.. open.Order()];
|
||||
}
|
||||
|
||||
internal static async Task<bool> ConnectWithTimeoutAsync(
|
||||
Func<CancellationToken, ValueTask> connectAsync,
|
||||
TimeSpan timeout,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var timeoutSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeoutSource.CancelAfter(timeout);
|
||||
|
||||
try
|
||||
{
|
||||
await connectAsync(timeoutSource.Token).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<HttpProbe?> ProbeHttpAsync(string ip, int port, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
@@ -1104,9 +1126,9 @@ public NetworkCameraScanner(
|
||||
{
|
||||
var uri = new Uri(url);
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(uri.Host, uri.Port, ct).AsTask();
|
||||
var connected = await Task.WhenAny(connectTask, Task.Delay(RtspTimeout, ct)).ConfigureAwait(false);
|
||||
if (connected != connectTask || !client.Connected)
|
||||
if (!await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(uri.Host, uri.Port, token), RtspTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
return false;
|
||||
|
||||
var options = $"OPTIONS rtsp://{uri.Host}:{uri.Port}/ RTSP/1.0\r\nCSeq: 1\r\nUser-Agent: HeimdallAgent/1.0\r\n\r\n";
|
||||
@@ -1188,11 +1210,10 @@ public NetworkCameraScanner(
|
||||
var password = credentials.Length > 1 ? Uri.UnescapeDataString(credentials[1]) : "";
|
||||
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(host, port, ct).AsTask();
|
||||
var connected = await Task.WhenAny(connectTask, Task.Delay(DvripTimeout, ct)).ConfigureAwait(false);
|
||||
if (connected != connectTask || !client.Connected)
|
||||
if (!await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(host, port, token), DvripTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
return false;
|
||||
await connectTask.ConfigureAwait(false);
|
||||
|
||||
var stream = client.GetStream();
|
||||
|
||||
@@ -1315,11 +1336,10 @@ public NetworkCameraScanner(
|
||||
var port = uri.IsDefaultPort ? 1935 : uri.Port;
|
||||
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(uri.Host, port, ct).AsTask();
|
||||
var connected = await Task.WhenAny(connectTask, Task.Delay(DvripTimeout, ct)).ConfigureAwait(false);
|
||||
if (connected != connectTask || !client.Connected)
|
||||
if (!await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(uri.Host, port, token), DvripTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
return false;
|
||||
await connectTask.ConfigureAwait(false);
|
||||
|
||||
var stream = client.GetStream();
|
||||
|
||||
@@ -1370,11 +1390,10 @@ public NetworkCameraScanner(
|
||||
var path = string.IsNullOrEmpty(uri.AbsolutePath) ? "/bubble/live?ch=0&stream=0" : uri.PathAndQuery;
|
||||
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(uri.Host, port, ct).AsTask();
|
||||
var connected = await Task.WhenAny(connectTask, Task.Delay(DvripTimeout, ct)).ConfigureAwait(false);
|
||||
if (connected != connectTask || !client.Connected)
|
||||
if (!await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(uri.Host, port, token), DvripTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
return false;
|
||||
await connectTask.ConfigureAwait(false);
|
||||
|
||||
var stream = client.GetStream();
|
||||
|
||||
@@ -1431,11 +1450,10 @@ public NetworkCameraScanner(
|
||||
var port = uri.IsDefaultPort ? 8080 : uri.Port;
|
||||
|
||||
using var client = new TcpClient();
|
||||
var connectTask = client.ConnectAsync(uri.Host, port, ct).AsTask();
|
||||
var connected = await Task.WhenAny(connectTask, Task.Delay(DvripTimeout, ct)).ConfigureAwait(false);
|
||||
if (connected != connectTask || !client.Connected)
|
||||
if (!await ConnectWithTimeoutAsync(
|
||||
token => client.ConnectAsync(uri.Host, port, token), DvripTimeout, ct)
|
||||
.ConfigureAwait(false))
|
||||
return false;
|
||||
await connectTask.ConfigureAwait(false);
|
||||
|
||||
var stream = client.GetStream();
|
||||
var request = $"GET /accessories HTTP/1.1\r\nHost: {uri.Host}\r\n\r\n";
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,4 +42,40 @@ public class NetworkCameraScannerTests
|
||||
var scanner = new NetworkCameraScanner(logger, loggerFactory);
|
||||
scanner.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectWithTimeoutAsync_CancelsAndObservesTimedOutConnect()
|
||||
{
|
||||
var connectCancellationObserved = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
var connected = await NetworkCameraScanner.ConnectWithTimeoutAsync(async cancellationToken =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
connectCancellationObserved.TrySetResult();
|
||||
throw;
|
||||
}
|
||||
}, TimeSpan.FromMilliseconds(25), CancellationToken.None);
|
||||
|
||||
connected.ShouldBeFalse();
|
||||
await connectCancellationObserved.Task.WaitAsync(TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConnectWithTimeoutAsync_PropagatesCallerCancellation()
|
||||
{
|
||||
using var cancellation = new CancellationTokenSource();
|
||||
var connecting = NetworkCameraScanner.ConnectWithTimeoutAsync(
|
||||
token => new ValueTask(Task.Delay(Timeout.InfiniteTimeSpan, token)),
|
||||
TimeSpan.FromSeconds(5),
|
||||
cancellation.Token);
|
||||
|
||||
await cancellation.CancelAsync();
|
||||
|
||||
await Should.ThrowAsync<OperationCanceledException>(async () => await connecting);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user