Skip to content

Commit e47fdbe

Browse files
authored
Timeout for analytics (#301)
* perf(analytics): timeout GPU info collection at 100ms nvidia-smi and system_profiler can be slow. Wrap getGPUInfo in a goroutine with a 100ms deadline so it never blocks CLI responsiveness. * perf(analytics): timeout parent process info collection at 100ms The ps commands on macOS can be slow under load, so wrap getParentProcessInfo in the same 100ms timeout used for GPU info.
1 parent 687ee4a commit e47fdbe

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

pkg/analytics/parentprocess.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,29 @@ import (
66
"runtime"
77
"strconv"
88
"strings"
9+
"time"
910
)
1011

1112
// getParentProcessInfo returns the name and full command line of the parent process.
13+
// Times out after 100ms to avoid blocking the CLI.
1214
func getParentProcessInfo() (name, cmdline string) {
15+
type result struct {
16+
name, cmdline string
17+
}
18+
ch := make(chan result, 1)
19+
go func() {
20+
n, c := getParentProcessInfoSync()
21+
ch <- result{name: n, cmdline: c}
22+
}()
23+
select {
24+
case r := <-ch:
25+
return r.name, r.cmdline
26+
case <-time.After(100 * time.Millisecond):
27+
return "", ""
28+
}
29+
}
30+
31+
func getParentProcessInfoSync() (name, cmdline string) {
1332
ppid := os.Getppid()
1433
if ppid <= 0 {
1534
return "", ""

pkg/analytics/posthog.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,24 @@ func getTimezone() string {
319319
}
320320

321321
func getGPUInfo() string {
322+
type result struct {
323+
out string
324+
}
325+
ch := make(chan result, 1)
326+
go func() {
327+
ch <- result{out: getGPUInfoSync()}
328+
}()
329+
select {
330+
case r := <-ch:
331+
return r.out
332+
case <-time.After(100 * time.Millisecond):
333+
return ""
334+
}
335+
}
336+
337+
func getGPUInfoSync() string {
322338
out, err := exec.Command("nvidia-smi", "--query-gpu=name,memory.total,driver_version,count", "--format=csv,noheader,nounits").Output() // #nosec G204
323339
if err != nil {
324-
// nvidia-smi not available or no NVIDIA GPU
325340
if runtime.GOOS == "darwin" {
326341
return getAppleGPUInfo()
327342
}
@@ -335,7 +350,6 @@ func getAppleGPUInfo() string {
335350
if err != nil {
336351
return ""
337352
}
338-
// Extract just the chipset/model lines
339353
lines := strings.Split(string(out), "\n")
340354
var gpuLines []string
341355
for _, line := range lines {

0 commit comments

Comments
 (0)