rpc: Let dumptxoutset fail early on stale targets and concurrent runs #36266

Open
fjahr wants to merge 3 commits from 2026-09-dump-fail-early into master AGit
fjahr commented 2026-09-16 07:25:34 +00:00 (Migrated from github.com)

This improves handling of two edge cases where dumptxoutset currently only fails after doing unnecessary and possibly incorrect work:

Stale rollback target (first commit): ParseHashOrHeight accepts any known block hash, and the rollback path never checks the target is on the active chain. The full UTXO set is copied and blocks are disconnected back from the tip only to fail when the target block height is reached. This is now rejected before all that as an invalid param, matching the check in getchaintxstats and scanblocks. This is a pretty unlikely scenario but the fix is also very small.

Concurrent calls (second commit): Nothing prevented triggering two runs at once and since this is a relatively long running operation I don't think it is that unlikely that this might happen by accident or that users think this might actually be a possible thing to do if you want two different dumps. On master currently both runs would write to <path>.incomplete, so the second dump to the same path corrupts the file the first one reports as successfully written. And also the prune lock has a fixed name, so the second run overwrites the first's height and the first to finish deletes the others lock mid-rollback. A second call now fails immediately with "dumptxoutset is already running". TemporaryPruneLock is extended and renamed to DumptxoutsetGuard and handles both the general running of multiple instances and the lock management. The alternative to this approach would be to allow for concurrent runs and fix that behavior but it seemed to me like that would be overengineering considering that the audience for this functionality should be very small.

This improves handling of two edge cases where `dumptxoutset` currently only fails after doing unnecessary and possibly incorrect work: Stale rollback target (first commit): `ParseHashOrHeight` accepts any known block hash, and the rollback path never checks the target is on the active chain. The full UTXO set is copied and blocks are disconnected back from the tip only to fail when the target block height is reached. This is now rejected before all that as an invalid param, matching the check in `getchaintxstats` and `scanblocks`. This is a pretty unlikely scenario but the fix is also very small. Concurrent calls (second commit): Nothing prevented triggering two runs at once and since this is a relatively long running operation I don't think it is that unlikely that this might happen by accident or that users think this might actually be a possible thing to do if you want two different dumps. On master currently both runs would write to `<path>.incomplete`, so the second dump to the same path corrupts the file the first one reports as successfully written. And also the prune lock has a fixed name, so the second run overwrites the first's height and the first to finish deletes the others lock mid-rollback. A second call now fails immediately with "dumptxoutset is already running". `TemporaryPruneLock` is extended and renamed to `DumptxoutsetGuard` and handles both the general running of multiple instances and the lock management. The alternative to this approach would be to allow for concurrent runs and fix that behavior but it seemed to me like that would be overengineering considering that the audience for this functionality should be very small.
DrahtBot commented 2026-09-16 07:25:42 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36266 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36266.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36266#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#34534](https://github.com/bitcoin/bitcoin/pull/34534) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34534.svg"></sub> (rpc: Manual prune lock management (Take 2) by fjahr) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
cyb3ralbert commented 2026-10-22 07:35:45 +00:00 (Migrated from github.com)

dumptxoutset checks ActiveChain().Contains(*target_index) under cs_main at blockchain.cpp:3161, but CreateRolledBackUTXOSnapshot pins the tip under a separate lock at :3296.

Is it intentional that if the target leaves the active chain between these two points, the operation can still reach CHECK_NONFATAL(rollback_cache.GetBestBlock() == target->GetBlockHash()) at :3365?

I reproduced this on regtest by blocking the first rollback call on a FIFO, invalidating the target block, generating 8 new blocks, and then allowing the dump to proceed. Both master and the PR branch hit the check, although the node remains up.

Would it make sense to revalidate the target against the pinned tip immediately after tip = chainstate.m_chain.Tip()?

if (tip->GetAncestor(target->nHeight) != target) {
    throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain");
}

With this check, the same reproduction returns -8 Block is not in main chain, and the subsequent latest dump succeeds. The relevant tests (rpc_dumptxoutset.py and feature_assumeutxo.py) also pass.

Tested on regtest, head 31000db0ea, Linux aarch64 (Debian 13), GCC 14.2.0, Release.

`dumptxoutset` checks `ActiveChain().Contains(*target_index)` under `cs_main` at `blockchain.cpp:3161`, but `CreateRolledBackUTXOSnapshot` pins the tip under a separate lock at `:3296`. Is it intentional that if the target leaves the active chain between these two points, the operation can still reach `CHECK_NONFATAL(rollback_cache.GetBestBlock() == target->GetBlockHash())` at `:3365`? I reproduced this on regtest by blocking the first `rollback` call on a FIFO, invalidating the target block, generating 8 new blocks, and then allowing the dump to proceed. Both master and the PR branch hit the check, although the node remains up. Would it make sense to revalidate the target against the pinned tip immediately after `tip = chainstate.m_chain.Tip()`? ```cpp if (tip->GetAncestor(target->nHeight) != target) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain"); } ``` With this check, the same reproduction returns `-8 Block is not in main chain`, and the subsequent `latest` dump succeeds. The relevant tests (`rpc_dumptxoutset.py` and `feature_assumeutxo.py`) also pass. Tested on regtest, head 31000db0ea, Linux aarch64 (Debian 13), GCC 14.2.0, Release.
First-time contributor

🟠 Minor · Active-command polling races with guard acquisition

The concurrency test can fail despite correct serialization if the FIFO worker stalls between active-command registration and guard acquisition.

Location: test/functional/rpc_dumptxoutset.py:70 (head).

🔵 Suggestion · Stale-target test misses late-rejection regressions

The stale-target test would still pass if rejection occurred only after copying the UTXO set and rolling back.

Location: test/functional/rpc_dumptxoutset.py:56 (head).

Review coverage was incomplete.


Reviewed: 755a45b95fc9 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:ed3d34ba0c0b28bb281b265eaca62222aae3a796 --> <!-- ralph-head:755a45b95fc9f8f24f3941b6ad17d87a6deddb00 --> <!-- ralph-case:1f8ae3654e4382bc278c0970339f158673107e08726ee6a790b9736a1ef9aa67 --> **🟠 Minor · Active-command polling races with guard acquisition** The concurrency test can fail despite correct serialization if the FIFO worker stalls between active-command registration and guard acquisition. Location: [`test/functional/rpc_dumptxoutset.py:70`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/755a45b95fc9f8f24f3941b6ad17d87a6deddb00/test/functional/rpc_dumptxoutset.py#L70>) (head). **🔵 Suggestion · Stale-target test misses late-rejection regressions** The stale-target test would still pass if rejection occurred only after copying the UTXO set and rolling back. Location: [`test/functional/rpc_dumptxoutset.py:56`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/755a45b95fc9f8f24f3941b6ad17d87a6deddb00/test/functional/rpc_dumptxoutset.py#L56>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`755a45b95fc9`](<https://git.fish.foo/bitcoin/bitcoin/commit/755a45b95fc9f8f24f3941b6ad17d87a6deddb00>) · [Full review report](<https://review.fish.foo/traces/36266-755a45b95fc9f8f24f3941b6ad17d87a6deddb00-2098-2.html>) · [Previous reports](<https://review.fish.foo/traces/36266/>) [permalink](<https://review.fish.foo/traces/36266-755a45b95fc9f8f24f3941b6ad17d87a6deddb00-2098-2.html>)
fjahr commented 2026-11-04 01:28:00 +00:00 (Migrated from github.com)

Would it make sense to revalidate the target against the pinned tip immediately after tip = chainstate.m_chain.Tip()?

Not a big problem but it seemed worth addressing. I moved the check there instead of adding a second one, so there is still only one check now and it runs against the pinned tip. This also covers type=rollback without an explicit rollback option. I like this better because it's cleaner.

The one draw-back of this that I ran into: Since the check now runs after the output file is opened, a stale target would leave an empty .incomplete file behind. I added 755a45b95f to remove that file when a dump fails. It also cleans up after the other errors that happen once the file is open, so it doesn't just fix the regression mentioned above but also is a general improvement for another (minor) issue.

> Would it make sense to revalidate the target against the pinned tip immediately after `tip = chainstate.m_chain.Tip()`? Not a big problem but it seemed worth addressing. I moved the check there instead of adding a second one, so there is still only one check now and it runs against the pinned tip. This also covers `type=rollback` without an explicit `rollback` option. I like this better because it's cleaner. The one draw-back of this that I ran into: Since the check now runs after the output file is opened, a stale target would leave an empty `.incomplete` file behind. I added 755a45b95fc to remove that file when a dump fails. It also cleans up after the other errors that happen once the file is open, so it doesn't just fix the regression mentioned above but also is a general improvement for another (minor) issue.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36266
No description provided.