You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Everywhere we got an error when read from / write into underlying transport (incl. flush write buffer), we should check if we have closed the connection, and wrap a CloseError.
How to reproduce
package main
import (
"context""log""net/http""net/http/httptest""strings""time""nhooyr.io/websocket"
)
funchandleHTTP(w http.ResponseWriter, r*http.Request) {
ws, err:=websocket.Accept(w, r, nil)
iferr==nil {
gohandleWS(ws)
}
}
funchandleWS(conn*websocket.Conn) {
deferconn.Close(websocket.StatusInternalError, "oops")
ctx:=context.Background()
for {
_, _, err:=conn.Read(ctx)
iferr!=nil {
return
}
}
}
funcmain() {
ctx:=context.Background()
server:=httptest.NewServer(http.HandlerFunc(handleHTTP))
deferserver.Close()
url:=strings.Replace(server.URL, "http", "ws", 1)
client, _, _:=websocket.Dial(ctx, url, nil)
// write something in backgroundjoinCh:=make(chanstruct{})
gofunc() {
deferclose(joinCh)
for {
err:=client.Write(ctx, websocket.MessageText, []byte("test"))
iferr!=nil {
log.Print("client write error: ", err)
log.Print("client close status code: ", websocket.CloseStatus(err))
return
}
}
}()
// close after some timetime.Sleep(time.Millisecond)
_=client.Close(websocket.StatusNormalClosure, "bye")
<-joinCh
}
Expected output
2020/03/15 21:20:23 client write error: failed to write msg: failed to close writer: failed to write fin frame: failed to write frame: WebSocket closed: sent close frame: status = StatusNormalClosure and reason = "bye"
2020/03/15 21:20:23 client close status code: StatusNormalClosure
Unexpected output
2020/03/15 21:20:21 client write error: failed to write msg: failed to close writer: failed to write fin frame: failed to write frame: failed to flush: write tcp 127.0.0.1:59943->127.0.0.1:59942: use of closed network connection
2020/03/15 21:20:21 client close status code: StatusCode(-1)
The text was updated successfully, but these errors were encountered:
Everywhere we got an error when read from / write into underlying transport (incl. flush write buffer), we should check if we have closed the connection, and wrap a CloseError.
How to reproduce
Expected output
Unexpected output
The text was updated successfully, but these errors were encountered: