Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5ff0c4c

Browse files
committed
Fix bug in Process' AsyncStreamReader.BeginReadLine
BeginReadLine is kicking off a Task to asynchronously run the ReadBuffer method. ReadBuffer is itself an async method, returning a Task. But Task.Factory.StartNew was being used to invoke ReadBuffer, such that StartNew was returning a Task<Task>, and StartNew doesn't automatically unwrap such inner Tasks. This meant that the _readToBufferTask was completing as soon as the first await yielded, which meant that Process.WaitForExit wouldn't actually wait for the async reading to complete. This change simply switches to using Task.Run, which automatically unwraps the inner task due to its overload that takes a Func<Task> delegate.
1 parent fac8a01 commit 5ff0c4c

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/System.Diagnostics.Process/src/System/Diagnostics/AsyncStreamReader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ internal void BeginReadLine()
6969
if (_sb == null)
7070
{
7171
_sb = new StringBuilder(DefaultBufferSize);
72-
_readToBufferTask = Task.Factory.StartNew(s => ((AsyncStreamReader)s).ReadBuffer(),
73-
this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
72+
_readToBufferTask = Task.Run((Func<Task>)ReadBufferAsync);
7473
}
7574
else
7675
{
@@ -84,7 +83,7 @@ internal void CancelOperation()
8483
}
8584

8685
// This is the async callback function. Only one thread could/should call this.
87-
private async Task ReadBuffer()
86+
private async Task ReadBufferAsync()
8887
{
8988
while (true)
9089
{

0 commit comments

Comments
 (0)