Skip to content

Commit

Permalink
recover from hwmon linux read file panic #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 6, 2024
1 parent b9d0932 commit c170c0d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 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"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -91,19 +92,29 @@ 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 {
level.Error(log.NewNopLogger()).Log("msg", "recovered from panic", "err", r)
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 c170c0d

Please sign in to comment.