Skip to content

Commit

Permalink
Refactor select loop timeout to be more explicit, bsd-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMaeder authored and sergiu128 committed Dec 19, 2024
1 parent 31a88e3 commit e0c8035
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/socket_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ func connect(fd int, remoteAddr net.Addr, timeout time.Duration, opts ...sonicop
var fds unix.FdSet
fds.Set(fd)

t := unix.NsecToTimeval(5 * timeout.Nanoseconds())
startTime = time.Now()

for {
// Linux will update t with the time not slept, so we don't need an separate check like before
// https://man7.org/linux/man-pages/man2/select.2.html
// Prevent an infinite select() loop, time out eventually
remainingTime := 5 * timeout - time.Since(startTime)
if remainingTime < 0 {
return sonicerrors.ErrTimeout
}

t := unix.NsecToTimeval(remainingTime.Nanoseconds())

n, err := unix.Select(fd+1, nil, &fds, nil, &t)
if err == nil {
// Handle timeout
Expand Down

0 comments on commit e0c8035

Please sign in to comment.