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

[bug] SetReadDeadLine did not work #549

Closed
weiyixuan opened this issue Oct 11, 2019 · 6 comments
Closed

[bug] SetReadDeadLine did not work #549

weiyixuan opened this issue Oct 11, 2019 · 6 comments
Labels

Comments

@weiyixuan
Copy link

go version: 1.12.8
platform: win10、macos、android

When I used wsc.SetReadDeadLine, I found that the ReadMessage exited before the timeout period reached, resulting in high-frequency execution of the loop and eventually crash.

`

for {

	wsc.SetReadDeadLine(time.Now().Add(500 * time.Millisecond))

	if _, buf, err = wsc.ReadMessage(); err != nil {

		log.Error(err)

		continue

	}

	...

}

`

@weiyixuan weiyixuan added the bug label Oct 11, 2019
@elithrar
Copy link
Contributor

elithrar commented Oct 11, 2019 via email

@weiyixuan
Copy link
Author

weiyixuan commented Oct 15, 2019

Windows 10 Professional 64 bit:10.0.17763.774

func main(){
	var err error

	header := http.Header{}
        // header setting

	dialer := &websocket.Dialer{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
		HandshakeTimeout: 6 * time.Second,
	}

	var wsc *websocket.Conn
	if wsc, _, err = dialer.Dial(`wss://x.x.x.x:443/...`, header); err != nil {
		log.Error(err)
		return
	}
	for {
		wsc.SetReadDeadline(time.Now().Add(time.Second))
		_, _, err = wsc.ReadMessage()
		fmt.Println(err)
	}
}

result:
……
read tcp 172.20.32.62:3733->x.x.x.x:443: i/o timeout
……
read tcp 172.20.32.62:3733->x.x.x.x:443: i/o timeout
read tcp 172.20.32.62:3733->x.x.x.x:443: i/o timeout
read tcp 172.20.32.62:3733->x.x.x.x:443: i/o timeout
read tcp 172.20.32.62:3733->x.x.x.x:443: i/o timeout
panic: repeated read on failed websocket connection

@nhooyr
Copy link

nhooyr commented Oct 15, 2019

You need to return from the loop if err is non nil.

@weiyixuan
Copy link
Author

You're right. But I'm curious as to why the timeout didn't work. The above log was printed in a flash.

@nhooyr
Copy link

nhooyr commented Oct 15, 2019

Well you set a deadline of 1s. Also, once a deadline is hit, the connection is done, see #474

@weiyixuan
Copy link
Author

Ok, got it. ୧(๑•̀◡•́๑)૭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants