From 6e6d81f094d5d99cd59bee1e57e7ab81d8ce5b05 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Fri, 22 Aug 2014 15:08:13 -0700 Subject: [PATCH] raft: copy ents to avoid races --- raft2/log.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/raft2/log.go b/raft2/log.go index 54ed1f907..1fc331248 100644 --- a/raft2/log.go +++ b/raft2/log.go @@ -85,7 +85,13 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 { } func (l *raftLog) unstableEnts() []Entry { - return l.entries(l.unstable) + ents := l.entries(l.unstable) + if ents == nil { + return nil + } + cpy := make([]Entry, len(ents)) + copy(cpy, ents) + return cpy } func (l *raftLog) resetUnstable() { @@ -96,7 +102,13 @@ func (l *raftLog) resetUnstable() { // all the returned entries will be marked as applied. func (l *raftLog) nextEnts() (ents []Entry) { if l.committed > l.applied { - return l.slice(l.applied+1, l.committed+1) + ents := l.slice(l.applied+1, l.committed+1) + if ents == nil { + return nil + } + cpy := make([]Entry, len(ents)) + copy(cpy, ents) + return cpy } return nil }