Fix memory leaks in server disposal#3560
Merged
marcschier merged 1 commit intoOPCFoundation:masterfrom Feb 17, 2026
Merged
Conversation
20e9725 to
d2ea5cd
Compare
d2ea5cd to
3e9e183
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3560 +/- ##
===========================================
+ Coverage 51.86% 64.68% +12.81%
===========================================
Files 370 458 +88
Lines 78618 96776 +18158
Branches 13650 16265 +2615
===========================================
+ Hits 40779 62601 +21822
+ Misses 33705 29161 -4544
- Partials 4134 5014 +880 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
marcschier
approved these changes
Feb 17, 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.
Two related disposal bugs prevent the server object graph from being garbage collected after
StopAsync/Disposewhen clients were connected. Discovered during resilience testing of a long-running OPC UA server with repeated restart cycles (~2 MB leaked per cycle).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.
1.
UaSCUaBinaryChannel.Dispose()does not dispose theSocketpropertyPending async I/O keeps the socket alive in
SocketAsyncEngine, which retains the entire channel/listener graph and prevents GC. Fixed by addingUtils.SilentDispose(Socket)inDispose.Double-dispose is safe because
TcpMessageSocket.Close()nullsm_socketbefore disposing, making subsequentDispose()calls a no-op.TcpServerChannel.Dispose()acquiresDataLock, preventing races withForceChannelFault/SendErrorMessage.2.
StandardServer.OnServerStarted()subscribesCertificateValidator.CertificateUpdatebut never unsubscribesSince the
CertificateValidatoris shared (fromApplicationConfiguration) and outlives the server, the delegate retains every disposed server instance. Fixed by unsubscribing inStandardServer.Dispose(bool)inside thedisposingguard.GC retention chains (before fix)
Verification
Tested in a long-running OPC UA server with chaos testing (repeated server stop/start cycles with connected clients):
Notes
Dispose(bool)methods, inside thedisposingguard