Skip to content

Commit

Permalink
Merge pull request #1096 from nats-io/fix_panic_on_expire_msgs
Browse files Browse the repository at this point in the history
[FIXED] FileStore: possible panic when unable to open an index file
  • Loading branch information
kozlovic committed Oct 6, 2020
2 parents 8f0eb07 + 65f3e19 commit f11f6f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions stores/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2213,8 +2213,12 @@ func (ms *FileMsgStore) doLockFiles(fslice *fileSlice, onlyIndexFile bool) error
}
idxWasOpened, err = ms.fm.lockFile(fslice.idxFile)
if err != nil {
if !datWasOpened {
ms.fm.unlockFile(fslice.file)
if !onlyIndexFile {
if !datWasOpened {
ms.fm.closeLockedFile(fslice.file)
} else {
ms.fm.unlockFile(fslice.file)
}
}
return err
}
Expand Down
28 changes: 28 additions & 0 deletions stores/filestore_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,34 @@ func TestFSPanicOnStoreCloseWhileMsgsExpire(t *testing.T) {
fs.Close()
}

func TestFSPanicOnMsgExpireWithClosedDatFile(t *testing.T) {
cleanupFSDatastore(t)
defer cleanupFSDatastore(t)

limits := testDefaultStoreLimits
limits.MaxAge = 500 * time.Millisecond

fs, _ := newFileStore(t, testFSDefaultDatastore, &limits, SliceConfig(1, 0, 0, ""))
defer fs.Close()

cs := storeCreateChannel(t, fs, "foo")
for i := 0; i < 3; i++ {
storeMsg(t, cs, "foo", uint64(i+1), []byte("msg"))
}

ms := cs.Msgs.(*FileMsgStore)
ms.Lock()
idxFileName := ms.files[1].idxFile.name
os.Remove(idxFileName)
err := ioutil.WriteFile(idxFileName, []byte("xxxxxxx"), 0666)
ms.Unlock()
if err != nil {
t.Fatalf("Error rewriting index file: %v", err)
}

time.Sleep(750 * time.Millisecond)
}

func TestFSMsgIndexFileWithExtraZeros(t *testing.T) {
cleanupFSDatastore(t)
defer cleanupFSDatastore(t)
Expand Down

0 comments on commit f11f6f9

Please sign in to comment.