Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why read timeout return a nil error? #148

Open
lu-xiaoliang opened this issue Nov 30, 2022 · 4 comments
Open

Why read timeout return a nil error? #148

lu-xiaoliang opened this issue Nov 30, 2022 · 4 comments
Assignees
Labels
2.0 Breaking changes scheduled for v2.0 release bug

Comments

@lu-xiaoliang
Copy link

lu-xiaoliang commented Nov 30, 2022

when i write a command, then i use io.ReadFull to read an response. But if the device do not response, it will STW.
whether to return a read timeout error?

@cmaglie cmaglie added bug 2.0 Breaking changes scheduled for v2.0 release labels Jan 2, 2023
@cmaglie
Copy link
Member

cmaglie commented Jan 2, 2023

Unfortunately, this is a breaking change. I'm collecting all the breaking changes to be implemented in a future 2.0 release, BTW it's a low priority, for now, no estimates for when it will be done.

@cmaglie cmaglie self-assigned this Jan 2, 2023
@bmuessig
Copy link

bmuessig commented May 29, 2024

This is a simple workaround:

var errTimeout = errors.New("timeout")

type timeoutReader struct {
	r io.Reader
}

func NewTimeoutReader(r io.Reader) timeoutReader {
	return timeoutReader{r}
}

func (t timeoutReader) Read(p []byte) (n int, err error) {
	n, err = t.r.Read(p)
	if n == 0 && err == nil {
		err = errTimeout
	}
	return
}

You only have to wrap your port in a NewTimeoutReader(port) and io.ReadFull and Scanner work as expected.

Updated with the suggestion from @ccollins476ad

@ccollins476ad
Copy link

This is a simple workaround:
[...]

	if n == 0 {
		err = errTimeout
	}

Good suggestion. Just be sure not to lose the original error if there is one!

 	if n == 0 && err == nil {
 		err = errTimeout
 	}

@bmuessig
Copy link

bmuessig commented Jul 7, 2024

Good suggestion. Just be sure not to lose the original error if there is one!

Good catch, oops. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 Breaking changes scheduled for v2.0 release bug
Projects
None yet
Development

No branches or pull requests

4 participants