Repository navigation
Fix sends racing with disconnect and handshake cancellation - #1374
Merged
Merged
Conversation
NicolasDorier
force-pushed
the
fix/send-disconnect-race
branch
from
November 6, 2026 09:16
e1781fd to
c9ce35e
Compare
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
Related to btcpayserver/NBXplorer#578 and btcpayserver/NBXplorer#579.
SendMessageAsyncchecksIsConnectedbefore invoking the outgoing filters and enqueueing the message. If the sender stops in between, it fails the messages it can see and replaces the queue. A late enqueue can land in either the already-drained queue or its replacement, leaving the returned task pending forever.VersionHandshakeAsyncawaits its sends without observing its cancellation token, so this race can also leave a handshake stuck even after cancellation.Changes
CompleteAddingbefore draining it during sender shutdown.WithCancellationhelper. Cancellation stops waiting; it does not retract an already accepted send.This fixes NBitcoin's lost-send race; NBXplorer still needs its separate fix for an old node's disconnect handler disposing a replacement connection.
Validation
The regression tests used for validation were removed from the final diff at the maintainer's request. Only the production fix is included.
TargetFrameworkOverride=netstandard2.0, exercising the legacy cancellation and receive paths on the .NET 10 test runner.net10.0,net472,netstandard2.1, andnetstandard2.0.