Transparently handle EINTR during Read

This has been observed in particular on MacOS, in this case just retry
the call without throwing the error back to the user.

Related to:
https://github.com/arduino/arduino-cli/issues/504
https://github.com/arduino/arduino-pro-ide/issues/157
This commit is contained in:
Cristian Maglie
2019-12-03 20:26:03 +01:00
parent d1a463e324
commit 6a673bf917

View File

@@ -15,8 +15,8 @@ import (
"sync" "sync"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
"go.bug.st/serial/unixutils" "go.bug.st/serial/unixutils"
"golang.org/x/sys/unix"
) )
type unixPort struct { type unixPort struct {
@@ -51,7 +51,7 @@ func (port *unixPort) Close() error {
return nil return nil
} }
func (port *unixPort) Read(p []byte) (n int, err error) { func (port *unixPort) Read(p []byte) (int, error) {
port.closeLock.RLock() port.closeLock.RLock()
defer port.closeLock.RUnlock() defer port.closeLock.RUnlock()
if !port.opened { if !port.opened {
@@ -59,14 +59,23 @@ func (port *unixPort) Read(p []byte) (n int, err error) {
} }
fds := unixutils.NewFDSet(port.handle, port.closeSignal.ReadFD()) fds := unixutils.NewFDSet(port.handle, port.closeSignal.ReadFD())
for {
res, err := unixutils.Select(fds, nil, fds, -1) res, err := unixutils.Select(fds, nil, fds, -1)
if err == unix.EINTR {
continue
}
if err != nil { if err != nil {
return 0, err return 0, err
} }
if res.IsReadable(port.closeSignal.ReadFD()) { if res.IsReadable(port.closeSignal.ReadFD()) {
return 0, &PortError{code: PortClosed} return 0, &PortError{code: PortClosed}
} }
return unix.Read(port.handle, p) n, err := unix.Read(port.handle, p)
if err == unix.EINTR {
continue
}
return n, err
}
} }
func (port *unixPort) Write(p []byte) (n int, err error) { func (port *unixPort) Write(p []byte) (n int, err error) {