Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Hangs forever in refresh after 5 minutes when 2ary unreachable #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/moped/connection/socket/connectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Socket
module Connectable

attr_reader :host, :port
attr_accessor :timeout

# Is the socket connection alive?
#
Expand Down Expand Up @@ -44,7 +45,14 @@ def self.included(klass)
# @since 1.2.0
def read(length)
check_if_alive!
handle_socket_errors { super }
handle_socket_errors {
if Kernel.select([self], nil, [self], @timeout)
super
else
raise Errors::ConnectionFailure,
"timeout #{@timeout} exceeded on read from #{host}:#{port}"
end
}
end

# Write to the socket.
Expand Down Expand Up @@ -145,9 +153,7 @@ def connect(host, port, timeout)
sock = new(host, port)
sock.set_encoding('binary')
timeout_val = [ timeout, 0 ].pack("l_2")
sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeout_val)
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeout_val)
sock.timeout = timeout
sock
end
rescue Timeout::Error
Expand Down
15 changes: 15 additions & 0 deletions spec/moped/connection/socket/tcp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'
require 'moped/connection/socket/tcp'

describe Moped::Connection::Socket::TCP do
it "raises Moped::Errors::ConnectionFailure if no response within timeout" do
Timeout::timeout(10) do
expect {
# this test relies on the mongo protocol expecting the client
# to speak first
socket = described_class.connect('127.0.0.1', 27017, 2)
socket.read(100)
}.to raise_exception Moped::Errors::ConnectionFailure
end
end
end