Skip to content
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
5 changes: 3 additions & 2 deletions lib/async/container/supervisor/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ def next_id
#
# @parameter message [Hash] The message to write.
def write(**message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to do this, I think it's better that we are explicit rather than just silently failing the write, e.g. raise IOError

Suggested change
def write(**message)
def write(**message)
raise IOError, "Connection closed!" if @stream.nil?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Then, we should probably have the caller deal with this exception gracefully).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup - good idea 👍
It looks like Connection#call and Connection#dispatch already gracefully handles the IOError

@stream.write(JSON.dump(message) << "\n")
@stream.flush
raise IOError, "Connection is closed!" unless @stream
@stream&.write(JSON.dump(message) << "\n")
@stream&.flush # it is possible for @stream to become nil after the write call
end

# Read a message from the connection stream.
Expand Down
21 changes: 21 additions & 0 deletions test/async/container/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def dispatch(call)
end
end

class SlowWriteStringIO < StringIO
def write(data)
super(data)
sleep(1) # Simulate a slow write
end
end

describe Async::Container::Supervisor::Connection do
let(:stream) {StringIO.new}
let(:connection) {Async::Container::Supervisor::Connection.new(stream)}
Expand Down Expand Up @@ -208,5 +215,19 @@ def dispatch(call)
connection.close
expect(stream).to be(:closed?)
end

it "closes while writing" do
slow_connection = Async::Container::Supervisor::Connection.new(SlowWriteStringIO.new)

Async do
Async do |task|
slow_connection.write(id: 1, do: :test)
end

Async do |task|
slow_connection.close
end
end
end
end
end
Loading