From 6523056e7ab5d829877d902c7163846e460f71cb Mon Sep 17 00:00:00 2001 From: Joris Kraak Date: Thu, 27 Jul 2017 14:11:20 +0200 Subject: [PATCH] Improve handling of sudden client disconnections This prevents sudden client disconnections (such as refreshing a browser window), occassionaly generating an error when trying to acknowledge the disconnection attempt due to the underlying socket already being closed. From a consumer of WebSockets.jl this should look similar to a regular disconnection by throwing WebSocketClosedError instead. --- src/WebSockets.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/WebSockets.jl b/src/WebSockets.jl index 611f648..68db7dc 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -282,7 +282,17 @@ function handle_control_frame(ws::WebSocket,wsf::WebSocketFragment) # The other side initiated the disconnect, so the action must be # acknowledged by replying with an empty CLOSE frame and cleaning # up - locked_write(ws.socket, true, "", OPCODE_CLOSE) + try + locked_write(ws.socket, true, "", OPCODE_CLOSE) + catch exception + # On sudden disconnects, the other side may be gone before the + # close acknowledgement can be sent. This will cause an + # ArgumentError to be thrown due to the underlying stream being + # closed. These are swallowed here and will be replaced by a + # WebSocketClosedError below + !isa(exception, ArgumentError) && rethrow(exception) + end + close(ws.socket) end