fix: make PooledSocket.DisposeSocket thread-safe#272
Merged
cnblogs-dudu merged 1 commit intoMay 15, 2026
Conversation
PooledSocket.Connect() registers a CTS cancellation callback that calls DisposeSocket() on a thread-pool thread when the connection timeout fires. The same method also calls DisposeSocket() from the caller thread when the post-connect check sees the socket is not connected. Without synchronisation, both paths race to call _socket.Dispose() and null _socket concurrently, producing ObjectDisposedException or NullReferenceException under load. Fix by using Interlocked.Exchange(ref _socket, null) in DisposeSocket() so exactly one concurrent caller gets the non-null reference and disposes it; all others receive null and no-op. Change DisposeSocket visibility to private protected and add InternalsVisibleTo so the concurrent-disposal race can be tested directly without reflection. Adds PooledSocketRaceReproTests covering the concurrent-disposal scenario. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thanks for your contribution! |
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.
Problem
PooledSocket.Connect()registers aCancellationTokenSourcecallback (Cancel()) to enforce the connection timeout. When the timeout fires,Cancel()callsDisposeSocket()on a thread-pool thread. The same code path callsDisposeSocket()again on the caller thread when the post-connect check finds the socket is not connected — with no protection against both running concurrently.This creates a race with two failure modes:
_socket != null, both call_socket.Dispose(), the second call throwsObjectDisposedException.Cancel()sets_socket = nullbetween the null-check and_socket.Dispose()on the caller thread.These races are low-probability under normal conditions but reproducible under connection-timeout pressure (e.g. connecting to a slow or firewalled host) and more likely on ARM / weakly-ordered architectures.
Fix
Rewrite
DisposeSocket()to useInterlocked.Exchange(ref _socket, null):Interlocked.Exchangeatomically captures the socket reference and nulls the field in one operation. Exactly one concurrent caller gets the non-null reference and disposes it; all others receivenulland no-op. This makes the method safe to call concurrently from any number of threads.The visibility is changed from
privatetoprivate protectedso the method can be exercised directly in tests (via a thin subclass in the test project) without reflection.InternalsVisibleTois scoped to the test assembly only, using the full public key to respect the existing strong-name signing.Testing
Added
PooledSocketRaceReproTestswhich callsDisposeSocket()from 64 concurrent tasks and asserts no exceptions are raised.Made with Cursor :)