Skip to content
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

Fix: Socket#accept obeys read_timeout #9538

Merged
merged 1 commit into from
Jul 3, 2020
Merged
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
11 changes: 11 additions & 0 deletions spec/std/socket/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ describe Socket do
end
end

it "accept raises timeout error if read_timeout is specified" do
server = Socket.new(Socket::Family::INET, Socket::Type::STREAM, Socket::Protocol::TCP)
port = unused_local_port
server.bind("0.0.0.0", port)
server.read_timeout = 0.1
server.listen

expect_raises(IO::TimeoutError) { server.accept }
expect_raises(IO::TimeoutError) { server.accept? }
end

it "sends messages" do
port = unused_local_port
server = Socket.tcp(Socket::Family::INET)
Expand Down
12 changes: 6 additions & 6 deletions src/io/evented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@ module IO::Evented

# :nodoc:
def wait_readable(timeout = @read_timeout)
wait_readable(timeout: timeout) { |err| raise err }
wait_readable(timeout: timeout) { raise TimeoutError.new("Read timed out") }
end

# :nodoc:
def wait_readable(timeout = @read_timeout) : Nil
def wait_readable(timeout = @read_timeout, *, raise_if_closed = true) : Nil
readers = @readers.get { Deque(Fiber).new }
readers << Fiber.current
add_read_event(timeout)
Crystal::Scheduler.reschedule

if @read_timed_out
@read_timed_out = false
yield TimeoutError.new("Read timed out")
yield
end

check_open
check_open if raise_if_closed
end

private def add_read_event(timeout = @read_timeout) : Nil
Expand All @@ -142,7 +142,7 @@ module IO::Evented

# :nodoc:
def wait_writable(timeout = @write_timeout)
wait_writable(timeout: timeout) { |err| raise err }
wait_writable(timeout: timeout) { raise TimeoutError.new("Write timed out") }
end

# :nodoc:
Expand All @@ -154,7 +154,7 @@ module IO::Evented

if @write_timed_out
@write_timed_out = false
yield TimeoutError.new("Write timed out")
yield
end

check_open
Expand Down
11 changes: 9 additions & 2 deletions src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Socket < IO
when Errno::EISCONN
return
when Errno::EINPROGRESS, Errno::EALREADY
wait_writable(timeout: timeout) do |error|
wait_writable(timeout: timeout) do
return yield IO::TimeoutError.new("connect timed out")
end
else
Expand Down Expand Up @@ -271,7 +271,8 @@ class Socket < IO
if closed?
return
elsif Errno.value == Errno::EAGAIN
wait_readable rescue nil
wait_acceptable
return if closed?
else
raise Socket::Error.from_errno("accept")
end
Expand All @@ -281,6 +282,12 @@ class Socket < IO
end
end

private def wait_acceptable
wait_readable(raise_if_closed: false) do
raise TimeoutError.new("Accept timed out")
end
end

# Sends a message to a previously connected remote address.
#
# ```
Expand Down