Skip to content

fix: resolve race condition in update_from_tx (PM-19905)#767

Draft
m2ux wants to merge 1 commit intomainfrom
fix/PM-19905-race-condition-update-from-tx
Draft

fix: resolve race condition in update_from_tx (PM-19905)#767
m2ux wants to merge 1 commit intomainfrom
fix/PM-19905-race-condition-update-from-tx

Conversation

@m2ux
Copy link
Contributor

@m2ux m2ux commented Feb 24, 2026

Summary

Fix a read-modify-write race condition in LedgerContext::update_from_tx where the ledger state is cloned outside the mutex then written back under a separate lock, enabling concurrent callers to overwrite each other's updates.

🎫 Ticket 📐 Engineering


Motivation

The tx_context function clones ledger_state under the mutex then immediately releases it. The subsequent update_from_tx modifies the clone and re-acquires the mutex to write back, creating a window where concurrent callers can clone the same baseline, compute divergent results, and overwrite each other. This was identified as Issue AJ in the Least Authority audit report (October 2025), rated Low severity but potentially causing lost updates, inconsistent SyntheticCost accumulation, and wallet/ledger divergence under concurrent toolkit usage.


Changes

Implementation (coming next):

  • Restructure update_from_tx to acquire the ledger_state mutex at method entry and hold through write-back
  • Replace self.tx_context() call with inline TransactionContext construction from the held guard
  • Preserve tx_context() public method for backward compatibility
  • Add concurrency test demonstrating proper serialization under parallel invocation

📌 Submission Checklist

  • Changes are backward-compatible (or flagged if breaking)
  • Pull request description explains why the change is needed
  • Self-reviewed the diff
  • I have included a change file, or skipped for this reason: [reason]
  • If the changes introduce a new feature, I have bumped the node minor version
  • Update documentation (if relevant)
  • No new todos introduced

🔱 Fork Strategy

  • Node Runtime Update
  • Node Client Update
  • Other
  • N/A

🗹 TODO before merging

  • Ready for review

Race condition fix in update_from_tx (Least Authority Issue AJ)

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions
Copy link
Contributor

kics-logo

KICS version: v2.1.16

Category Results
CRITICAL CRITICAL 0
HIGH HIGH 0
MEDIUM MEDIUM 96
LOW LOW 12
INFO INFO 83
TRACE TRACE 0
TOTAL TOTAL 191
Metric Values
Files scanned placeholder 31
Files parsed placeholder 31
Files failed to scan placeholder 0
Total executed queries placeholder 73
Queries failed to execute placeholder 0
Execution time placeholder 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants