snapshotsync: make snapshot deletion idempotent#19930
Merged
Merged
Conversation
AskAlexSharov
approved these changes
Mar 18, 2026
mh0lt
pushed a commit
that referenced
this pull request
Mar 18, 2026
`RoSnapshots.delete` could call `dirtySegments.Delete(delSeg)` even when the target segment was no longer present. That makes repeated deletes or overlapping cleanup paths turn a harmless "already gone" case into a nil-pointer failure in snapshot pruning. This change makes deletion explicitly idempotent: once no matching segment is found, we return `nil` instead of touching an empty tree. It also stops the tree walk as soon as the target is found. The regression test covers both cases that matter here: deleting the same snapshot twice and deleting a snapshot that does not exist. That gives a direct proof that the new behavior is safe and matches the intended delete semantics.
yperbasis
pushed a commit
to Sahil-4555/erigon
that referenced
this pull request
Jun 1, 2026
…ntech#21545) ## Problem - `dirtySegment.close()` (closes seg and idx) can happen on subsegments once some collation does `OpenFolder`, which uses `TypedSegments` which closes the subsegments. - `closeWhatNotInList`-- merge calls this and can crash because of close earlier. - maybe user in erigontech#19930 observed this - This started happening more after I tried to take snapshot merge off the build semaphore - erigontech#21526 ``` panic: runtime error: invalid memory address or nil pointer dereference seg.(*Decompressor).FilePath snapshotsync.(*DirtySegment).closeAndRemoveFiles snapshots.go:420 snapshotsync.(*RoTx).Close snapshots.go:537 snapshotsync.(*View).Close ``` ## Fix In `closeWhatNotInList`, skip segments with `refcount > 0`: a live reader still references them, so closing now would invalidate that reader. They are reaped on a later pass once the reader releases them (`closeWhatNotInList` already runs on every `OpenFolder`). `View`/`BeginRo` stays lock-free (erigontech#20490) — the fix is purely in the close path. ## Test `TestCloseWhatNotInListVsLiveViewDoesNotCrash` reproduces the crash deterministically (pure `snapshotsync`, no merge machinery): it builds sub-segments, opens a `View` over them, drops a covering merged file on disk, reopens (so `NoOverlaps` removes the subs from the list), and asserts `View.Close` does not crash. It fails before this change and passes after. Co-authored-by: Sudeep Kumar <sudeep.kumar@erigon.tech>
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.
RoSnapshots.deletecould calldirtySegments.Delete(delSeg)even when the target segment was no longer present. That makes repeated deletes or overlapping cleanup paths turn a harmless "already gone" case into a nil-pointer failure in snapshot pruning.This change makes deletion explicitly idempotent: once no matching segment is found, we return
nilinstead of touching an empty tree. It also stops the tree walk as soon as the target is found.The regression test covers both cases that matter here: deleting the same snapshot twice and deleting a snapshot that does not exist. That gives a direct proof that the new behavior is safe and matches the intended delete semantics.