Skip to content

Commit 935df63

Browse files
authored
fix: skip sync.Once in snapshotStruct to prevent false-positive divergence (#87)
1 parent 7355594 commit 935df63

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

tracker/tracker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ func snapshotVal(v reflect.Value, buf *bytes.Buffer, visited map[uintptr]bool, d
128128
var (
129129
mutexType = reflect.TypeOf(sync.Mutex{})
130130
rwMutexType = reflect.TypeOf(sync.RWMutex{})
131+
onceType = reflect.TypeOf(sync.Once{})
131132
)
132133

133134
func snapshotStruct(v reflect.Value, buf *bytes.Buffer, visited map[uintptr]bool, depth int, trk *KeeperReflectTracker) {
134135
typ := v.Type()
135-
if typ == mutexType || typ == rwMutexType {
136+
if typ == mutexType || typ == rwMutexType || typ == onceType {
136137
return
137138
}
138139

tracker/tracker_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ func TestSnapshot_DeepNesting_NoPanic(t *testing.T) {
179179

180180
// ── Filtering: mutex and function fields ─────────────────────────────────────
181181

182+
func TestSnapshot_OnceNotTracked(t *testing.T) {
183+
type S struct {
184+
once sync.Once
185+
N int
186+
}
187+
s := &S{N: 5}
188+
tr := New(s)
189+
snap1 := tr.SnapshotOutOfKVStoreState()
190+
s.once.Do(func() {}) // transitions done: 0 → 1
191+
snap2 := tr.SnapshotOutOfKVStoreState()
192+
require.Equal(t, snap1, snap2, "sync.Once state must not affect snapshot")
193+
}
194+
182195
func TestSnapshot_MutexNotTracked(t *testing.T) {
183196
type S struct {
184197
mu sync.Mutex

0 commit comments

Comments
 (0)