PipeReaderStream.ReadInternal throws AggregateException unexpectedly (#70248)
authorBadre BSAILA <54767641+pedrobsaila@users.noreply.github.com>
Fri, 22 Jul 2022 18:36:02 +0000 (20:36 +0200)
committerGitHub <noreply@github.com>
Fri, 22 Jul 2022 18:36:02 +0000 (14:36 -0400)
* PipeReaderStream.ReadInternal throws AggregateException unexpectedly

* add unit test

* assert on error message

* Update src/libraries/System.IO.Pipelines/tests/PipeReaderStreamTests.nonnetstandard.cs

simplify task throwing exception

Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs
src/libraries/System.IO.Pipelines/tests/PipeReaderStreamTests.nonnetstandard.cs

index bc8edbc..19128cc 100644 (file)
@@ -64,7 +64,7 @@ namespace System.IO.Pipelines
         private int ReadInternal(Span<byte> buffer)
         {
             ValueTask<ReadResult> vt = _pipeReader.ReadAsync();
-            ReadResult result = vt.IsCompletedSuccessfully ? vt.Result : vt.AsTask().Result;
+            ReadResult result = vt.IsCompletedSuccessfully ? vt.Result : vt.AsTask().GetAwaiter().GetResult();
             return HandleReadResult(result, buffer);
         }
 
index 803c101..e345bcf 100644 (file)
@@ -279,6 +279,14 @@ namespace System.IO.Pipelines.Tests
             Assert.Equal(producedSum, consumedSum);
         }
 
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+        public void ReadThrowsOriginalExceptionWhenItOccurs()
+        {
+            var pipeReader = new BuggyAndNotCompletedPipeReader();
+            Stream stream = pipeReader.AsStream();
+            AssertExtensions.Throws<InvalidOperationException>(() => stream.Read(new byte[3], 0, 3), "error occured during reading");
+        }
+
         [Fact]
         public void AsStreamDoNotCompleteReader()
         {
@@ -410,5 +418,11 @@ namespace System.IO.Pipelines.Tests
                 yield return new object[] { readSpanSync };
             }
         }
+
+        public class BuggyAndNotCompletedPipeReader : BuggyPipeReader
+        {
+            public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) =>
+                new ValueTask<ReadResult>(Task.FromException<ReadResult>(new InvalidOperationException("error occured during reading")));
+        }
     }
 }