Skip to content

x/sys/unix: strange behaviour on unix.Select #61225

@cyberxnomad

Description

@cyberxnomad

What version of Go are you using (go version)?

$ go version
go1.20.5

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

linux/arm

What did you do?

I'm using "Select" to get serial port data, but I'm experiencing some strange behavior. Instead of waiting for the specified timeout duration, it immediately times out.

code:

var (
	fd      int
	nfds    int
	readSet unix.FdSet
	tv      unix.Timeval
	buf     []byte // read buffer
	frame   []byte
	nbytes  int // how many bytes read
)

fd = g.ser.Fd() // get serial fd
frame = make([]byte, 0)

readSet.Zero()
readSet.Set(fd)
tv.Sec = 0
tv.Usec = 100 * 1000 // 100ms

mlog.Debug.Println("starting select")
if nfds, err = unix.Select(fd+1, &readSet, nil, nil, &tv); err != nil {
	mlog.Error.Println(err)
	return
}
if nfds > 0 {
	if readSet.IsSet(fd) {
		buf = make([]byte, 1024)
		if nbytes, err = g.ser.Read(buf); err != nil { // read data of serial
			mlog.Error.Println(err)
			return
		}
		if nbytes > 0 {
			frame = append(frame, buf[:nbytes]...) // package as a frame
		}
	}
} else if nfds == 0 {
	mlog.Debug.Println("timeout")
	err = eAckTimeOut
	return
} else {
	err = eUnknow
	return
}

Loop calling the above code

What did you expect to see?

[DEBUG] 2023/07/07 17:17:03.662758 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.775798 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.885805 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.995762 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.105745 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.215735 gbus.go:211: starting select
...

What did you see instead?

[DEBUG] 2023/07/07 17:17:03.662758 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.775798 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.885805 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:03.995762 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.105745 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.215735 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.325825 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.435783 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.545723 gbus.go:211: starting select
[DEBUG] 2023/07/07 17:17:04.548090 gbus.go:232: timeout

Please take a look at the time in the last two lines. The difference in time between the two is only about 3 milliseconds. But the timeout time set in the code is 100 milliseconds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions