Commit a884600
authored
fix(sandbox): prevent state patch loss during block processing (#15534)
## Background
`sandbox_patch_state` sometimes permanently loses patches — the patched
account never appears, even on retry. This was reported in the [Zulip
thread](https://near.zulipchat.com/#narrow/channel/308695-nearone.2Fprivate/topic/Sandbox.20patch_state.20issues)
and reproduced with
[near-sandbox-rs#51](near/near-sandbox-rs#51).
Three interrelated bugs:
- **Patch lost on block failure**: `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.
- **RPC returns too early**: `patch_state_in_progress()` checks
`is_empty()`, which is true right after `take()` — before the patch is
actually committed to state.
- **Startup race**: Block 1 after genesis has no new chunks. The
runtime's `apply()` early-returns for old chunks and silently drops the
`state_patch`.
## What changed
Introduces `SandboxPatchTracker` — a type that encapsulates the pending
patch, a generation counter, and a committed-generation counter. Like
`SandboxStatePatch`, it uses the ZST pattern: real struct on sandbox
builds, zero-sized no-op on non-sandbox builds. This replaces the three
separate fields on `Chain` (`pending_state_patch`,
`sandbox_patch_generation`, `sandbox_patch_committed_gen`) with a single
`sandbox_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 of `start_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()`
checks `generation != committed_gen`, not `is_empty()`) ensures the RPC
doesn't return prematurely despite the late take.
This removes the `state_patch` parameter from `preprocess_block` and
`apply_chunks_preprocessing`, and results in zero `#[cfg(feature =
"sandbox")]` blocks in `chain.rs`.
## Related
- #15536 — alternative fix by @r-near that addresses the same three bugs
with a backup/restore approach and 22 cfg-gates. This PR takes a
different approach (take-late + tracker) to achieve the same result with
less code deviation.
- #14893 — fixed a different patch loss path (`clear()` in
`postprocess_ready_block`)1 parent f0bf399 commit a884600
3 files changed
Lines changed: 114 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
318 | 308 | | |
319 | 309 | | |
320 | 310 | | |
| |||
458 | 448 | | |
459 | 449 | | |
460 | 450 | | |
461 | | - | |
| 451 | + | |
462 | 452 | | |
463 | 453 | | |
464 | 454 | | |
| |||
640 | 630 | | |
641 | 631 | | |
642 | 632 | | |
643 | | - | |
| 633 | + | |
644 | 634 | | |
645 | 635 | | |
646 | 636 | | |
| |||
1650 | 1640 | | |
1651 | 1641 | | |
1652 | 1642 | | |
1653 | | - | |
1654 | 1643 | | |
1655 | 1644 | | |
1656 | 1645 | | |
1657 | 1646 | | |
1658 | 1647 | | |
1659 | 1648 | | |
1660 | | - | |
1661 | 1649 | | |
1662 | 1650 | | |
1663 | 1651 | | |
| |||
1835 | 1823 | | |
1836 | 1824 | | |
1837 | 1825 | | |
| 1826 | + | |
1838 | 1827 | | |
1839 | 1828 | | |
1840 | 1829 | | |
| |||
1847 | 1836 | | |
1848 | 1837 | | |
1849 | 1838 | | |
| 1839 | + | |
1850 | 1840 | | |
1851 | 1841 | | |
1852 | 1842 | | |
| |||
2348 | 2338 | | |
2349 | 2339 | | |
2350 | 2340 | | |
2351 | | - | |
2352 | 2341 | | |
2353 | 2342 | | |
2354 | 2343 | | |
| |||
2535 | 2524 | | |
2536 | 2525 | | |
2537 | 2526 | | |
2538 | | - | |
2539 | 2527 | | |
2540 | 2528 | | |
2541 | 2529 | | |
| |||
2550 | 2538 | | |
2551 | 2539 | | |
2552 | 2540 | | |
| 2541 | + | |
2553 | 2542 | | |
2554 | 2543 | | |
2555 | 2544 | | |
| |||
2775 | 2764 | | |
2776 | 2765 | | |
2777 | 2766 | | |
2778 | | - | |
2779 | 2767 | | |
2780 | 2768 | | |
2781 | 2769 | | |
| |||
3200 | 3188 | | |
3201 | 3189 | | |
3202 | 3190 | | |
3203 | | - | |
3204 | 3191 | | |
3205 | 3192 | | |
3206 | 3193 | | |
| |||
3248 | 3235 | | |
3249 | 3236 | | |
3250 | 3237 | | |
| 3238 | + | |
3251 | 3239 | | |
3252 | 3240 | | |
3253 | 3241 | | |
3254 | | - | |
3255 | | - | |
3256 | | - | |
| 3242 | + | |
| 3243 | + | |
| 3244 | + | |
| 3245 | + | |
| 3246 | + | |
| 3247 | + | |
| 3248 | + | |
| 3249 | + | |
| 3250 | + | |
| 3251 | + | |
| 3252 | + | |
3257 | 3253 | | |
3258 | 3254 | | |
3259 | 3255 | | |
| |||
4030 | 4026 | | |
4031 | 4027 | | |
4032 | 4028 | | |
4033 | | - | |
4034 | | - | |
4035 | 4029 | | |
4036 | | - | |
| 4030 | + | |
4037 | 4031 | | |
4038 | 4032 | | |
4039 | 4033 | | |
4040 | | - | |
| 4034 | + | |
4041 | 4035 | | |
4042 | 4036 | | |
4043 | 4037 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 8 | + | |
12 | 9 | | |
13 | 10 | | |
14 | 11 | | |
| |||
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
43 | 103 | | |
44 | 104 | | |
45 | 105 | | |
| |||
59 | 119 | | |
60 | 120 | | |
61 | 121 | | |
62 | | - | |
| 122 | + | |
63 | 123 | | |
64 | 124 | | |
65 | 125 | | |
| |||
71 | 131 | | |
72 | 132 | | |
73 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
74 | 156 | | |
0 commit comments