fix: address PR #198 review — lock Deactivate, avoid stale _client in status callback - #199
Merged
anthony-lopez-pd merged 1 commit intoSep 7, 2026
Conversation
…callback Three review comments on the disposal change, all valid. 1. Deactivate() disposed RetryTimer and the client without holding connectLock, so it could race Connect()/Disconnect()/Reconnect() and WaitAndTryReconnect's lock-protected RetryTimer.Reset. Now wrapped in connectLock/try-finally like the other lifecycle methods. Verified no same-thread re-entry: nothing DisposeClient() calls takes the lock. 2. Client_SocketStatusChange's connected path called _client.ReceiveDataAsync(...) on the field rather than the callback's own TCPClient. Since DisposeClient() now nulls _client, a queued status change could arrive afterwards and throw. It now uses the `client` argument, guarded with a null check and a ReferenceEquals test so events from a superseded socket don't re-arm receive on a dead client. The ConnectionChange event still fires as before — the guard only skips the receive re-arm, not the notification. 3. The dispose catch logged ex.Message, dropping the stack trace and any inner exception. Now logs the full exception, which is exactly what a disposal race needs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jkdevito
approved these changes
Sep 7, 2026
anthony-lopez-pd
merged commit Sep 7, 2026
9045b33
into
maintenance-1x-tcp-client-dispose-socket
2 checks passed
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.
Addresses all three review comments on #198. Targets #198's branch rather than
maintenance-1xso merging this updates that PR in place and keeps its review threads intact.
Direct pushes to the #198 branch are refused by branch protection ("Changes must be made through
a pull request"), which is why this arrives as a stacked PR rather than another commit.
Deactivate()doesn't holdconnectLockconnectLock/try-finallyClient_SocketStatusChangeuses_clientnot the callback'sclientReferenceEqualsguardsex.Message1.
Deactivate()underconnectLockIt disposed
RetryTimerand the client without the lock, so it could raceConnect()/Disconnect()/Reconnect()andWaitAndTryReconnect's lock-protectedRetryTimer.Reset. My disposal change made this materially worse by nulling_client.Verified no same-thread re-entry: nothing
DisposeClient()calls takesconnectLock.2. Stale
_clientin the status callbackThe connected path called
_client.ReceiveDataAsync(...)on the field rather than thecallback's own
TCPClient. Now thatDisposeClient()nulls_client, a queued status changearriving afterwards would throw — a failure mode my change introduced.
Now uses the
clientargument with a null check and aReferenceEquals(client, _client)test,so events from a superseded socket don't re-arm receive on a dead client.
The
ConnectionChangeevent still fires as before — the guard only skips the receivere-arm, not the notification. I deliberately avoided an early
return, which would have changedevent semantics beyond what the comment asked for.
3. Log the full exception
A disposal race is exactly the case where the stack trace and any inner exception matter.
One note on that comment's premise: it said the sibling
GenericSecureTcpIpClient"frequently"logs the full exception. It's mixed — full
exin one place,ex.Messagein two others. Theunderlying argument holds regardless, so I made the change.
Compiles clean under MSBuild. Line endings unchanged (LF-only, matching the file).