Skip to content

Commit

Permalink
Make sure to raise an error on exiting @read_thread
Browse files Browse the repository at this point in the history
Although an error should be always raised on exiting @read_thread to
break `get` loop, there was a case that it's not raised.

Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Mar 18, 2021
1 parent e9ddf6f commit 683ce8a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/mqtt/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 683ce8a

Please sign in to comment.