fix(sandbox): prevent state patch loss during block processing#15534
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a sandbox-only race where sandbox_patch_state patches could be dropped when normal block processing is deferred due to an optimistic block already executing.
Changes:
- Stops draining
pending_state_patchat the start ofstart_process_block_impl. - Removes
state_patchthreading throughpreprocess_block→apply_chunks_preprocessing. - Drains
pending_state_patchinsideapply_chunks_preprocessing, after theshould_be_pending_executiondeferral check.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #15534 +/- ##
==========================================
- Coverage 69.30% 69.30% -0.01%
==========================================
Files 940 940
Lines 212116 212130 +14
Branches 212116 212130 +14
==========================================
- Hits 147016 147014 -2
- Misses 59275 59279 +4
- Partials 5825 5837 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
stedfn
approved these changes
Apr 9, 2026
5af3290 to
a54bb33
Compare
r-near
approved these changes
Apr 16, 2026
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.
Background
sandbox_patch_statesometimes permanently loses patches — the patched account never appears, even on retry. This was reported in the Zulip thread and reproduced with near-sandbox-rs#51.Three interrelated bugs:
pending_state_patch.take()drains the queue at the start of block processing. If preprocessing fails (orphan, missing chunks, optimistic deferral), the patch is gone forever.patch_state_in_progress()checksis_empty(), which is true right aftertake()— before the patch is actually committed to state.apply()early-returns for old chunks and silently drops thestate_patch.What changed
Introduces
SandboxPatchTracker— a type that encapsulates the pending patch, a generation counter, and a committed-generation counter. LikeSandboxStatePatch, it uses the ZST pattern: real struct on sandbox builds, zero-sized no-op on non-sandbox builds. This replaces the three separate fields onChain(pending_state_patch,sandbox_patch_generation,sandbox_patch_committed_gen) with a singlesandbox_patches: SandboxPatchTracker.The key design change is taking the patch late — at point of use inside the per-shard loop of
apply_chunks_preprocessing, instead of at the top ofstart_process_block_impl. This eliminates the need for backup/clone/restore on error paths, since any error before the take leaves the patch in the tracker. The generation counter (in_progress()checksgeneration != committed_gen, notis_empty()) ensures the RPC doesn't return prematurely despite the late take.This removes the
state_patchparameter frompreprocess_blockandapply_chunks_preprocessing, and results in zero#[cfg(feature = "sandbox")]blocks inchain.rs.Related
clear()inpostprocess_ready_block)