Skip to content

fix(utxo): stop swallowing stale-mempool cleanup errors after committed spends - #8519

Merged
Scottcjn merged 1 commit into
mainfrom
fix/utxo-mempool-cleanup-surface-errors
Sep 25, 2026
Merged

Scottcjn merged 1 commit into
mainfrom
fix/utxo-mempool-cleanup-surface-errors

Conversation

@Scottcjn

@Scottcjn Scottcjn commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Reported by @expertedgevault-rgb (#2819).

Problem

After apply_transaction() spends inputs it evicts mempool txs that depend on the spent boxes (as inputs or data_inputs). Every failure there was swallowed twice: _evict_stale_data_input_txs() caught all exceptions and returned 0, and apply_transaction() wrapped the call in except Exception: pass. Stale dependents could stay in the pool with no log, counter, or return signal.

Two more problems showed up while writing the tests (caller-connection path, which /utxo/transfer uses):

  1. Partial eviction leaked. If the 2nd DELETE failed, the 1st (claim rows) still rode along on the caller's COMMIT. That left a mempool row whose inputs were no longer claimed.
  2. False green. If SQLite aborted the caller's whole transaction during eviction, apply_transaction() still returned True for a spend that had been rolled back. The endpoint would then carry on with its dual-write account writes.

Fix (only node/utxo_db.py changes)

  • The helper now raises and is atomic. On its own connection it uses BEGIN IMMEDIATE. On a caller's connection it uses a SAVEPOINT, so the caller's spend writes are never touched.
  • A spend is never rolled back because cleanup failed. The failure is logged at ERROR with the tx_id and spent box ids, and it increments mempool_eviction_failure_count() (process-local).
  • Fail closed: if the caller's transaction is gone, or the savepoint rollback itself failed, apply_transaction() raises instead of returning True.
  • Defense in depth: a row whose tx_data_json spends a spent box is evicted even when its claim rows are missing. Non-dict rows no longer abort the scan.

Tests

New tests/test_utxo_mempool_eviction_failure_surfaced.py (17 tests). Failures are injected with real SQLite triggers (RAISE(ABORT) / RAISE(ROLLBACK)) plus one monkeypatch.

  • Old code (main): the tests that pin the new behavior fail. The normal-cleanup and claim-row coverage tests pass, which is expected.
  • New code: 17/17 pass.
  • UTXO suite (tests/*utxo*, node/test_utxo*, node/tests/*utxo*): before 343 passed / 21 failed, after 360 passed / 21 failed. The 21 failures are identical before and after (existing PoC tests).

Merge considerations

  • New behavior on the caller-connection path: it now raises (endpoint → rollback + 500) in cases where it used to return True. This only happens when SQLite has aborted the transaction or the savepoint rollback failed.
  • The counter is not wired to /metrics or health yet. That can be a follow-up.
  • Not deployed.

🤖 Generated with Claude Code

@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 tests Test suite changes size/L PR: 201-500 lines labels Sep 24, 2026
@Scottcjn
Scottcjn force-pushed the fix/utxo-mempool-cleanup-surface-errors branch from 40c34d6 to 1f39ab0 Compare September 24, 2026 22:18
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ BCOS v2 Scan Results

Metric Value
Trust Score 36/100
Certificate ID BCOS-2e687bd4
Tier L2 (not met)

BCOS Badge

What does this mean?

The BCOS (Beacon Certified Open Source) engine scans for:

  • SPDX license header compliance
  • Known CVE vulnerabilities (OSV database)
  • Static analysis findings (Semgrep)
  • SBOM completeness
  • Dependency freshness
  • Test infrastructure evidence
  • Review attestation tier

Full report | What is BCOS?


BCOS v2 Engine - Free & Open Source (MIT) - Elyan Labs

…ed spends

After apply_transaction() spends inputs it evicts mempool txs that depend
on the spent boxes. Every failure in that cleanup was swallowed twice:
_evict_stale_data_input_txs() caught all exceptions and returned 0, and
apply_transaction() wrapped the call in `except Exception: pass`. Stale
dependents could silently stay in the pool with no log or signal.

Now:
- The helper raises instead of returning 0, and is atomic: BEGIN
  IMMEDIATE on its own connection, a SAVEPOINT on a caller's connection,
  so a mid-eviction failure can no longer commit claim-row DELETEs while
  leaving the mempool row behind (orphaned, unclaimed inputs).
- apply_transaction() never rolls back a spend over a cleanup failure;
  it logs at ERROR with tx_id + spent box ids and increments a
  process-local counter (mempool_eviction_failure_count()).
- Fail closed on the caller-connection path: if SQLite aborted the
  caller's whole transaction during eviction (spend gone), or the
  savepoint rollback itself failed, apply_transaction() raises instead
  of returning True for a spend that is not there.
  A RELEASE failure after a successful ROLLBACK TO (or after a
  successful eviction) is only logged: the caller's writes are intact
  and its COMMIT closes the leftover savepoint.
- Defense in depth: a mempool row whose tx_data_json spends a spent box
  is evicted even if its utxo_mempool_inputs claim rows are missing;
  non-dict / malformed rows no longer abort the whole scan.

Reported by @expertedgevault-rgb (#2819).

Signed-off-by: Scott <scottbphone12@gmail.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Scottcjn
Scottcjn force-pushed the fix/utxo-mempool-cleanup-surface-errors branch from 1f39ab0 to 07c0826 Compare September 24, 2026 22:45
@Scottcjn Scottcjn removed the BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) label Sep 24, 2026
@Scottcjn

Copy link
Copy Markdown
Owner Author

Changes after the outside review (amended into the single signed-off commit, now 07c0826):

  1. Savepoint RELEASE is handled separately from ROLLBACK TO (Codex). _EvictionRollbackFailed is now raised only when ROLLBACK TO fails. That is the one case where partial DELETEs could ride along on the caller's COMMIT. A RELEASE failure is logged at ERROR and not raised. This covers a RELEASE after a successful rollback and the final RELEASE after a successful eviction. SQLite leaves the savepoint on its stack, and the caller's COMMIT or ROLLBACK closes it with the outer transaction. The spend is kept.
    • New tests deny RELEASE using set_authorizer(SQLITE_SAVEPOINT). One case has eviction failing and one has eviction succeeding. Each test asserts that the caller's COMMIT succeeds and the spend is durable. Both tests fail on the previous PR head and pass now.
  2. Test gaps (Grok):
    • Strategy 1 (the claim-row lookup) is now exercised directly by a helper test. The row has no inputs in its JSON, only a claim row. It is also exercised end to end: the confirmed tx's own mempool entry is evicted with its claims, on both connection paths.
    • _EvictionRollbackFailed now has a test. It denies ROLLBACK TO, and the test asserts apply_transaction raises and nothing is committed.
    • Malformed rows (list, string, null, bad JSON, odd shapes) are now covered. The test asserts they no longer abort the scan: real dependents are evicted and the malformed rows are left alone. This test fails on main.
  3. Removed the duplicate BCOS-L2 label I had added by hand. BCOS-L1 stays.

UTXO suite: before (main) 343 passed / 21 failed. After: 360 passed / 21 failed. The same 21 tests fail before and after; those failures already exist on main. The new file has 17/17 passing.

@github-actions github-actions Bot added size/XL PR: 500+ lines and removed size/L PR: 201-500 lines labels Sep 24, 2026
@Scottcjn
Scottcjn merged commit b03f59d into main Sep 25, 2026
14 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

RTC Reward

This merged PR earned 5 RTC — sent to Scottcjn.

RustChain Bounty Program

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) node Node server related size/XL PR: 500+ lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants