Skip to content

[UTXO-BUG] Fix non-atomic mempool_clear_expired — wrap in BEGIN IMMEDIATE - #7954

Closed
sumarna35 wants to merge 3 commits into
Scottcjn:mainfrom
sumarna35:fix/mempool-atomic-expiry
Closed

sumarna35 wants to merge 3 commits into
Scottcjn:mainfrom
sumarna35:fix/mempool-atomic-expiry

Conversation

@sumarna35

@sumarna35 sumarna35 commented Jul 13, 2026 •

Copy link
Copy Markdown

Fix for UTXO Red Team Audit Finding #1

Refs: Scottcjn/rustchain-bounties#2819

Bug

mempool_clear_expired() deletes mempool inputs+mempool rows per-tx without transaction wrapper. Crash between DELETEs → orphan state (UTXO prematurely unlocked, stale mempool entry remains).

Fix

Wrap delete loop in BEGIN IMMEDIATE + COMMIT — same pattern as mempool_remove().

Audit Finding

Full report: Scottcjn/rustchain-bounties#2819 (comment)

Wallet: RTCad35e1c1746560097ce9989abc140e0529b87163

…IATE

The per-tx DELETE loop in mempool_clear_expired() ran outside a
transaction wrapper. A crash between the utxo_mempool_inputs DELETE
and the utxo_mempool DELETE leaves inconsistent state: UTXO lock
prematurely released while orphan mempool entry remains.

Same class of bug already fixed in mempool_remove() (BUG-1).
Apply identical BEGIN IMMEDIATE wrapping pattern.

Found during Scottcjn#2819 UTXO Red Team audit.
Refs: Scottcjn/rustchain-bounties#2819
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Non-doc PRs have a BCOS-L1 or BCOS-L2 label
  • Doc-only PRs are exempt from BCOS tier labels when they only touch docs/**, *.md, or common image/PDF files
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related size/S PR: 11-50 lines labels Jul 13, 2026
@Scottcjn

Copy link
Copy Markdown
Owner

Maintainer review: the stated partial-commit failure is not reproduced with the connection settings used here. Python sqlite3 opens an implicit transaction on the first DELETE under the default isolation mode; both DELETEs remain in that transaction until conn.commit(), and closing after only the first DELETE rolls it back. I verified this directly: conn.in_transaction becomes true after the first DELETE and both rows remain after closing without a commit.

BEGIN IMMEDIATE may still provide a useful earlier write lock, but this patch begins it after the SELECT and currently has no regression test demonstrating a behavior change. Please add a deterministic test that fails on current main and passes this branch, and revise the docstring to describe the demonstrated locking issue rather than a partial commit. The red aggregate CI result is also from the stale main-branch baseline covered by #7957, not evidence for this patch.

@IcanBENCHurCAT IcanBENCHurCAT left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — audit finding fix. Wraps paired DELETEs in BEGIN IMMEDIATE to prevent orphan UTXO rows on crash. ✅

@Scottcjn

Copy link
Copy Markdown
Owner

Verified, with one correction to the framing.

The concurrency hardening is real and worth taking: grabbing the write lock up front matches the BEGIN IMMEDIATE pattern mempool_add already uses, plus the empty-set early return is a nice touch. But the stated corruption impact is overstated. _conn() uses default deferred isolation, so the pre-fix DELETEs already run inside one implicit transaction committed once at the end, and a mid-loop crash already rolls everything back. There are no orphan utxo_mempool_inputs rows under the current config.

So this is a safe belt-and-suspenders improvement, not the critical corruption fix the description implies. The change is correct and deadlock-safe (timeout=30 makes IMMEDIATE block-and-retry rather than raise). Red CI is unrelated baseline drift. Recommend rebase and merge as a hardening cleanup, and scored as a minor finding rather than high severity.

Per @Scottcjn review on 2026-07-14: the previous description
implied a partial-commit corruption that does not exist under
Python sqlite3's default deferred isolation (the implicit txn is
held until conn.commit(), so a mid-loop crash rolls back).

The BEGIN IMMEDIATE wrap is still valuable as a hardening change:
it matches the pattern already used by mempool_add and
mempool_remove, and prevents concurrent writers from observing a
half-cleared mempool. Update the docstring to describe the
demonstrated lock-discipline benefit rather than the unproven
corruption claim.

Refs: Scottcjn#7954, Scottcjn/rustchain-bounties#2819
@sumarna35

Copy link
Copy Markdown
Author

Thanks again for the thorough review, and you were right: the partial-commit
corruption claim does not hold up under Python sqlite3's default deferred
isolation (the implicit transaction is held until conn.commit(), so a
mid-loop crash rolls everything back). I'm withdrawing that framing.

What I changed (just pushed to the branch, commit 325cdef3):

  1. Docstring reworded to describe the demonstrated benefit
    (write-lock hardening / matching the existing mempool_add /
    mempool_remove pattern) rather than the unproven corruption claim.

  2. On the regression test: I tried to construct one, but I think you are
    correct that there is no deterministic test that fails on current main
    and passes this branch under the deferred-isolation model. A
    half-finished sweep cannot be observed externally; only a contention
    timing test would, and those are flaky in CI. If you have a specific test
    scenario in mind, I'd appreciate a pointer — otherwise I'd rather not
    land a placebo test that just measures the IMMEDIATE keyword without
    proving the failure mode you actually want to prevent.

Scoring: I accept your re-tiering to minor / hardening. If this is still
worth landing as a lock-discipline cleanup, great; if you'd rather close it
as not-pursued, I understand. Either way, thanks for the time and the
correction — the audit report on #2819 will be updated to reflect the
revised impact assessment.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 21 days. It will be closed in 14 days unless updated.
Need help finishing? Ask in the PR comments — we're happy to assist!

@github-actions github-actions Bot added the stale Inactive — will close soon label Aug 24, 2026
@Scottcjn

Scottcjn commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Closing as superseded: mempool_clear_expired on main now runs inside an explicit transaction (there is a stress test asserting it). Thank you for finding it. — Sophia

@Scottcjn Scottcjn closed this Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related size/S PR: 11-50 lines stale Inactive — will close soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants