Fix memory leak from undisposed sockets and event handler retention during server restart#3608
Merged
marcschier merged 4 commits intoMar 11, 2026
Conversation
…uring server restart When an OPC UA server is stopped and restarted in-process, TcpServerChannel instances were never garbage collected because UaSCBinaryChannel.Dispose() did not close the underlying socket. Pending ReceiveAsync operations kept the channels pinned via SocketAsyncEngine, causing a steady heap growth per cycle. Changes: - Close socket in UaSCBinaryChannel.Dispose() to cancel pending async I/O - Unsubscribe CertificateValidator.CertificateUpdate in StandardServer.Dispose() - Unsubscribe ConfigurationWatcher.Changed in StandardServer.Dispose() - Unsubscribe ConnectionStatusChanged from TransportListeners in ServerBase.Dispose() Verified over 786 restart cycles with stable heap.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3608 +/- ##
===========================================
+ Coverage 51.86% 66.06% +14.19%
===========================================
Files 370 459 +89
Lines 78618 97822 +19204
Branches 13650 16383 +2733
===========================================
+ Hits 40779 64630 +23851
+ Misses 33705 28143 -5562
- Partials 4134 5049 +915 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
marcschier
approved these changes
Mar 10, 2026
jnsjll
pushed a commit
to jnsjll/UA-.NETStandard
that referenced
this pull request
Mar 12, 2026
…uring server restart (OPCFoundation#3608) * Fix memory leak from undisposed sockets and retained event handlers during server restart When an OPC UA server is stopped and restarted in-process, TcpServerChannel instances were never garbage collected because UaSCBinaryChannel.Dispose() did not close the underlying socket. Pending ReceiveAsync operations kept the channels pinned via SocketAsyncEngine, causing a steady heap growth per cycle. Changes: - Close socket in UaSCBinaryChannel.Dispose() to cancel pending async I/O - Unsubscribe CertificateValidator.CertificateUpdate in StandardServer.Dispose() - Unsubscribe ConfigurationWatcher.Changed in StandardServer.Dispose() - Unsubscribe ConnectionStatusChanged from TransportListeners in ServerBase.Dispose() Verified over 786 restart cycles with stable heap. * Remove verbose comment * Remove redundant event unsubscribe before dispose * Remove duplicate CertificateUpdate unsubscribe (already existed in master)
mrsuciu
pushed a commit
to mrsuciu/UA-.NETStandard
that referenced
this pull request
Mar 19, 2026
…uring server restart (OPCFoundation#3608) * Fix memory leak from undisposed sockets and retained event handlers during server restart When an OPC UA server is stopped and restarted in-process, TcpServerChannel instances were never garbage collected because UaSCBinaryChannel.Dispose() did not close the underlying socket. Pending ReceiveAsync operations kept the channels pinned via SocketAsyncEngine, causing a steady heap growth per cycle. Changes: - Close socket in UaSCBinaryChannel.Dispose() to cancel pending async I/O - Unsubscribe CertificateValidator.CertificateUpdate in StandardServer.Dispose() - Unsubscribe ConfigurationWatcher.Changed in StandardServer.Dispose() - Unsubscribe ConnectionStatusChanged from TransportListeners in ServerBase.Dispose() Verified over 786 restart cycles with stable heap. * Remove verbose comment * Remove redundant event unsubscribe before dispose * Remove duplicate CertificateUpdate unsubscribe (already existed in master) (cherry picked from commit eae41a5)
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.
When an OPC UA server is stopped and restarted in-process (e.g. for configuration changes),
TcpServerChannelinstances from previous server lifetimes were never garbage collected, causing a steady heap growth per restart cycle.Root cause:
UaSCBinaryChannel.Dispose()did not close the underlyingSocket. PendingReceiveAsyncoperations kept the channels pinned viaSocketAsyncEngine, preventing GC of the entire channel object graph (certificate caches, protocol buffers, etc.).Changes
UaSCBinaryChannel.Dispose()— Close socket to cancel pending async I/O and allow GC of disposed channelsServerBase.Dispose()— UnsubscribeConnectionStatusChangedfrom transport listeners before disposing themVerification
Tested with an in-process server restart loop over 786 cycles (~17 hours). Without the fix, heap grew steadily (~15 KB/cycle). With the fix, heap stabilized at 80-81 MB after initial warmup and remained flat for the entire run.
Test plan
TcpServerChannelcount stays at active-only (3-5) instead of accumulatingNotes
Do I need to add tests for this?
Disclaimer: This bug has been found with hours of manual work with AI and my local OPC UA stress test application and should be safe - please review the changes as I don't really know the OPC UA code base in depth.