Skip to content

Commit 1621eca

Browse files
Ibrahim Jarifbalaji
authored andcommitted
Acquire lock before unmapping vlog files (#1050)
* Acquire lock before unmapping vlog files When we truncate the vlog file, we unmap and remap it. This leaves a window for segfault. This PR reintroduces the lock that got removed in commit afa8faea.
1 parent ef1e8e4 commit 1621eca

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

value.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,16 @@ func (lf *logFile) doneWriting(offset uint32) error {
141141
return errors.Wrapf(err, "Unable to sync value log: %q", lf.path)
142142
}
143143

144+
// Before we were acquiring a lock here on lf.lock, because we were invalidating the file
145+
// descriptor due to reopening it as read-only. Now, we don't invalidate the fd, but unmap it,
146+
// truncate it and remap it. That creates a window where we have segfaults because the mmap is
147+
// no longer valid, while someone might be reading it. Therefore, we need a lock here again.
148+
lf.lock.Lock()
149+
defer lf.lock.Unlock()
150+
144151
// Unmap file before we truncate it. Windows cannot truncate a file that is mmapped.
145152
if err := lf.munmap(); err != nil {
146-
return errors.Wrapf(err, "failed to mumap vlog file %s", lf.fd.Name())
153+
return errors.Wrapf(err, "failed to munmap vlog file %s", lf.fd.Name())
147154
}
148155

149156
// TODO: Confirm if we need to run a file sync after truncation.

0 commit comments

Comments
 (0)