Skip to content

snapshotsync: make snapshot deletion idempotent#19930

Merged
AskAlexSharov merged 1 commit into
erigontech:mainfrom
apetro2:idempotence
Mar 18, 2026
Merged

snapshotsync: make snapshot deletion idempotent#19930
AskAlexSharov merged 1 commit into
erigontech:mainfrom
apetro2:idempotence

Conversation

@apetro2

@apetro2 apetro2 commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

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.

@AskAlexSharov AskAlexSharov merged commit 3fd3198 into erigontech:main Mar 18, 2026
34 of 35 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants