From 029c2f0575b62d5f969daa9b2050e3ec783c18cc Mon Sep 17 00:00:00 2001 From: Maurice van Veen Date: Mon, 7 Oct 2024 17:25:45 +0200 Subject: [PATCH] Fix data race in processAppendEntry Signed-off-by: Maurice van Veen --- server/raft.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/raft.go b/server/raft.go index 8c79dac729a..0ae8d1183f0 100644 --- a/server/raft.go +++ b/server/raft.go @@ -3547,13 +3547,17 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { } } + // Make a copy of these values, as the AppendEntry might be cached and returned to the pool in applyCommit. + aeCommit := ae.commit + aeReply := ae.reply + // Apply anything we need here. - if ae.commit > n.commit { + if aeCommit > n.commit { if n.paused { - n.hcommit = ae.commit - n.debug("Paused, not applying %d", ae.commit) + n.hcommit = aeCommit + n.debug("Paused, not applying %d", aeCommit) } else { - for index := n.commit + 1; index <= ae.commit; index++ { + for index := n.commit + 1; index <= aeCommit; index++ { if err := n.applyCommit(index); err != nil { break } @@ -3569,7 +3573,7 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { // Success. Send our response. if ar != nil { - n.sendRPC(ae.reply, _EMPTY_, ar.encode(arbuf)) + n.sendRPC(aeReply, _EMPTY_, ar.encode(arbuf)) arPool.Put(ar) } }