Skip to content

Commit

Permalink
Fix for a filestore data race on hash during snapshots (#4470)
Browse files Browse the repository at this point in the history
 Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Sep 1, 2023
2 parents cb8b94a + 4df5f51 commit 0fadaf2
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions server/filestore.go
Expand Up @@ -7023,8 +7023,9 @@ func (fs *fileStore) streamSnapshot(w io.WriteCloser, state *StreamState, includ
writeErr(fmt.Sprintf("Could not gather stream meta file: %v", err))
return
}
fs.hh.Reset()
fs.hh.Write(meta)
hh := fs.hh
hh.Reset()
hh.Write(meta)
sum := []byte(hex.EncodeToString(fs.hh.Sum(nil)))
fs.mu.Unlock()

Expand All @@ -7048,8 +7049,8 @@ func (fs *fileStore) streamSnapshot(w io.WriteCloser, state *StreamState, includ
buf, err = fs.aek.Open(nil, buf[:ns], buf[ns:len(buf)-highwayhash.Size64], nil)
if err == nil {
// Redo hash checksum at end on plaintext.
fs.hh.Reset()
fs.hh.Write(buf)
hh.Reset()
hh.Write(buf)
buf = fs.hh.Sum(buf)
}
}
Expand Down Expand Up @@ -7102,9 +7103,9 @@ func (fs *fileStore) streamSnapshot(w io.WriteCloser, state *StreamState, includ
}

// Do consumers' state last.
fs.mu.Lock()
fs.mu.RLock()
cfs := fs.cfs
fs.mu.Unlock()
fs.mu.RUnlock()

for _, cs := range cfs {
o, ok := cs.(*consumerFileStore)
Expand Down

0 comments on commit 0fadaf2

Please sign in to comment.