Skip to content

Commit f3ad9ab

Browse files
author
Ibrahim Jarif
committed
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. (cherry picked from commit 1621eca)
1 parent 6ba68c9 commit f3ad9ab

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
@@ -135,9 +135,16 @@ func (lf *logFile) doneWriting(offset uint32) error {
135135
return errors.Wrapf(err, "Unable to sync value log: %q", lf.path)
136136
}
137137

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

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

0 commit comments

Comments
 (0)