To prevent hang if wmi.Query hangs, add a context-aware wrapper for it.

pull/452/head
leaf 8 years ago
parent 384a55110a
commit 65598d98cc

@ -3,6 +3,7 @@
package cpu package cpu
import ( import (
"context"
"fmt" "fmt"
"unsafe" "unsafe"
@ -81,8 +82,9 @@ func Info() ([]InfoStat, error) {
var ret []InfoStat var ret []InfoStat
var dst []Win32_Processor var dst []Win32_Processor
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
if err != nil { defer cancel()
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
return ret, err return ret, err
} }
@ -113,8 +115,11 @@ func Info() ([]InfoStat, error) {
// Name property is the key by which overall, per cpu and per core metric is known. // Name property is the key by which overall, per cpu and per core metric is known.
func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) { func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
var ret []Win32_PerfFormattedData_Counters_ProcessorInformation var ret []Win32_PerfFormattedData_Counters_ProcessorInformation
q := wmi.CreateQuery(&ret, "") q := wmi.CreateQuery(&ret, "")
err := wmi.Query(q, &ret) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, q, &ret)
return ret, err return ret, err
} }
@ -123,7 +128,9 @@ func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error)
func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) { func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) {
var ret []Win32_PerfFormattedData_PerfOS_System var ret []Win32_PerfFormattedData_PerfOS_System
q := wmi.CreateQuery(&ret, "") q := wmi.CreateQuery(&ret, "")
err := wmi.Query(q, &ret) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, q, &ret)
return ret, err return ret, err
} }

@ -4,9 +4,9 @@ package disk
import ( import (
"bytes" "bytes"
"context"
"unsafe" "unsafe"
"github.com/StackExchange/wmi"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
@ -132,7 +132,9 @@ func IOCounters(names ...string) (map[string]IOCountersStat, error) {
ret := make(map[string]IOCountersStat, 0) ret := make(map[string]IOCountersStat, 0)
var dst []Win32_PerfFormattedData var dst []Win32_PerfFormattedData
err := wmi.Query("SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk ", &dst) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, "SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk", &dst)
if err != nil { if err != nil {
return ret, err return ret, err
} }

@ -3,6 +3,7 @@
package host package host
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@ -109,7 +110,9 @@ func getMachineGuid() (string, error) {
func GetOSInfo() (Win32_OperatingSystem, error) { func GetOSInfo() (Win32_OperatingSystem, error) {
var dst []Win32_OperatingSystem var dst []Win32_OperatingSystem
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, q, &dst)
if err != nil { if err != nil {
return Win32_OperatingSystem{}, err return Win32_OperatingSystem{}, err
} }

@ -3,8 +3,10 @@
package common package common
import ( import (
"context"
"unsafe" "unsafe"
"github.com/StackExchange/wmi"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
@ -110,3 +112,18 @@ func CreateCounter(query windows.Handle, pname, cname string) (*CounterInfo, err
Counter: counter, Counter: counter,
}, nil }, nil
} }
// WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging
func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error {
errChan := make(chan error, 1)
go func() {
errChan <- wmi.Query(query, dst, connectServerArgs...)
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errChan:
return err
}
}

@ -3,6 +3,7 @@
package process package process
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"syscall" "syscall"
@ -130,8 +131,10 @@ func GetWin32Proc(pid int32) ([]Win32_Process, error) {
var dst []Win32_Process var dst []Win32_Process
query := fmt.Sprintf("WHERE ProcessId = %d", pid) query := fmt.Sprintf("WHERE ProcessId = %d", pid)
q := wmi.CreateQuery(&dst, query) q := wmi.CreateQuery(&dst, query)
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
if err := wmi.Query(q, &dst); err != nil { defer cancel()
err := common.WMIQueryWithContext(ctx, q, &dst)
if err != nil {
return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err) return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err)
} }
@ -333,7 +336,9 @@ func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
var dst []Win32_Process var dst []Win32_Process
query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid)) query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid))
err := wmi.Query(query, &dst) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, query, &dst)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save