OPCFoundation / UA-.NETStandard

OPC Unified Architecture .NET Standard
Other
1.97k stars 950 forks source link

Uncaught ServiceResultException: BadSubscriptionIdInvalid when creating multiple subscriptions #2791

Open woutervd3 opened 1 month ago

woutervd3 commented 1 month ago

Type of issue

Current Behavior

We're creating multiple subscriptions in a session (per 1000 monitored items) using the client library. When creating e.g. 6 subscriptions we see logs:

- Subscription CREATED, Id=4, LastNotificationTime=06:49:48, GoodPublishRequestCount=1, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
- Subscription CREATED, Id=5, LastNotificationTime=06:49:49, GoodPublishRequestCount=2, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
- Subscription CREATED, Id=6, LastNotificationTime=06:49:51, GoodPublishRequestCount=3, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
-  Subscription CREATED, Id=7, LastNotificationTime=06:49:52, GoodPublishRequestCount=4, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
- Received Publish Response for Unknown SubscriptionId=8. Deleting abandoned subscription from server.
- Deleting server subscription for SubscriptionId=8
- fail: Microsoft.Extensions.Hosting.Internal.Host[9]
      BackgroundService failed
      Opc.Ua.ServiceResultException: BadSubscriptionIdInvalid
         at Opc.Ua.ClientBase.ValidateResponse(ResponseHeader header)
         at Opc.Ua.SessionClient.CreateMonitoredItems(RequestHeader requestHeader, UInt32 subscriptionId, TimestampsToReturn timestampsToReturn, MonitoredItemCreateRequestCollection itemsToCreate, MonitoredItemCreateResultCollection& results, DiagnosticInfoCollection& diagnosticInfos)
         at Opc.Ua.SessionClientBatched.CreateMonitoredItems(RequestHeader requestHeader, UInt32 subscriptionId, TimestampsToReturn timestampsToReturn, MonitoredItemCreateRequestCollection itemsToCreate, MonitoredItemCreateResultCollection& results, DiagnosticInfoCollection& diagnosticInfos)
         at Opc.Ua.Client.Subscription.CreateItems()
         at Opc.Ua.Client.Subscription.Create()
- Subscription PUBLISHING STOPPED, Id=8, LastNotificationTime=06:49:53, GoodPublishRequestCount=4, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
- Subscription PUBLISHING STOPPED, Id=8, LastNotificationTime=06:49:53, GoodPublishRequestCount=6, PublishingInterval=100, KeepAliveCount=10, PublishingEnabled=True, MonitoredItemCount=1000
- Unexpected error sending publish request.
      Opc.Ua.ServiceResultException: BadConnectionClosed
         at Opc.Ua.Bindings.UaSCUaBinaryClientChannel.BeginSendRequest(IServiceRequest request, Int32 timeout, AsyncCallback callback, Object state)
         at Opc.Ua.Bindings.UaSCUaBinaryTransportChannel.BeginSendRequest(IServiceRequest request, AsyncCallback callback, Object callbackData)
         at Opc.Ua.SessionClient.BeginPublish(RequestHeader requestHeader, SubscriptionAcknowledgementCollection subscriptionAcknowledgements, AsyncCallback callback, Object asyncState)
         at Opc.Ua.Client.Session.BeginPublish(Int32 timeout)
-  Publish abandoned after error because session id changed: Old ns=1;g=c5cc3489-70b9-4b48-859f-e73029bb1f90 != New (null)
- ChannelId 1688135334: CLIENTCHANNEL SOCKET CLOSED: 00AD8B02
-  Publish #49, Subscription count = 0, Error: Channel has been closed.
-  Publish #53, Subscription count = 0, Error: Channel has been closed.
- ....

In the server's logs (KEPServerEX) we see normal behaviour (see screenshots below), every CreateSubscriptionRequest is answered with a good CreateSubscriptionResponse, only then we see a DeleteSubscriptionRequest (which is answered with a good DeleteSubscriptionResponse as well) for the last confirmed create.

CreateSubscriptionRequest : image CreateSubscriptionResponse: image

DeleteSubscriptionRequest: image DeleteSubscriptionResponse: image

Sometimes the application does succeed in creating all subscriptions.

Expected Behavior

I don't expect to see any unknown subscription Id's while creating new subscriptions. If they do happen, I expect a successful delete of a subscription to not throw exceptions.

Steps To Reproduce

I have not created an isolated project to reproduce this yet, but what we're doing:

  1. create a session
  2. Add some Handlers (SubscriptionsChanged, KeepAlive, SessionClosing, PublishError, Notification)
  3. create a list of subscriptions, each with x amount of monitoredItems (with subscription.AddItems()). Add handlers for FastDataChangeCallback, StateChanged, PublishStatusChanged and Notification of each item.
  4. add each subscription to the session with session.AddSubscription(subscription)
  5. create each subscription server-side with subscription.Create()

Environment

- OS: Ubuntu
- Environment: Visual Studio 2022, application running in container
- Runtime: .NET 8
- Nuget Version: 1.5.374.124
- Component: Opc.Ua.Client
- Server: KepServerEX 6.14 (V6.14.263.0)
- Client: UA-.NETStandard library

Anything else?

No response