Files
Marc Schier 8d3eccd41c Fix concurrent channel reconnect certificate leaks (#4356)
# Description

Fix the intermittent one-certificate leak reported by the macOS Sessions
suite.

The leak had two concurrent ownership paths:

1. `ChannelEntry.OpenInitialAsync` could complete after a reconnect
cycle had already installed a
newer transport. The initial-open path overwrote `m_underlying`,
orphaning the reconnect
   transport and its certificate handles.
2. `UaSCUaBinaryTransportChannel.ReconnectAsync` did not dispose a newly
created inner channel when
`ConnectAsync` failed. Initial-open cleanup could also be skipped when
it reused the caller's
   already-cancelled token.

The fix keeps the transport installed first, closes and disposes the
losing candidate, disposes
failed reconnect candidates, and performs failed initial-open cleanup
independently of caller
cancellation.

This also adds opt-in Release allocation tracking for outstanding
`Certificate` handles. Set
`OPCUA_CERTIFICATE_LEAK_TRACKING=1` to include allocation stacks and
NUnit suite attribution in leak
failures. The contributor workflow is documented in
`docs/DeveloperGuide.md`.

## Tests and verification

- Added a deterministic `ChannelEntry` race test that orchestrates
concurrent initial open and
  reconnect, then verifies the losing transport is closed and disposed.
- Added deterministic UA-binary transport tests for failed reconnect
certificate cleanup and retry
  after a cancelled initial open.
- Pre-fix macOS diagnostic run reproduced 3 leaks in 15 full Sessions
runs:

https://github.com/OPCFoundation/UA-.NETStandard/actions/runs/33329288604
- Post-fix targeted macOS run passed 50/50 exact reproducer executions:

https://github.com/OPCFoundation/UA-.NETStandard/actions/runs/33334875967
- Post-fix full macOS run passed 15/15 complete Sessions suites:

https://github.com/OPCFoundation/UA-.NETStandard/actions/runs/33335718790
- Full local net10 Sessions suite passed: 775 passed, 379 skipped, 1,154
total.
- Focused ownership regressions passed on net10 and net48.
- `Opc.Ua.Core` built warning-free across all supported target
frameworks.
- Final branch Build/Test, CodeQL, and NuGet workflows passed.

The temporary reproduction workflow and trigger tags were removed after
verification.

## Related Issues

- Fixes #4338

## Checklist

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [x] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-31 15:04:50 +02:00
..