diff --git a/vendor/gems/mongo-1.1.4/lib/mongo/connection.rb b/vendor/gems/mongo-1.1.4/lib/mongo/connection.rb index 68e7851..9aa49bf 100644 --- a/vendor/gems/mongo-1.1.4/lib/mongo/connection.rb +++ b/vendor/gems/mongo-1.1.4/lib/mongo/connection.rb @@ -431,11 +431,13 @@ module Mongo packed_message = message_with_headers.append!(message_with_check).to_s docs = num_received = cursor_id = '' @safe_mutexes[sock].synchronize do - send_message_on_socket(packed_message, sock) - docs, num_received, cursor_id = receive(sock) + close_socket_on_error(sock) do + send_message_on_socket(packed_message, sock) + docs, num_received, cursor_id = receive(sock) + end end ensure - checkin_writer(sock) + checkin_writer(sock) if sock && !sock.closed? end if num_received == 1 && (error = docs[0]['err'] || docs[0]['errmsg']) @@ -462,11 +464,15 @@ module Mongo result = '' @safe_mutexes[sock].synchronize do - send_message_on_socket(packed_message, sock) - result = receive(sock) + close_socket_on_error(sock) do + send_message_on_socket(packed_message, sock) + result = receive(sock) + end end ensure - command ? checkin_writer(sock) : checkin_reader(sock) + if sock && !sock.closed? + command ? checkin_writer(sock) : checkin_reader(sock) + end end result end @@ -927,5 +933,14 @@ module Mongo "" end end + + # When an exception occurs between a send/receive sequence or between + # receiving a header and a document, the client driver loses track of its + # place in the command stream. The socket must be closed. + def close_socket_on_error(socket) + yield + rescue Exception => exception + socket.close + end end end