bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
657 stars 199 forks source link

Can't set small timeouts on linux #134

Closed zwade closed 2 years ago

zwade commented 2 years ago

When computing the timeouts for the select syscall on unix/linux, the code first adds time.Now() to the user-provided timeout, then at a later point subtracts time.Now(). However, since these are two different timestamps, it means that the value that gets passed to the syscall can be negative if the user supplies a sufficiently small timeout. (c.f. https://github.com/bugst/go-serial/blob/master/serial_unix.go#L75)

In theory this should raise EINVAL which would at least get propagated up the stack, but in my testing it seems that the function never returns. I haven't tracked down why, but my suspicion is that something is casting the int to a uint.

Ideally the timeout value would get passed directly to the syscall unchanged, but I think it's probably more important to make sure that the function always returns.

Thanks!

(Details: go: 17, go-serial: 1.3.4, os: Ubuntu 20.04.3)

cmaglie commented 2 years ago

Thanks for the report!

You're correct, a very short timeout may result in blocking operation. Even if theoretical (we are talking about timeout of less than microseconds) I'll prepare a patch for this one.

zwade commented 2 years ago

Thanks for taking a look at this! The PR looks good to me, hopefully the maintainers can take a look soon.