Skip to content

Commit 264b36d

Browse files
committed
Connection#close_write closes the connection on failure.
1 parent 6ae289f commit 264b36d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/protocol/websocket/connection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def close_write(error = nil)
104104
else
105105
send_close
106106
end
107+
rescue
108+
@state = :closed
107109
end
108110

109111
# Close the connection gracefully. This will send a close frame and wait for the remote end to respond with a close frame. Any data received after the close frame is sent will be ignored. If you want to process this data, use {#close_write} instead, and read the data before calling {#close}.

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- If `Connection#close_write` fails, the connection will now be fully closed to prevent hanging connections.
6+
37
## v0.21.0
48

59
- All frame reading and writing logic has been consolidated into `Framer` to improve performance.

test/protocol/websocket/connection.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,43 @@
4747

4848
expect(connection).to be(:closed?)
4949
end
50+
51+
it "does not raise if the underlying stream is closed" do
52+
server.close
53+
54+
expect do
55+
connection.close_write
56+
end.not.to raise_exception
57+
58+
expect(connection).to be(:closed?)
59+
end
60+
61+
it "does not raise if the underlying stream is closed (with error)" do
62+
server.close
63+
64+
expect do
65+
connection.close_write(RuntimeError.new("something went wrong"))
66+
end.not.to raise_exception
67+
68+
expect(connection).to be(:closed?)
69+
end
70+
71+
it "is not closed when close_write succeeds" do
72+
connection.close_write
73+
74+
expect(connection).not.to be(:closed?)
75+
end
76+
77+
it "can still read after a successful close_write" do
78+
text_frame = Protocol::WebSocket::TextFrame.new(true)
79+
text_frame.pack("Hello")
80+
client.write_frame(text_frame)
81+
82+
connection.close_write
83+
84+
message = connection.read
85+
expect(message).to be == "Hello"
86+
end
5087
end
5188

5289
with "#shutdown" do

0 commit comments

Comments
 (0)