Skip to content

Commit 357e652

Browse files
committed
Add a test for a rare shutdown hang
When doing a config reload that need to stop some providers while also sending SIGTERM to Prometheus at the same time can sometimes hang 1: sync.WaitGroup.Wait [83 minutes] [Created by run.(*Group).Run in goroutine 1 @ group.go:37] sync sema.go:110 runtime_SemacquireWaitGroup(*uint32(#166)) sync waitgroup.go:118 (*WaitGroup).Wait(*WaitGroup(#23)) discovery manager.go:276 (*Manager).ApplyConfig(#23, #167) main main.go:964 main.func5(#120) main main.go:1505 reloadConfig({#183, 0x1b}, 1, #40, #43, #50, {#31, 0xa, 0}) main main.go:1182 main.func22() run group.go:38 (*Group).Run.func1(*Group(#26), #51) Add a test for it. Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
1 parent d902abc commit 357e652

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

discovery/manager_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,3 +1562,53 @@ func TestUnregisterMetrics(t *testing.T) {
15621562
cancel()
15631563
}
15641564
}
1565+
1566+
// Calling ApplyConfig() that removes providers at the same time as shutting down
1567+
// the manager should not hang.
1568+
func TestConfigReloadAndShutdownRace(t *testing.T) {
1569+
reg := prometheus.NewRegistry()
1570+
_, sdMetrics := NewTestMetrics(t, reg)
1571+
1572+
mgrCtx, mgrCancel := context.WithCancel(context.Background())
1573+
discoveryManager := NewManager(mgrCtx, promslog.NewNopLogger(), reg, sdMetrics)
1574+
require.NotNil(t, discoveryManager)
1575+
discoveryManager.updatert = 100 * time.Millisecond
1576+
1577+
var wgDiscovery sync.WaitGroup
1578+
wgDiscovery.Add(1)
1579+
go func() {
1580+
discoveryManager.Run()
1581+
wgDiscovery.Done()
1582+
}()
1583+
time.Sleep(time.Millisecond * 200)
1584+
1585+
var wgBg sync.WaitGroup
1586+
updateChan := discoveryManager.SyncCh()
1587+
wgBg.Add(1)
1588+
ctx, cancel := context.WithCancel(context.Background())
1589+
go func() {
1590+
defer wgBg.Done()
1591+
select {
1592+
case <-ctx.Done():
1593+
return
1594+
case <-updateChan:
1595+
}
1596+
}()
1597+
1598+
c := map[string]Configs{
1599+
"prometheus": {staticConfig("bar:9090")},
1600+
}
1601+
discoveryManager.ApplyConfig(c)
1602+
1603+
delete(c, "prometheus")
1604+
wgBg.Add(1)
1605+
go func() {
1606+
discoveryManager.ApplyConfig(c)
1607+
wgBg.Done()
1608+
}()
1609+
mgrCancel()
1610+
wgDiscovery.Wait()
1611+
1612+
cancel()
1613+
wgBg.Wait()
1614+
}

0 commit comments

Comments
 (0)