Skip to content

More robust handling of Connection#close. #12

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

Merged
merged 8 commits into from
Mar 7, 2023
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
52 changes: 30 additions & 22 deletions lib/protocol/websocket/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,32 @@ def flush
@framer.flush
end

def open!
@state = :open

return self
end

def close!(...)
@state = :closed

return self
end

def closed?
@state == :closed
end

def close(code = Error::NO_ERROR, reason = "")
send_close(code, reason) unless closed?
def close(...)
unless @state == :closed
close!

begin
send_close(...)
rescue
# Ignore.
end
end

@framer.close
end
Expand All @@ -80,11 +100,11 @@ def read_frame

return frame
rescue ProtocolError => error
send_close(error.code, error.message)
close(error.code, error.message)

raise
rescue
send_close(Error::PROTOCOL_ERROR, $!.message)
close(Error::PROTOCOL_ERROR, $!.message)

raise
end
Expand Down Expand Up @@ -120,11 +140,13 @@ def receive_continuation(frame)
end

def receive_close(frame)
@state = :closed

code, reason = frame.unpack

send_close(code, reason)
# If we're already closed, then we don't need to send a close frame. Otherwise, according to the RFC, we should echo the close frame. However, it's possible it will fail to send if the connection is already closed.
unless @state == :closed
close!
send_close(code, reason)
end

if code and code != Error::NO_ERROR
raise ClosedError.new reason, code
Expand All @@ -142,18 +164,6 @@ def send_ping(data = "")
end
end

def open!
@state = :open

return self
end

def close!
@state = :closed

return self
end

def receive_ping(frame)
if @state != :closed
write_frame(frame.reply(mask: @mask))
Expand Down Expand Up @@ -198,8 +208,6 @@ def send_close(code = Error::NO_ERROR, reason = "")

self.write_frame(frame)
self.flush

@state = :closed
end

# Write a message to the connection.
Expand Down Expand Up @@ -237,7 +245,7 @@ def read(**options)
end
end
rescue ProtocolError => error
send_close(error.code, error.message)
close(error.code, error.message)

raise
end
Expand Down
24 changes: 16 additions & 8 deletions test/protocol/websocket/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@

let(:connection) {subject.new(server)}

it "can manipulate open/closed state" do
expect(connection).not.to be(:closed?)
connection.close!
expect(connection).to be(:closed?)
connection.open!
expect(connection).not.to be(:closed?)
with '#close!' do
it "can manipulate open/closed state" do
expect(connection).not.to be(:closed?)
connection.close!
expect(connection).to be(:closed?)
connection.open!
expect(connection).not.to be(:closed?)
end

it "ignores write failures" do
server.close
connection.close!
expect(connection).to be(:closed?)
end
end

it "doesn't generate mask by default" do
Expand Down Expand Up @@ -158,8 +166,8 @@

expect(connection).to be(:closed?)

# frame = client.read_frame
# expect(frame).to be_a(Protocol::WebSocket::CloseFrame)
frame = client.read_frame
expect(frame).to be_a(Protocol::WebSocket::CloseFrame)
end
end

Expand Down