Skip to content

Commit 30e988f

Browse files
committed
make sure stream exists before flushing
1 parent 33487bb commit 30e988f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/async/container/supervisor/connection.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ def next_id
253253
#
254254
# @parameter message [Hash] The message to write.
255255
def write(**message)
256-
@stream.write(JSON.dump(message) << "\n")
257-
@stream.flush
256+
@stream&.write(JSON.dump(message) << "\n")
257+
@stream&.flush
258258
end
259259

260260
# Read a message from the connection stream.
@@ -327,9 +327,9 @@ def close
327327
@reader = nil
328328
end
329329

330-
if stream = @stream
330+
if @stream
331+
@stream.close
331332
@stream = nil
332-
stream.close
333333
end
334334

335335
if @calls

test/async/container/connection.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def dispatch(call)
1717
end
1818
end
1919

20+
class SlowWriteStringIO < StringIO
21+
def write(data)
22+
super(data)
23+
sleep(1) # Simulate a slow write
24+
end
25+
end
26+
2027
describe Async::Container::Supervisor::Connection do
2128
let(:stream) {StringIO.new}
2229
let(:connection) {Async::Container::Supervisor::Connection.new(stream)}
@@ -208,5 +215,19 @@ def dispatch(call)
208215
connection.close
209216
expect(stream).to be(:closed?)
210217
end
218+
219+
it "closes while writing" do
220+
slow_connection = Async::Container::Supervisor::Connection.new(SlowWriteStringIO.new)
221+
222+
Async do
223+
Async do |task|
224+
slow_connection.write(id: 1, do: :test)
225+
end
226+
227+
Async do |task|
228+
slow_connection.close
229+
end
230+
end
231+
end
211232
end
212233
end

0 commit comments

Comments
 (0)