diff --git a/lib/mqtt/client.rb b/lib/mqtt/client.rb index ae971b6..56e7fc7 100644 --- a/lib/mqtt/client.rb +++ b/lib/mqtt/client.rb @@ -271,7 +271,19 @@ def connect(clientid = nil) # Start packet reading thread @read_thread = Thread.new(Thread.current) do |parent| Thread.current[:parent] = parent - receive_packet while connected? + no_error = true + no_error = receive_packet while no_error && connected? + + if no_error + # Should not reach here on normal state since `disconnect` kills + # this thread, but it will occur when `receive_packet` catches no + # error and # `connected?` returns false. An error should be raised + # in this case too to break `get` loop. + @socket_semaphore.synchronize do + close_socket(false) + end + Thread.current[:parent].raise(MQTT::NotConnectedException) + end end end @@ -469,12 +481,14 @@ def receive_packet end keep_alive! end + true # Pass exceptions up to parent thread rescue Exception => exp @socket_semaphore.synchronize do close_socket(false) end Thread.current[:parent].raise(exp) + false end def wait_for_puback(id, queue)