File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed
lib/async/container/supervisor Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -253,8 +253,9 @@ 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+ raise IOError , "Connection is closed!" unless @stream
257+ @stream &.write ( JSON . dump ( message ) << "\n " )
258+ @stream &.flush # it is possible for @stream to become nil after the write call
258259 end
259260
260261 # Read a message from the connection stream.
Original file line number Diff line number Diff line change @@ -17,6 +17,13 @@ def dispatch(call)
1717 end
1818end
1919
20+ class SlowWriteStringIO < StringIO
21+ def write ( data )
22+ super ( data )
23+ sleep ( 1 ) # Simulate a slow write
24+ end
25+ end
26+
2027describe 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
212233end
You can’t perform that action at this time.
0 commit comments