Make splice select+reserve atomic under the wallet lock#43
Open
amackillop wants to merge 1 commit into
Open
Conversation
A splice keeps its coins live in the in-memory reserved_utxos set rather than marking them spent, because signing happens later in a detached event. An earlier deadlock fix made the wallet and reserved locks strictly non-overlapping: select_utxos_inner read reserved, took the wallet lock, selected a coin, dropped the wallet lock, and only then published the reservation. The other selection paths likewise snapshotted reserved before taking the wallet lock. That left a window where a splice-selected coin was both live in the BDK graph and absent from the reserved set. A concurrent open or on-chain withdrawal could read the stale reserved set under its own wallet lock and select the same coin, double-spending it. The service runs LSPS4 splices in the background while exposing gRPC payments, so these two can race. Establish a wallet-outer / reserved-inner lock hierarchy instead: every selection path reads reserved while holding the wallet lock, and select_utxos_inner publishes the reservation before dropping it. Because all selection paths serialize on the wallet lock, select+reserve is now one critical section and no two builds can pick the same coin. Opens were never affected: they mark inputs spent in the graph under the same lock. Deadlock-free because no path holds reserved while acquiring the wallet lock; the release paths take reserved alone. Considered folding reserved_utxos into the wallet Mutex to make the atomicity type-enforced, but that touches every inner.lock() site and makes release paths contend on the full wallet lock. The hierarchy is small and documented once on the Wallet struct.
22e40c2 to
1c32fee
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.
A splice keeps its coins live in the in-memory reserved_utxos set rather than marking them spent, because signing happens later in a detached event. An earlier deadlock fix made the wallet and reserved locks strictly non-overlapping: select_utxos_inner read reserved, took the wallet lock, selected a coin, dropped the wallet lock, and only then published the reservation. The other selection paths likewise snapshotted reserved before taking the wallet lock.
That left a window where a splice-selected coin was both live in the BDK graph and absent from the reserved set. A concurrent open or on-chain withdrawal could read the stale reserved set under its own wallet lock and select the same coin, double-spending it. The service runs LSPS4 splices in the background while exposing gRPC payments, so these two can race.
Establish a wallet-outer / reserved-inner lock hierarchy instead: every selection path reads reserved while holding the wallet lock, and select_utxos_inner publishes the reservation before dropping it. Because all selection paths serialize on the wallet lock, select+reserve is now one critical section and no two builds can pick the same coin. Opens were never affected: they mark inputs spent in the graph under the same lock.
Deadlock-free because no path holds reserved while acquiring the wallet lock; the release paths take reserved alone.
Considered folding reserved_utxos into the wallet Mutex to make the atomicity type-enforced, but that touches every inner.lock() site and makes release paths contend on the full wallet lock. The hierarchy is small and documented once on the Wallet struct.