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

Fix for deadlock issue #3682 #3715

Merged
merged 1 commit into from
Mar 1, 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
1 change: 0 additions & 1 deletion provider/mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func max(a, b int) int {
func (a *Alerts) Subscribe() provider.AlertIterator {
a.mtx.Lock()
defer a.mtx.Unlock()

var (
done = make(chan struct{})
alerts = a.alerts.List()
Expand Down
57 changes: 57 additions & 0 deletions provider/mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,63 @@ func TestAlertsSubscribePutStarvation(t *testing.T) {
}
}

func TestDeadLock(t *testing.T) {
t0 := time.Now()
t1 := t0.Add(5 * time.Second)

marker := types.NewMarker(prometheus.NewRegistry())
// Run gc every 5 milliseconds to increase the possibility of a deadlock with Subscribe()
alerts, err := NewAlerts(context.Background(), marker, 5*time.Millisecond, noopCallback{}, log.NewNopLogger(), nil)
if err != nil {
t.Fatal(err)
}
alertsToInsert := []*types.Alert{}
for i := 0; i < 200+1; i++ {
alertsToInsert = append(alertsToInsert, &types.Alert{
Alert: model.Alert{
// Make sure the fingerprints differ
Labels: model.LabelSet{"iteration": model.LabelValue(strconv.Itoa(i))},
Annotations: model.LabelSet{"foo": "bar"},
StartsAt: t0,
EndsAt: t1,
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: t0,
Timeout: false,
})
}

if err := alerts.Put(alertsToInsert...); err != nil {
t.Fatal("Unable to add alerts")
}
done := make(chan bool)

// call subscribe repeatedly in a goroutine to increase
// the possibility of a deadlock occurring
go func() {
tick := time.NewTicker(10 * time.Millisecond)
defer tick.Stop()
stopAfter := time.After(1 * time.Second)
for {
select {
case <-tick.C:
alerts.Subscribe()
case <-stopAfter:
done <- true
break
}
}
}()

select {
case <-done:
// no deadlock
alerts.Close()
case <-time.After(10 * time.Second):
t.Error("Deadlock detected")
}
}

func TestAlertsPut(t *testing.T) {
marker := types.NewMarker(prometheus.NewRegistry())
alerts, err := NewAlerts(context.Background(), marker, 30*time.Minute, noopCallback{}, log.NewNopLogger(), nil)
Expand Down
3 changes: 1 addition & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ func (a *Alerts) Run(ctx context.Context, interval time.Duration) {

func (a *Alerts) gc() {
a.Lock()
defer a.Unlock()

var resolved []*types.Alert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to stop passing []*types.Alert and instead now send a slice of values to the callback.

Suggested change
var resolved []*types.Alert
var resolved []types.Alert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can submit a separate PR for this

for fp, alert := range a.c {
if alert.Resolved() {
delete(a.c, fp)
resolved = append(resolved, alert)
}
}
a.Unlock()
a.cb(resolved)
}

Expand Down
Loading