fix(lmxproxy): await COM subscription creation to fix Subscribe flakiness

SubscriptionManager.Subscribe was fire-and-forgetting the MxAccess COM
subscription creation. The initial OnDataChange callback could fire
before the subscription was established, losing the first (and possibly
only) value update. Changed to async SubscribeAsync that awaits
CreateMxAccessSubscriptionsAsync before returning the channel reader.

Subscribe_ReceivesUpdates now passes 5/5 consecutive runs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-03-22 23:48:01 -04:00
parent 84b7b6a7a9
commit b218773ab0
4 changed files with 31 additions and 26 deletions

View File

@@ -360,7 +360,7 @@ namespace ZB.MOM.WW.LmxProxy.Host.Grpc.Services
throw new RpcException(new GrpcStatus(StatusCode.Unauthenticated, "Invalid session"));
}
var reader = _subscriptionManager.Subscribe(
var reader = await _subscriptionManager.SubscribeAsync(
request.SessionId, request.Tags, context.CancellationToken);
try

View File

@@ -42,8 +42,10 @@ namespace ZB.MOM.WW.LmxProxy.Host.Subscriptions
/// <summary>
/// Creates a subscription for a client. Returns a ChannelReader to stream from.
/// Awaits COM subscription creation so the initial OnDataChange callback
/// is not missed.
/// </summary>
public ChannelReader<(string address, Vtq vtq)> Subscribe(
public async Task<ChannelReader<(string address, Vtq vtq)>> SubscribeAsync(
string clientId, IEnumerable<string> addresses, CancellationToken ct)
{
var channel = Channel.CreateBounded<(string address, Vtq vtq)>(
@@ -83,10 +85,13 @@ namespace ZB.MOM.WW.LmxProxy.Host.Subscriptions
_rwLock.ExitWriteLock();
}
// Create MxAccess COM subscriptions for newly subscribed tags
// Create MxAccess COM subscriptions — awaited so the initial
// OnDataChange (first value delivery after AdviseSupervisory)
// is not lost. The channel and routing are already set up above,
// so any callback that fires during this call will be delivered.
if (newTags.Count > 0)
{
_ = CreateMxAccessSubscriptionsAsync(newTags);
await CreateMxAccessSubscriptionsAsync(newTags);
}
// Register cancellation cleanup