Skip to content

Commit

Permalink
recover from hwmon linux read file panic prometheus#3108
Browse files Browse the repository at this point in the history
Signed-off-by: joey <zchengjoey@gmail.com>
  • Loading branch information
chengjoey committed Sep 22, 2024
1 parent 6253b08 commit cb3c425
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package collector

import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -90,19 +91,28 @@ func addValueFile(data map[string]map[string]string, sensor string, prop string,
}

// sysReadFile is a simplified os.ReadFile that invokes syscall.Read directly.
func sysReadFile(file string) ([]byte, error) {
func sysReadFile(file string) (b []byte, err error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
defer func() {
if r := recover(); r != nil {
if rErr, ok := r.(error); ok {
err = rErr
} else {
err = fmt.Errorf("%v", r)
}
}
}()

// On some machines, hwmon drivers are broken and return EAGAIN. This causes
// Go's os.ReadFile implementation to poll forever.
//
// Since we either want to read data or bail immediately, do the simplest
// possible read using system call directly.
b := make([]byte, 128)
b = make([]byte, 128)
n, err := unix.Read(int(f.Fd()), b)
if err != nil {
return nil, err
Expand Down

0 comments on commit cb3c425

Please sign in to comment.