fix(bridge): enforce TaskCompletionSource handshake gate to prevent connection race#3110
Open
HarshalPatel1972 wants to merge 1 commit into
Open
Conversation
Collaborator
|
Will twill try to look tomorrow before the next 3.0.x release Note that since this targets "main" which is now V3, there is no more pipe-reader, but that doesn't mean the async goes away. However, there is now technically support for running the read loop on dedicated sync IO threads - I have not exposed this on the public API yet, however. |
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.
Description
Fixes #2989 / #3085
This PR introduces an explicit, asynchronous
TaskCompletionSource<bool>handshake coordination gate within thePhysicalBridgeprocessing loop. This completely eliminates an intermittent protocol corruption race condition that occurs when the .NET ThreadPool experiences heavy starvation during rapid socket reconnection cycles.Technical Root Cause
When a multiplexed socket connection drops under thread strain, the
PhysicalBridgecorrectly initiates an asynchronous restart. However, the subsequent server handshake commands (HELLO,AUTH,CLIENT SETNAME) rely on parsing asynchronous callbacks handled downstream by thePipeReaderrunning on the shared ThreadPool.Under severe ThreadPool exhaustion, these parsing continuations can be heavily delayed. Prior to this patch, the connection state was flagged as available prematurely relative to the actual protocol acknowledgment. As a result, concurrent user application threads checking the connection status would immediately start dispatching application-level data payloads down the socket. Because these payloads interleave into the raw socket pipe before the server completely processes and acknowledges the initial
AUTHhandshake, the protocol boundaries become completely misaligned, throwing deceptiveNOAUTHorUnauthorizedexceptions and locking the bridge into an unstable retry loop.Resolution Strategy
TaskCompletionSource<bool>(_handshakeCompletion) assigned withTaskCreationOptions.RunContinuationsAsynchronouslyto isolate the lifetime of the socket connection sequence.PhysicalBridge.cs. If a data payload attempts a transmission while the state isConnectedEstablishing, the calling task cleanly executes anawait _handshakeCompletion.Task.ConfigureAwait(false);.AUTH,HELLO,CLIENT SETNAME). OnceOnFullyEstablished()is safely invoked following the final protocol validation,_handshakeCompletion.TrySetResult(true)is fired, atomically releasing any queued application tasks to send their data down a perfectly authenticated stream.Verification
ThreadPool.SetMinThreads(1, 1)) to ensure high-concurrency requests cleanly queue behind the establishing gate without dropping or triggering thread lockups.