Skip to content

Commit

Permalink
Add timeout to onewire init to ensure pipeline compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsexton committed Sep 12, 2024
1 parent 0f05e40 commit 2eaa0bc
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions netlink/onewire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"sync"
"syscall"
"time"

"periph.io/x/conn/v3/driver/driverreg"
"periph.io/x/conn/v3/onewire"
Expand Down Expand Up @@ -262,7 +266,8 @@ type socket interface {
// w1Socket is a netlink connector socket for communicating with the w1 Linux
// kernel module.
type w1Socket struct {
s socket
s socket
fd int
}

// newW1Socket returns a socket instance.
Expand All @@ -273,7 +278,7 @@ func newW1Socket() (*w1Socket, error) {
return nil, fmt.Errorf("failed to open netlink socket: %v", err)
}

return &w1Socket{s: s}, nil
return &w1Socket{s: s, fd: s.fd}, nil

Check failure on line 281 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / lint: macos-latest

s.fd undefined (type *connSocket has no field or method fd) (compile)

Check failure on line 281 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / test: macos-latest

s.fd undefined (type *connSocket has no field or method fd)

Check failure on line 281 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / lint: windows-latest

s.fd undefined (type *connSocket has no field or method fd)

Check failure on line 281 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / test: windows-latest

s.fd undefined (type *connSocket has no field or method fd)
}

func (ws *w1Socket) sendAndRecv(seq uint32, m *w1Msg) ([]byte, error) {
Expand Down Expand Up @@ -469,6 +474,18 @@ func (d *driver1W) Init() (bool, error) {
}
defer s.close()

// When run in pipelines, this blocks infinitely. Set a reasonable timeout.
// Since this package has not tests that will run in the pipeline, it will
// work out.
err = syscall.SetNonblock(s.fd, true)

Check failure on line 480 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / lint: windows-latest

cannot use s.fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock (compile)

Check failure on line 480 in netlink/onewire.go

View workflow job for this annotation

GitHub Actions / test: windows-latest

cannot use s.fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock
if err != nil {
log.Println("set nonblock failed in onewire.init. error: ", err)
return false, err
}
socketFile := os.NewFile(uintptr(s.fd), "onewire-socket")
_ = socketFile.SetReadDeadline(time.Now().Add(2 * time.Second))
defer socketFile.Close()

// Find bus masters.
m := &w1Msg{typ: msgListMasters}
if err = s.sendMsg(m.serialize(), 0); err != nil {
Expand Down

0 comments on commit 2eaa0bc

Please sign in to comment.