Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Deadlock when removing a peer that happened to be the leader. #5912

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ func (n *raft) applyCommit(index uint64) error {

// If this is us and we are the leader we should attempt to stepdown.
if peer == n.id && n.State() == Leader {
n.stepdown(n.selectNextLeader())
n.stepdownLocked(n.selectNextLeader())
}

// Remove from string intern map.
Expand Down
24 changes: 24 additions & 0 deletions server/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package server

import (
"errors"
"fmt"
"math"
"math/rand"
Expand Down Expand Up @@ -917,3 +918,26 @@ func TestNRGCandidateDontStepdownDueToLeaderOfPreviousTerm(t *testing.T) {
// Check that the candidate's term is still ahead of the leader's term
require_True(t, candidate.term > ae.term)
}

func TestNRGRemoveLeaderPeerDeadlockBug(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()

rg := c.createMemRaftGroup("TEST", 3, newStateAdder)
rg.waitOnLeader()

n := rg.leader().node().(*raft)
leader := n.ID()

// Propose to remove the leader as a peer. Will lead to a deadlock with bug.
require_NoError(t, n.ProposeRemovePeer(leader))
rg.waitOnLeader()

checkFor(t, 10*time.Second, 200*time.Millisecond, func() error {
nl := n.GroupLeader()
if nl != leader {
return nil
}
return errors.New("Leader has not moved")
})
}