From 582d44710bc392467e506a51fdc00ff78f74c599 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 6 Oct 2016 11:29:02 +1030 Subject: [PATCH 1/2] Avoid read/close race on the socket Kill the read thread, and wait for it to die, before we close the socket. --- lib/websocket-client-simple/client.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/websocket-client-simple/client.rb b/lib/websocket-client-simple/client.rb index 128f523..92854ef 100644 --- a/lib/websocket-client-simple/client.rb +++ b/lib/websocket-client-simple/client.rb @@ -84,11 +84,16 @@ def close if !@pipe_broken send nil, :type => :close end + @closed = true + if @thread + Thread.kill @thread + Thread.pass while @thread.status + @thread = nil + end @socket.close if @socket @socket = nil emit :__close - Thread.kill @thread if @thread end def open? From e161305f1a466b9398d86df3b1731b03362da91b Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 6 Oct 2016 11:32:41 +1030 Subject: [PATCH 2/2] Fix 'instance variable uninitialized' warning --- lib/websocket-client-simple/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/websocket-client-simple/client.rb b/lib/websocket-client-simple/client.rb index 92854ef..9d208d2 100644 --- a/lib/websocket-client-simple/client.rb +++ b/lib/websocket-client-simple/client.rb @@ -14,7 +14,7 @@ class Client attr_reader :url, :handshake def connect(url, options={}) - return if @socket + return if @socket ||= nil @url = url uri = URI.parse url @socket = TCPSocket.new(uri.host,