Skip to content

Does RemoveSubscripiton() is need before Close() and Dispose()? #1777

Closed

Description

@enricobenedos

Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

We are experiencing some issue related with RemoveSubscription method when connection is lost. It generates exception only in some cases because probably the connection timeout arrives some seconds after the real disconnection that we catch "instantaneously" with our implementation (failed read).
So it happens that if we have more than one session and our code reach the disconnection, we start disposing the sessions. At this point we are able to dispose for example the 1st session but no the 2th one because the internal subscription.Delete() method generated an exception.

We implemented our wrapper of this package and we also implemented a "custom dispose method" that we use to dispose our lib session:

public void Dispose()
{
    // Remove all subscription and relative subscribed events
    if (opcUaSubscription != null)
    {
        Console.WriteLine($"Disposing {opcUaSubscription.DisplayName} subscription..");
        foreach (var monitoredItem in opcUaSubscription.MonitoredItems)
        {
            monitoredItem.Notification -= OnMonitoredItemNotification;
        }
        Console.WriteLine("Disposed")
        opcUaSession?.RemoveSubscription(opcUaSubscription);
    
    // Close session and dispose it
    opcUaSession?.Close();
    opcUaSession?.Dispose();
    opcUaSession = null;
}

My question is: Does we need to call RemoveSubscription() before the Dispose() method? Or does Dispose() provide all the necessary to correctly release all resources?

Thanks

Expected Behavior

RemoveSubscription method with a more consistent behaviour.

Steps To Reproduce

1- Create an app that instance 2 OPC-UA sessions
2- Start reading/writing some variables with both session with a polling of 1 second
3- Unplug the ethernet cable or simply disable the adapter
4- Catch the read/write exception and execute our Dispose() code implementation
5- Probably an exception will be generated only for the second session during RemoveSubscription (unfortunately it is not properly constant due to the previous considerations)

Environment

- OS: Windows 11
- Environment: Visual Studio 2022 17.1.4
- Runtime: .NET 6
- Nuget Version: 1.4.366.38
- Component: Opc.Ua.Client

Anything else?

No response

Activity

  1. added
    question
    on Dec 11, 2022
  2. locked and limited conversation to collaborators on Dec 3, 2025
  3. converted this issue into a discussion #3372 on Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs investigationquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions