From d64b02f07db578554dc055c939d5fa1e556648c4 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle Date: Tue, 8 May 2018 17:00:31 -0400 Subject: [PATCH] Override 3 sec. WMI timeout in gopsutil The default timeout is too short for some overburdened or resource constrained machines to complete the WMI query before the context deadline expires. This causes them to be unable to fingerprint the CPU properly. --- helper/stats/cpu.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/helper/stats/cpu.go b/helper/stats/cpu.go index e6d3bf26c..5f2623047 100644 --- a/helper/stats/cpu.go +++ b/helper/stats/cpu.go @@ -1,14 +1,21 @@ package stats import ( + "context" "fmt" "math" "sync" + "time" multierror "github.com/hashicorp/go-multierror" "github.com/shirou/gopsutil/cpu" ) +const ( + // We use this constant to override the built in 3 second timeout in gopsutil + cpuInfoTimeout = 10 * time.Second +) + var ( cpuMhzPerCore float64 cpuModelName string @@ -28,7 +35,9 @@ func Init() error { } var cpuInfo []cpu.InfoStat - if cpuInfo, err = cpu.Info(); err != nil { + ctx, _ := context.WithTimeout(context.Background(), cpuInfoTimeout) + + if cpuInfo, err = cpu.InfoWithContext(ctx); err != nil { merrs = multierror.Append(merrs, fmt.Errorf("Unable to obtain CPU information: %v", initErr)) }