@@ -87,6 +87,10 @@ impl HostOutputStream for MemoryOutputPipe {
8787 // This stream is always ready for writing.
8888 Ok ( ( ) )
8989 }
90+
91+ async fn flush ( & mut self ) -> Result < ( ) , Error > {
92+ Ok ( ( ) )
93+ }
9094}
9195
9296/// TODO
@@ -105,9 +109,7 @@ pub struct AsyncReadStream {
105109 state : StreamState ,
106110 buffer : Option < Result < Bytes , std:: io:: Error > > ,
107111 receiver : tokio:: sync:: mpsc:: Receiver < Result < ( Bytes , StreamState ) , std:: io:: Error > > ,
108- // the join handle for the background task is Some until join_background_tasks, after which
109- // further use of the AsyncReadStream is not allowed.
110- join_handle : Option < tokio:: task:: JoinHandle < ( ) > > ,
112+ join_handle : tokio:: task:: JoinHandle < ( ) > ,
111113}
112114
113115impl AsyncReadStream {
@@ -136,17 +138,13 @@ impl AsyncReadStream {
136138 state : StreamState :: Open ,
137139 buffer : None ,
138140 receiver,
139- join_handle : Some ( join_handle ) ,
141+ join_handle,
140142 }
141143 }
142144 // stdio implementation uses this to determine if the backing tokio runtime has been shutdown and
143145 // restarted:
144146 pub ( crate ) fn is_finished ( & self ) -> bool {
145- assert ! (
146- self . join_handle. is_some( ) ,
147- "illegal use of AsyncReadStream after join_background_tasks"
148- ) ;
149- self . join_handle . as_ref ( ) . unwrap ( ) . is_finished ( )
147+ self . join_handle . is_finished ( )
150148 }
151149}
152150
@@ -155,18 +153,14 @@ impl AsyncReadStream {
155153// on reader.read_buf's await it could hold the reader open indefinitely.
156154impl Drop for AsyncReadStream {
157155 fn drop ( & mut self ) {
158- self . join_handle . take ( ) . map ( |h| h . abort ( ) ) ;
156+ self . join_handle . abort ( ) ;
159157 }
160158}
161159
162160#[ async_trait:: async_trait]
163161impl HostInputStream for AsyncReadStream {
164162 fn read ( & mut self , size : usize ) -> Result < ( Bytes , StreamState ) , Error > {
165163 use tokio:: sync:: mpsc:: error:: TryRecvError ;
166- assert ! (
167- self . join_handle. is_some( ) ,
168- "illegal use of AsyncReadStream after join_background_tasks"
169- ) ;
170164
171165 match self . buffer . take ( ) {
172166 Some ( Ok ( mut bytes) ) => {
@@ -209,11 +203,6 @@ impl HostInputStream for AsyncReadStream {
209203 }
210204
211205 async fn ready ( & mut self ) -> Result < ( ) , Error > {
212- assert ! (
213- self . join_handle. is_some( ) ,
214- "illegal use of AsyncReadStream after join_background_tasks"
215- ) ;
216-
217206 if self . buffer . is_some ( ) || self . state == StreamState :: Closed {
218207 return Ok ( ( ) ) ;
219208 }
@@ -233,9 +222,6 @@ impl HostInputStream for AsyncReadStream {
233222 }
234223 Ok ( ( ) )
235224 }
236- async fn join_background_tasks ( & mut self ) {
237- self . join_handle . take ( ) . map ( |h| h. abort ( ) ) ;
238- }
239225}
240226
241227#[ derive( Debug ) ]
@@ -348,40 +334,6 @@ impl AsyncWriteStream {
348334 fn has_pending_op ( & self ) -> bool {
349335 matches ! ( self . state, Some ( WriteState :: Pending ) )
350336 }
351-
352- async fn flush ( & mut self ) -> anyhow:: Result < ( ) > {
353- // NB: This method needs to be "cancel safe" where it can be cancelled
354- // at any `.await` point but the flush operation still needs to be able
355- // to be restarted successfully.
356-
357- // First wait for any pending operation to complete to have the ability
358- // to send another message.
359- self . ready ( ) . await ?;
360-
361- // Queue up a flush operation in our background task, and if it's
362- // already gone then that's ok as flushing has completed anyway.
363- //
364- // Note that this may end up returning a queued error from a previous
365- // write or flush.
366- if !self . send ( WriteMesage :: Flush ) ? {
367- return Ok ( ( ) ) ;
368- }
369-
370- // Wait again for the flush to fully complete before considering this
371- // request to flush as fully complete.
372- self . ready ( ) . await ?;
373-
374- // Extract the error, if any, that occurred.
375- match mem:: replace ( & mut self . state , Some ( WriteState :: Ready ) ) {
376- Some ( WriteState :: Err ( e) ) => Err ( e. into ( ) ) ,
377- Some ( WriteState :: Pending ) => unreachable ! ( ) ,
378- Some ( WriteState :: Ready ) => Ok ( ( ) ) ,
379- None => {
380- self . state = None ;
381- Ok ( ( ) )
382- }
383- }
384- }
385337}
386338
387339// Make sure the background task does not outlive the AsyncWriteStream handle.
@@ -429,8 +381,38 @@ impl HostOutputStream for AsyncWriteStream {
429381 Ok ( ( ) )
430382 }
431383
432- async fn join_background_tasks ( & mut self ) {
433- let _ = self . flush ( ) . await ;
384+ async fn flush ( & mut self ) -> anyhow:: Result < ( ) > {
385+ // NB: This method needs to be "cancel safe" where it can be cancelled
386+ // at any `.await` point but the flush operation still needs to be able
387+ // to be restarted successfully.
388+
389+ // First wait for any pending operation to complete to have the ability
390+ // to send another message.
391+ self . ready ( ) . await ?;
392+
393+ // Queue up a flush operation in our background task, and if it's
394+ // already gone then that's ok as flushing has completed anyway.
395+ //
396+ // Note that this may end up returning a queued error from a previous
397+ // write or flush.
398+ if !self . send ( WriteMesage :: Flush ) ? {
399+ return Ok ( ( ) ) ;
400+ }
401+
402+ // Wait again for the flush to fully complete before considering this
403+ // request to flush as fully complete.
404+ self . ready ( ) . await ?;
405+
406+ // Extract the error, if any, that occurred.
407+ match mem:: replace ( & mut self . state , Some ( WriteState :: Ready ) ) {
408+ Some ( WriteState :: Err ( e) ) => Err ( e. into ( ) ) ,
409+ Some ( WriteState :: Pending ) => unreachable ! ( ) ,
410+ Some ( WriteState :: Ready ) => Ok ( ( ) ) ,
411+ None => {
412+ self . state = None ;
413+ Ok ( ( ) )
414+ }
415+ }
434416 }
435417}
436418
@@ -446,6 +428,10 @@ impl HostOutputStream for SinkOutputStream {
446428 async fn ready ( & mut self ) -> Result < ( ) , Error > {
447429 Ok ( ( ) )
448430 }
431+
432+ async fn flush ( & mut self ) -> Result < ( ) , Error > {
433+ Ok ( ( ) )
434+ }
449435}
450436
451437/// A stream that is ready immediately, but will always report that it's closed.
@@ -474,6 +460,10 @@ impl HostOutputStream for ClosedOutputStream {
474460 async fn ready ( & mut self ) -> Result < ( ) , Error > {
475461 Ok ( ( ) )
476462 }
463+
464+ async fn flush ( & mut self ) -> Result < ( ) , Error > {
465+ Ok ( ( ) )
466+ }
477467}
478468
479469#[ cfg( test) ]
0 commit comments