Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 3, 2025

Proposed changes

This PR adds automatic restoration of triggering item relationships when an OPC UA session reconnects or subscriptions are transferred. Previously, when a server restarted and the client reconnected, monitored items that were configured with triggering relationships (via SetTriggering) would lose those links, requiring manual re-establishment by the application.

The implementation includes:

  1. Persistent storage for triggering relationships - New properties in MonitoredItemState and MonitoredItem classes to track which items trigger which other items:

    • TriggeringItemId: Identifies the triggering item for a monitored item
    • TriggeredItems: Collection of client handles for items triggered by this item
  2. Automatic restoration logic - A new RestoreTriggeringAsync method that automatically re-establishes triggering links:

    • Called after monitored items are created during session recreation
    • Called after subscription transfer completes
    • Handles errors gracefully with appropriate logging
  3. User-friendly API - New Subscription.SetTriggeringAsync method that:

    • Accepts MonitoredItem objects instead of just server IDs
    • Automatically tracks triggering relationships for future restoration
    • Simplifies the API for setting up triggered items
  4. Test coverage - Added SetTriggeringTrackingAsync test that validates:

    • Triggering relationships are properly tracked
    • Relationships persist through snapshot/restore operations
    • The new API functions correctly

Related Issues

Types of changes

What types of changes does your code introduce?

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

This change eliminates the need for applications to manually track and restore triggering relationships after reconnection. Users can now use the new Subscription.SetTriggeringAsync method, and the SDK will automatically handle restoration during reconnection or transfer scenarios.

The implementation is backward compatible - existing code using Session.SetTriggering directly will continue to work, but won't benefit from automatic restoration unless migrated to use Subscription.SetTriggeringAsync.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for restoring (re-linking) of triggered monitored items upon session reconnect</issue_title>
<issue_description>### Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

I am developing an OPC UA connector (client) that can create subscriptions that contain monitored items that are linked to a triggering item via the Session.SetTriggering method.

Documentation about this approach can be found here:
https://reference.opcfoundation.org/Core/Part4/v105/docs/5.13

When I stop the opcua server and restart it, my connector (client) reconnects as soon as the opcua server is online.
The subscriptions and their monitored items are retained, but the linking of monitored items and their respective triggering items are gone.

Digging in the code I see a lot of code where the session and subscriptions are recreated upon reconnect, but nowhere the links between triggering items and their monitored items can be seen, which explains why triggered notifications don't work after a reconnect.

Could support for this be built into the underlying SDK?

Expected Behavior

After having created a subscription with triggering items,
as soon as the opcua server has restarted and the client has reconnected to the server
triggered notifications should resume normally as they did before the reconnect.

Steps To Reproduce

Start opc ua server
Start a client that connects to the server
On the client, create and link monitored items with a triggering item and start the subscription
Observe that notifications are received when the trigger item is triggered
Stop the server
Observe that the clients detects the disconnection
Start the server
Observe that the client reconnects to the server
Observe that triggered notifications don't work like they did before the reconnect.

Environment

- OS: Windows 10, Ubuntu
- Environment: Visual Studio 2022
- Runtime: .Net 6.0
- Nuget Version: Project references from master branch
- Component: Opc.Ua.Client
- Server: Reference Server
- Client: Own developed connector

Anything else?

Currently I work around this by maintaining a table of trigger items and their linked monitored items, and in the On_ReconnectComplete I will call the SetTriggering method again for all subscriptions and all associated linked items and triggers.
It requires a lot of leg-work to correlate items and their triggers and locate the new id's on the new reconnected session. I have to make extensive use of the ClientHandle to join the original items and the new items after the reconnect.</issue_description>

Comments on the Issue (you are @copilot in this section)

@mregen Great feedback, thanks. we need to investigate...

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 2 commits December 3, 2025 06:12
Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
@romanett
Copy link
Contributor

romanett commented Dec 3, 2025

@copilot please continue working

Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 3, 2025

@copilot please continue working

Work completed! The implementation now includes:

  1. ✅ Automatic tracking and restoration of triggering relationships during reconnection
  2. ✅ New Subscription.SetTriggeringAsync() method for easy setup with automatic tracking
  3. ✅ Persistence through MonitoredItemState snapshot/restore
  4. ✅ Test coverage validating the functionality

The triggering relationships are now automatically restored after session reconnects or subscription transfers. Commit 894c865 adds the final test validation.

Copilot AI changed the title [WIP] Add support for restoring triggered monitored items on session reconnect Add support for restoring triggered monitored items on session reconnect Dec 3, 2025
@marcschier marcschier marked this pull request as ready for review December 5, 2025 05:37
@marcschier marcschier merged commit 0f196ba into master Dec 5, 2025
165 of 186 checks passed
@romanett romanett deleted the copilot/add-support-for-relinking-items branch December 20, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for restoring (re-linking) of triggered monitored items upon session reconnect

4 participants