Skip to content

Fixing error handling for socket timeouts that occur due to a race-li… #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/poseidon/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ def read_response(response_class)
s = ensure_read_or_timeout(n)
buffer = Protocol::ResponseBuffer.new(s)
response_class.read(buffer)
rescue Errno::ECONNRESET, SocketError, TimeoutException
rescue Errno::ECONNRESET, SocketError, Errno::ETIMEDOUT, TimeoutException
@socket = nil
raise_connection_failed_error
end

def ensure_read_or_timeout(maxlen)
if IO.select([@socket], nil, nil, @socket_timeout_ms / 1000.0)
# There is a potential race condition here, where the socket could time out between IO Select and Write
# This will throw a Errno::ETIMEDOUT, but it will be caught in the calling code
@socket.read(maxlen)
else
raise TimeoutException.new
Expand All @@ -135,13 +137,15 @@ def send_request(request)
buffer = Protocol::RequestBuffer.new
request.write(buffer)
ensure_write_or_timeout([buffer.to_s.bytesize].pack("N") + buffer.to_s)
rescue Errno::EPIPE, Errno::ECONNRESET, TimeoutException
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ETIMEDOUT, TimeoutException
@socket = nil
raise_connection_failed_error
end

def ensure_write_or_timeout(data)
if IO.select(nil, [@socket], nil, @socket_timeout_ms / 1000.0)
# There is a potential race condition here, where the socket could time out between IO Select and Write
# This will throw a Errno::ETIMEDOUT, but it will be caught in the calling code
@socket.write(data)
else
raise TimeoutException.new
Expand Down