fix(agent): observe timed-out camera probes (#250)
Heimdall Gitea / Detect CI scope (pull_request) Successful in 8s
Heimdall Gitea / Build and tests (pull_request) Successful in 19m48s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m48s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 10m41s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Successful in 19m35s
Heimdall Gitea / CI policy (pull_request) Successful in 1m0s

This commit is contained in:
arnaud.dartois committed 2026-11-06 21:43:06 +01:00
1 parent f632fee7d8
commit 0b0431c1d2
3 files changed
+79 -23

No files matched your search

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