diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 12b6c792..80c37e9a 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3515,27 +3515,8 @@ def tcp_socket(host, port) end def receive_responses - connection_closed = false - until connection_closed - synchronize do - @exception = nil - end - begin - resp = get_response - rescue Exception => e - synchronize do - state_logout! - @sock.close - @exception = e - end - break - end - unless resp - synchronize do - @exception = EOFError.new("end of file reached") - end - break - end + loop do + resp = get_response or raise EOFError, "end of file reached" begin synchronize do case resp @@ -3566,16 +3547,24 @@ def receive_responses @response_handlers.each do |handler| handler.call(resp) end - end - rescue Exception => e - @exception = e - synchronize do + rescue Exception => e + @exception = e @tagged_response_arrival.broadcast @continuation_request_arrival.broadcast + ensure + @exception = nil unless connection_closed end end end + ensure synchronize do + if $! + # Handling exceptions here, not in a rescue clause, so the lock isn't + # released and reacquired between rescue and ensure clauses. + @exception ||= $! + @sock.close + end + state_logout! @receiver_thread_terminating = true @tagged_response_arrival.broadcast @continuation_request_arrival.broadcast @@ -3583,8 +3572,6 @@ def receive_responses @idle_done_cond.signal end end - ensure - state_logout! end def get_response