Skip to content

Conversation

@romanett
Copy link
Contributor

Proposed changes

  • Improve thread safe Utils methods for incrementing identifiers
  • Add Test for Wrapround of MonitoredItemIdFactory (this would fail before the improvements, with 0 included as id in as often as ever 3rd iteration)

Thanks to @marcschier for bringing this up.

Types of changes

  • 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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves thread safety for identifier incrementation methods in the OPC UA .NET Standard stack by addressing wraparound behavior and ensuring no duplicate or zero IDs are generated.

  • Updates utility methods to use atomic operations with proper type handling (uint instead of long)
  • Implements safer increment logic that prevents generation of zero values during wraparound
  • Adds comprehensive concurrency testing for wraparound scenarios in MonitoredItemIdFactory

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

File Description
Tests/Opc.Ua.Server.Tests/MonitoredItemIdFactoryTests.cs Adds test for concurrent ID generation during wraparound scenarios
Stack/Opc.Ua.Core/Types/Utils/Utils.cs Updates identifier utility methods with thread-safe atomic operations and zero-prevention logic
Multiple server/client files Changes identifier fields from long to uint for consistency with new utility methods

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

}
} while (exchangedValue != value);
return (uint)Interlocked.Read(ref identifier);
return value;
Copy link

Copilot AI Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should return the final value of the identifier after the operation, but it returns the old value instead of reading the current identifier value. This could return stale data if another thread modified the identifier between the last read and the return statement.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation needs to change, no matter, no one is using the retur value

@codecov
Copy link

codecov bot commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.65%. Comparing base (2902718) to head (66db34d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
Stack/Opc.Ua.Core/Types/Utils/Utils.cs 87.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3221      +/-   ##
==========================================
+ Coverage   57.62%   57.65%   +0.03%     
==========================================
  Files         355      355              
  Lines       78679    78688       +9     
  Branches    13857    13860       +3     
==========================================
+ Hits        45335    45366      +31     
+ Misses      29092    29069      -23     
- Partials     4252     4253       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}
} while (exchangedValue != value);
return (uint)Interlocked.Read(ref identifier);
return value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation needs to change, no matter, no one is using the retur value

@marcschier marcschier merged commit 7cbf817 into OPCFoundation:master Sep 22, 2025
77 checks passed
@romanett romanett deleted the feat/ThreadSafeIncrement branch September 22, 2025 18:17
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.

2 participants