Websocket connection not getting closed when close() is called from client

2,930 views
Skip to first unread message

Dennis Francis

unread,
Jul 7, 2014, 8:55:20 AM7/7/14
to golan...@googlegroups.com
Hi


I am using a golang websocket server as per the gist below. 

When I try to call close() in the client ws object, 
  - the connection does not close. 
  - the client keeps receiving data from the server
  - The client ws object's "readyState" is 2 (CLOSING)
  - the server Send() does not return a non nil error.


Browser : Google Chrome 35.0.1916.153
Go version : 1.3



Am I missing something in the server code ? Please advise.

Thanks,
Dennis

Nicolas Hillegeer

unread,
Jul 7, 2014, 11:38:24 AM7/7/14
to golan...@googlegroups.com
Does the same thing happen if you explictly close the websocket connection on the server side as well? For example with a defer:

func dataFeeder(ws *websocket.Conn) {
defer ws.Close()
fmt.Println("Received a ws connection")

Dennis Francis

unread,
Jul 8, 2014, 2:37:56 AM7/8/14
to golan...@googlegroups.com
Yes adding defer ws.Close() does not change the results.

Thanks,
Dennis

Nicolas Hillegeer

unread,
Jul 8, 2014, 5:44:59 AM7/8/14
to golan...@googlegroups.com
It seems I misread your original post. I thought the dataFeeder() function was, in fact, exiting. An explicit ws.Close probably wouldn't have done anything more than just returning form dataFeeder (I think the http server cleans up connections by itself), though it's always nice to explicitly close the resources anyway.

There's two things that come to mind now:

1) Perhaps it's a bug in Chrome, when you call close() from the client side it doesn't actually close the connection. What happens when you close the tab in chrome? What happens when you close the browser entirely? Did you check if the FIN packet was sent with wireshark?
2) Another possibility is that the websocket close event is only relayed through the reader side. So try this: http://play.golang.org/p/-eueuhlxCg


That's the way I setup my WebSocket connections, spin up a goroutine for writing, and block the http handler on the reader side.

Dennis Francis

unread,
Jul 10, 2014, 12:06:06 AM7/10/14
to golan...@googlegroups.com

1) When I close the google chrome tab, the server Send() returns err = write tcp 127.0.0.1:60067: broken pipe
2) I tried the playground code
        ...
        err := websocket.Message.Receive(ws, nil)
fmt.Println("got a (disconnection) message:", err)
    This gets triggered and returns err = io.EOF when client close() is called. So that solves the problem.

Thanks to all,
Dennis

On Tuesday, July 8, 2014 8:45:29 PM UTC+5:30, gary b wrote:
Calling close() in the browser sends a close frame. It does not close the underlying network connection. My guess is that you need to read the connection on the server and close the connection when read returns an error. See 
http://godoc.org/github.com/gorilla/websocket#hdr-Read_is_Required for how another websocket package handles this issue.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages