@@ -34,10 +34,12 @@ public static async ValueTask<long> WriteAllAsync<T>(
3434 if ( source is null ) throw new ArgumentNullException ( nameof ( source ) ) ;
3535 Contract . EndContractBlock ( ) ;
3636
37- await target . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken ) . ConfigureAwait ( false ) ;
38-
3937 try
4038 {
39+ await target
40+ . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken )
41+ . ConfigureAwait ( false ) ;
42+
4143 long count = 0 ;
4244 var next = new ValueTask ( ) ;
4345 foreach ( ValueTask < T > e in source )
@@ -50,19 +52,20 @@ public static async ValueTask<long> WriteAllAsync<T>(
5052 await next . ConfigureAwait ( false ) ;
5153 return count ;
5254 }
55+ catch ( ChannelClosedException ) { throw ; }
5356 catch ( Exception ex )
5457 {
5558 if ( complete )
5659 {
57- target . Complete ( ex ) ;
60+ target . TryComplete ( ex ) ;
5861 complete = false ;
5962 }
6063 throw ;
6164 }
6265 finally
6366 {
6467 if ( complete )
65- target . Complete ( ) ;
68+ target . TryComplete ( ) ;
6669 }
6770 }
6871
@@ -246,11 +249,11 @@ public static async ValueTask<long> WriteAllLines(
246249 if ( source is null ) throw new ArgumentNullException ( nameof ( source ) ) ;
247250 Contract . EndContractBlock ( ) ;
248251
249- ValueTask next = target . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken ) ;
250- await next . ConfigureAwait ( false ) ;
251-
252252 try
253253 {
254+ ValueTask next = target . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken ) ;
255+ await next . ConfigureAwait ( false ) ;
256+
254257 long count = 0 ;
255258 bool more = false ; // if it completed and actually returned false, no need to bubble the cancellation since it actually completed.
256259 while ( ! cancellationToken . IsCancellationRequested )
@@ -275,19 +278,20 @@ public static async ValueTask<long> WriteAllLines(
275278 if ( more ) cancellationToken . ThrowIfCancellationRequested ( ) ;
276279 return count ;
277280 }
281+ catch ( ChannelClosedException ) { throw ; }
278282 catch ( Exception ex )
279283 {
280284 if ( complete )
281285 {
282- target . Complete ( ex ) ;
286+ target . TryComplete ( ex ) ;
283287 complete = false ;
284288 }
285289 throw ;
286290 }
287291 finally
288292 {
289293 if ( complete )
290- target . Complete ( ) ;
294+ target . TryComplete ( ) ;
291295 }
292296 }
293297
@@ -330,12 +334,12 @@ public static async ValueTask<long> WriteAllAsync<T>(
330334 if ( source is null ) throw new ArgumentNullException ( nameof ( source ) ) ;
331335 Contract . EndContractBlock ( ) ;
332336
333- await target
334- . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken )
335- . ConfigureAwait ( false ) ;
336-
337337 try
338338 {
339+ await target
340+ . WaitToWriteAndThrowIfClosedAsync ( ChannelClosedMessage , deferredExecution , cancellationToken )
341+ . ConfigureAwait ( false ) ;
342+
339343 long count = 0 ;
340344 var next = new ValueTask ( ) ;
341345 await foreach ( T ? value in source )
@@ -347,19 +351,20 @@ await target
347351 await next . ConfigureAwait ( false ) ;
348352 return count ;
349353 }
354+ catch ( ChannelClosedException ) { throw ; }
350355 catch ( Exception ex )
351356 {
352357 if ( complete )
353358 {
354- target . Complete ( ex ) ;
359+ target . TryComplete ( ex ) ;
355360 complete = false ;
356361 }
357362 throw ;
358363 }
359364 finally
360365 {
361366 if ( complete )
362- target . Complete ( ) ;
367+ target . TryComplete ( ) ;
363368 }
364369 }
365370
0 commit comments