Optimize event handling performance: move GetEventFields outside lock in QueueEvent#3596
Draft
Optimize event handling performance: move GetEventFields outside lock in QueueEvent#3596
Conversation
|
|
…ce load test Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
…ublish wait Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix OPC UA server unresponsiveness with increased monitored items
Optimize event handling performance: move GetEventFields outside lock in QueueEvent
Mar 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Server becomes unresponsive under load (~7000 monitored items) because
MonitoredItem.QueueEventholdsm_lockwhile executingGetEventFields— an expensive node-hierarchy traversal that callsGetAttributeValueper select clause. With many concurrent subscriptions, threads pile up waiting for the lock.Changes
Libraries/Opc.Ua.Server/Subscription/MonitoredItem/MonitoredItem.csRefactored
QueueEvent(IFilterTarget, bool)to a double-lock pattern:CanSendFilteredAlarmstays in the first lock because it mutatesm_filteredRetainConditionIdsFilterContextis created once inside the first lock (capturing session locales atomically) and reused forGetEventFieldsTests/Opc.Ua.Client.Tests/LoadTest.csAdded
ServerEventSubscribeLoadTestAsync(explicit,[Order(120)]) — 10 sessions × 5 subscriptions monitoringObjectIds.Server, 500 events generated viaReferenceServer.CurrentInstance.ReportEvent(), asserts ≥ 99% notification delivery ratio and measures throughput (events/sec).Types of changes
Checklist
Further comments
The core issue is that
GetEventFieldsscales linearly with(select clauses) × (subscriptions per node). Under load this dominates lock hold time and starves other threads. Moving it outside the lock lets multiple subscriptions read event fields concurrently rather than serially. TheCanSendFilteredAlarm/m_filteredRetainConditionIdsconcern was the main constraint preventing a straightforward unlock-and-read approach.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.