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

raft: Fix possible deadlocks #1537

Merged
merged 3 commits into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
raft: Fix possible deadlock in processInternalRaftRequest
This function calls Propose, which will block if there's no leader.
Suppose we lose leadership just before calling Propose, and now there's
no leader. processInternalRaftRequest will block until a new leader is
elected, but this may interfere with the leader election. If Run
receives a Ready value that has new items to commit to the store, it
will try to do that, and get stuck there because
processInternalRaftRequest is called by a store function that has the
write lock held. Then the Run goroutine will get stuck, and not be able
to send outgoing messages.

To solve this, create a context with a cancel function for
processInternalRaftRequest, and make it so that cancelling the wait
calls this cancel function and unblocks Propose.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
  • Loading branch information
aaronlehmann committed Sep 14, 2016
commit 8b346005c946631ab9f712fa78e2b84257964b94
31 changes: 16 additions & 15 deletions manager/state/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,11 @@ func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRa

r.ID = n.reqIDGen.Next()

ch := n.wait.register(r.ID, cb)
// This must be derived from the context which is cancelled by stop()
// to avoid a deadlock on shutdown.
waitCtx, cancel := context.WithCancel(n.Ctx)

ch := n.wait.register(r.ID, cb, cancel)

// Do this check after calling register to avoid a race.
if atomic.LoadUint32(&n.signalledLeadership) != 1 {
Expand All @@ -1234,24 +1238,19 @@ func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRa
return nil, ErrRequestTooLarge
}

// This must use the context which is cancelled by stop() to avoid a
// deadlock on shutdown.
err = n.Propose(n.Ctx, data)
err = n.Propose(waitCtx, data)
if err != nil {
n.wait.cancel(r.ID)
return nil, err
}

select {
case x, ok := <-ch:
if ok {
res := x.(*applyResult)
return res.resp, res.err
}
return nil, ErrLostLeadership
case <-n.Ctx.Done():
case x := <-ch:
res := x.(*applyResult)
return res.resp, res.err
case <-waitCtx.Done():
n.wait.cancel(r.ID)
return nil, ErrStopped
return nil, ErrLostLeadership
case <-ctx.Done():
n.wait.cancel(r.ID)
return nil, ctx.Err()
Expand All @@ -1263,10 +1262,12 @@ func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRa
// until the change is performed or there is an error.
func (n *Node) configure(ctx context.Context, cc raftpb.ConfChange) error {
cc.ID = n.reqIDGen.Next()
ch := n.wait.register(cc.ID, nil)

ctx, cancel := context.WithCancel(ctx)
ch := n.wait.register(cc.ID, nil, cancel)

if err := n.ProposeConfChange(ctx, cc); err != nil {
n.wait.trigger(cc.ID, nil)
n.wait.cancel(cc.ID)
return err
}

Expand All @@ -1280,7 +1281,7 @@ func (n *Node) configure(ctx context.Context, cc raftpb.ConfChange) error {
}
return nil
case <-ctx.Done():
n.wait.trigger(cc.ID, nil)
n.wait.cancel(cc.ID)
return ctx.Err()
case <-n.Ctx.Done():
return ErrStopped
Expand Down
5 changes: 2 additions & 3 deletions manager/state/raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -684,8 +683,8 @@ func TestStress(t *testing.T) {
// update leader
leader = i
break
} else if strings.Contains(err.Error(), "context deadline exceeded") {
// though it's timing out, we still record this value
} else {
// though ProposeValue returned an error, we still record this value,
// for it may be proposed successfully and stored in Raft some time later
pIDs = append(pIDs, id)
}
Expand Down
15 changes: 9 additions & 6 deletions manager/state/raft/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type waitItem struct {
ch chan interface{}
// callback which is called synchronously when the wait is triggered
cb func()
// callback which is called to cancel a waiter
cancel func()
}

type wait struct {
Expand All @@ -21,13 +23,13 @@ func newWait() *wait {
return &wait{m: make(map[uint64]waitItem)}
}

func (w *wait) register(id uint64, cb func()) <-chan interface{} {
func (w *wait) register(id uint64, cb func(), cancel func()) <-chan interface{} {
w.l.Lock()
defer w.l.Unlock()
_, ok := w.m[id]
if !ok {
ch := make(chan interface{}, 1)
w.m[id] = waitItem{ch: ch, cb: cb}
w.m[id] = waitItem{ch: ch, cb: cb, cancel: cancel}
return ch
}
panic(fmt.Sprintf("duplicate id %x", id))
Expand All @@ -43,7 +45,6 @@ func (w *wait) trigger(id uint64, x interface{}) bool {
waitItem.cb()
}
waitItem.ch <- x
close(waitItem.ch)
return true
}
return false
Expand All @@ -54,8 +55,8 @@ func (w *wait) cancel(id uint64) {
waitItem, ok := w.m[id]
delete(w.m, id)
w.l.Unlock()
if ok {
close(waitItem.ch)
if ok && waitItem.cancel != nil {
waitItem.cancel()
}
}

Expand All @@ -65,6 +66,8 @@ func (w *wait) cancelAll() {

for id, waitItem := range w.m {
delete(w.m, id)
close(waitItem.ch)
if waitItem.cancel != nil {
waitItem.cancel()
}
}
}