Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recover from hwmon linux read file panic #3108 #3112

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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