mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-27 23:09:08 +01:00
Propagate context to OS calls that can leverage its cancellation / timeout
This commit is contained in:
@@ -155,7 +155,7 @@ func GetInfoWithChecks(ctx context.Context, checks []*proto.Checks) (*Info, erro
|
|||||||
processCheckPaths = append(processCheckPaths, check.GetFiles()...)
|
processCheckPaths = append(processCheckPaths, check.GetFiles()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := checkFileAndProcess(processCheckPaths)
|
files, err := checkFileAndProcess(ctx, processCheckPaths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func GetInfo(ctx context.Context) *Info {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
||||||
func checkFileAndProcess(paths []string) ([]File, error) {
|
func checkFileAndProcess(_ context.Context, _ []string) ([]File, error) {
|
||||||
return []File{}, nil
|
return []File{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func GetInfo(ctx context.Context) *Info {
|
|||||||
sysName := string(bytes.Split(utsname.Sysname[:], []byte{0})[0])
|
sysName := string(bytes.Split(utsname.Sysname[:], []byte{0})[0])
|
||||||
machine := string(bytes.Split(utsname.Machine[:], []byte{0})[0])
|
machine := string(bytes.Split(utsname.Machine[:], []byte{0})[0])
|
||||||
release := string(bytes.Split(utsname.Release[:], []byte{0})[0])
|
release := string(bytes.Split(utsname.Release[:], []byte{0})[0])
|
||||||
swVersion, err := exec.Command("sw_vers", "-productVersion").Output()
|
swVersion, err := exec.CommandContext(ctx, "sw_vers", "-productVersion").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("got an error while retrieving macOS version with sw_vers, error: %s. Using darwin version instead.\n", err)
|
log.Warnf("got an error while retrieving macOS version with sw_vers, error: %s. Using darwin version instead.\n", err)
|
||||||
swVersion = []byte(release)
|
swVersion = []byte(release)
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ func isDuplicated(addresses []NetworkAddress, addr NetworkAddress) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
||||||
func checkFileAndProcess(paths []string) ([]File, error) {
|
func checkFileAndProcess(_ context.Context, _ []string) ([]File, error) {
|
||||||
return []File{}, nil
|
return []File{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ func collectLocationInfo(info *Info) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFileAndProcess(_ []string) ([]File, error) {
|
func checkFileAndProcess(_ context.Context, _ []string) ([]File, error) {
|
||||||
return []File{}, nil
|
return []File{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,24 +3,30 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/process"
|
"github.com/shirou/gopsutil/v3/process"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getRunningProcesses returns a list of running process paths.
|
// getRunningProcesses returns a list of running process paths. The context bounds the work:
|
||||||
func getRunningProcesses() ([]string, error) {
|
// the per-PID loop bails as soon as ctx is done, and the gopsutil calls honor it where they
|
||||||
processIDs, err := process.Pids()
|
// can, so a stuck enumeration cannot run unbounded.
|
||||||
|
func getRunningProcesses(ctx context.Context) ([]string, error) {
|
||||||
|
processIDs, err := process.PidsWithContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
processMap := make(map[string]bool)
|
processMap := make(map[string]bool)
|
||||||
for _, pID := range processIDs {
|
for _, pID := range processIDs {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
p := &process.Process{Pid: pID}
|
p := &process.Process{Pid: pID}
|
||||||
|
|
||||||
path, _ := p.Exe()
|
path, _ := p.ExeWithContext(ctx)
|
||||||
if path != "" {
|
if path != "" {
|
||||||
processMap[path] = false
|
processMap[path] = false
|
||||||
}
|
}
|
||||||
@@ -35,18 +41,21 @@ func getRunningProcesses() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
||||||
func checkFileAndProcess(paths []string) ([]File, error) {
|
func checkFileAndProcess(ctx context.Context, paths []string) ([]File, error) {
|
||||||
files := make([]File, len(paths))
|
files := make([]File, len(paths))
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
runningProcesses, err := getRunningProcesses()
|
runningProcesses, err := getRunningProcesses(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, path := range paths {
|
for i, path := range paths {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
file := File{Path: path}
|
file := File{Path: path}
|
||||||
|
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/process"
|
"github.com/shirou/gopsutil/v3/process"
|
||||||
@@ -9,7 +10,7 @@ import (
|
|||||||
func Benchmark_getRunningProcesses(b *testing.B) {
|
func Benchmark_getRunningProcesses(b *testing.B) {
|
||||||
b.Run("getRunningProcesses new", func(b *testing.B) {
|
b.Run("getRunningProcesses new", func(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
ps, err := getRunningProcesses()
|
ps, err := getRunningProcesses(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("unexpected error: %v", err)
|
b.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -29,7 +30,7 @@ func Benchmark_getRunningProcesses(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
s, _ := getRunningProcesses()
|
s, _ := getRunningProcesses(context.Background())
|
||||||
b.Logf("getRunningProcesses returned %d processes", len(s))
|
b.Logf("getRunningProcesses returned %d processes", len(s))
|
||||||
s, _ = getRunningProcessesOld()
|
s, _ = getRunningProcessesOld()
|
||||||
b.Logf("getRunningProcessesOld returned %d processes", len(s))
|
b.Logf("getRunningProcessesOld returned %d processes", len(s))
|
||||||
|
|||||||
Reference in New Issue
Block a user