fix(galaxy): background probe seed; generation-checked registrations
Review fix round 2 for lane F: - N1: the init platform seed no longer runs inline under the init token. Galaxy's Tier-A Initialize budget is 5 s, and a cold gateway DiscoverHierarchy can spend all of it, so the OperationCanceledException rethrow failed init, opened the Initialize breaker and cost a full rebuild per retry. InitializeAsync now only resolves the platform source (no network) and queues a background seed tied to a per-runtime CTS that the teardown cancels. A result that lands after its runtime was torn down is discarded. - N2: the seed worker serves the runtime that is live when each pass STARTS, so a request made under a new runtime while a stale pass is in flight always gets a pass of its own. - N3: SubscribeAsync and the subscribe-once read capture the runtime generation before SubscribeBulk and register only if it is unchanged, atomically with the teardown's registry Clear (_registryGate). On a mismatch they release the handles and fail the call. Re-subscribe Rebind uses the same gate, and the production rebuild clears the registry unconditionally. - PerPlatformProbeWatcher re-checks its stop after the SubscribeBulk await, atomically with the record and publish. A pass that straddles StopAsync releases the handles it acquired instead of writing the probe entry back into a cleared registry. Refs #735 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -49,7 +49,8 @@ public sealed class PerPlatformProbeWatcher : IDisposable
|
||||
private readonly ConcurrentDictionary<string, int> _itemHandlesByPlatform =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Lock _syncLock = new();
|
||||
private bool _disposed;
|
||||
// Written under _syncLock by StopAsync; read under it wherever a write-back must not outlive a stop.
|
||||
private volatile bool _disposed;
|
||||
|
||||
/// <summary>Initializes a new instance of the PerPlatformProbeWatcher class.</summary>
|
||||
/// <param name="subscriber">The Galaxy subscriber for managing probe subscriptions.</param>
|
||||
@@ -100,10 +101,18 @@ public sealed class PerPlatformProbeWatcher : IDisposable
|
||||
private IReadOnlyList<TagBinding> BuildBindings() =>
|
||||
[.. _itemHandlesByPlatform.Select(kvp => new TagBinding(kvp.Key + ProbeSuffix, kvp.Value))];
|
||||
|
||||
/// <summary>Mirror the tracked handle set into the driver registry (no-op without one).</summary>
|
||||
/// <summary>
|
||||
/// Mirror the tracked handle set into the driver registry (no-op without one, or once stopped —
|
||||
/// a stopped watcher must never write its entry back into a registry the teardown cleared).
|
||||
/// </summary>
|
||||
private void PublishBindings()
|
||||
{
|
||||
if (_registry is null) return;
|
||||
lock (_syncLock) PublishBindingsLocked();
|
||||
}
|
||||
|
||||
private void PublishBindingsLocked()
|
||||
{
|
||||
if (_registry is null || _disposed) return;
|
||||
var bindings = BuildBindings();
|
||||
if (bindings.Count == 0) _registry.Remove(SubscriptionRegistry.ProbeSubscriptionId);
|
||||
else _registry.Upsert(SubscriptionRegistry.ProbeSubscriptionId, bindings);
|
||||
@@ -136,24 +145,51 @@ public sealed class PerPlatformProbeWatcher : IDisposable
|
||||
var results = await _subscriber.SubscribeBulkAsync(
|
||||
probeAddresses, _bufferedUpdateIntervalMs, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var acquired = new (string Platform, int ItemHandle, string? Error)[platforms.Count];
|
||||
for (var i = 0; i < platforms.Count; i++)
|
||||
{
|
||||
var platform = platforms[i];
|
||||
var match = results.FirstOrDefault(r => string.Equals(
|
||||
r.TagAddress, probeAddresses[i], StringComparison.OrdinalIgnoreCase));
|
||||
acquired[i] = (platforms[i], match is { WasSuccessful: true } ? match.ItemHandle : 0,
|
||||
match?.ErrorMessage ?? "<no result returned>");
|
||||
}
|
||||
|
||||
var itemHandle = match is { WasSuccessful: true } ? match.ItemHandle : 0;
|
||||
_itemHandlesByPlatform[platform] = itemHandle;
|
||||
|
||||
if (itemHandle <= 0)
|
||||
// Re-check the stop AFTER the await, atomically with the record + publish: a StopAsync that
|
||||
// ran while SubscribeBulk was in flight already cleared the map and the registry entry, so
|
||||
// recording now would resurrect the probe entry into the next runtime's registry.
|
||||
bool stopped;
|
||||
lock (_syncLock)
|
||||
{
|
||||
stopped = _disposed;
|
||||
if (!stopped)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"PerPlatformProbeWatcher subscribe failed for {Platform}: {Error}",
|
||||
platform, match?.ErrorMessage ?? "<no result returned>");
|
||||
foreach (var (platform, itemHandle, _) in acquired) _itemHandlesByPlatform[platform] = itemHandle;
|
||||
PublishBindingsLocked();
|
||||
}
|
||||
}
|
||||
|
||||
PublishBindings();
|
||||
if (stopped)
|
||||
{
|
||||
var orphans = acquired.Where(a => a.ItemHandle > 0).Select(a => a.ItemHandle).ToArray();
|
||||
if (orphans.Length > 0)
|
||||
{
|
||||
try { await _subscriber.UnsubscribeBulkAsync(orphans, CancellationToken.None).ConfigureAwait(false); }
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"PerPlatformProbeWatcher could not release {Count} probe handle(s) acquired after stop", orphans.Length);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (platform, itemHandle, error) in acquired)
|
||||
{
|
||||
if (itemHandle <= 0)
|
||||
{
|
||||
_logger.LogWarning("PerPlatformProbeWatcher subscribe failed for {Platform}: {Error}", platform, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Snapshot of platform tag names currently watched.</summary>
|
||||
@@ -274,12 +310,15 @@ public sealed class PerPlatformProbeWatcher : IDisposable
|
||||
/// <returns>A task that completes once the handles have been released (or the attempt gave up).</returns>
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
var liveHandles = _itemHandlesByPlatform.Values.Where(h => h > 0).ToArray();
|
||||
_itemHandlesByPlatform.Clear();
|
||||
_registry?.Remove(SubscriptionRegistry.ProbeSubscriptionId);
|
||||
int[] liveHandles;
|
||||
lock (_syncLock)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
liveHandles = [.. _itemHandlesByPlatform.Values.Where(h => h > 0)];
|
||||
_itemHandlesByPlatform.Clear();
|
||||
_registry?.Remove(SubscriptionRegistry.ProbeSubscriptionId);
|
||||
}
|
||||
if (liveHandles.Length > 0)
|
||||
{
|
||||
try { await _subscriber.UnsubscribeBulkAsync(liveHandles, cancellationToken).ConfigureAwait(false); }
|
||||
|
||||
Reference in New Issue
Block a user