Skip to content

Commit

Permalink
silences: avoid deadlock
Browse files Browse the repository at this point in the history
Calling gossip.GossipBroadcast() will cause a deadlock if
there is a currently executing OnBroadcast* function.

See prometheus#982
  • Loading branch information
Corentin Chary committed Sep 15, 2017
1 parent be819dd commit 243d6bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion silence/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ func (s *Silences) setSilence(sil *pb.Silence) error {
}

s.st.Merge(st)
s.gossip.GossipBroadcast(st)
// setSilence() is called with s.mtx locked, which can produce
// a deadlock if we call GossipBroadcast from here.
go s.gossip.GossipBroadcast(st)

return nil
}
Expand Down
13 changes: 9 additions & 4 deletions silence/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,24 @@ func TestSilencesSetSilence(t *testing.T) {
},
}

var called bool
done := make(chan bool)
s.gossip = &mockGossip{
broadcast: func(d mesh.GossipData) {
data, ok := d.(*gossipData)
require.True(t, ok, "gossip data of unknown type")
require.Equal(t, want, data, "unexpected gossip broadcast data")

called = true
close(done)
},
}
require.NoError(t, s.setSilence(sil))
require.True(t, called, "GossipBroadcast was not called")
require.Equal(t, want.data, s.st.data, "Unexpected silence state")

// GossipBroadcast is called in a gorountine.
select {
case <-done:
case <-time.After(1 * time.Second):
t.Fatal("GossipBroadcast was not called")
}
}

func TestSilenceSet(t *testing.T) {
Expand Down

0 comments on commit 243d6bc

Please sign in to comment.