diff --git a/assemblySize.include.md b/assemblySize.include.md index 213d6895..15fb6377 100644 --- a/assemblySize.include.md +++ b/assemblySize.include.md @@ -2,20 +2,20 @@ | | Empty Assembly | With Polyfill | Diff | Ensure | ArgumentExceptions | StringInterpolation | Nullability | |----------------|----------------|---------------|-----------|-----------|--------------------|---------------------|-------------| -| netstandard2.0 | 8.0KB | 407.5KB | +399.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| netstandard2.1 | 8.5KB | 352.0KB | +343.5KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | -| net461 | 8.5KB | 406.0KB | +397.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net462 | 7.0KB | 409.5KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net47 | 7.0KB | 409.5KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net471 | 8.5KB | 408.5KB | +400.0KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net472 | 8.5KB | 407.0KB | +398.5KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | -| net48 | 8.5KB | 407.0KB | +398.5KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | -| net481 | 8.5KB | 407.0KB | +398.5KB | +9.5KB | +7.0KB | +9.5KB | +14.5KB | -| netcoreapp2.0 | 9.0KB | 384.5KB | +375.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| netcoreapp2.1 | 9.0KB | 361.0KB | +352.0KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | -| netcoreapp2.2 | 9.0KB | 361.0KB | +352.0KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | -| netcoreapp3.0 | 9.5KB | 349.5KB | +340.0KB | +7.5KB | +6.5KB | +9.0KB | +14.0KB | -| netcoreapp3.1 | 9.5KB | 349.5KB | +340.0KB | +7.5KB | +6.5KB | +9.0KB | +14.0KB | +| netstandard2.0 | 8.0KB | 410.0KB | +402.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | +| netstandard2.1 | 8.5KB | 354.5KB | +346.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | +| net461 | 8.5KB | 408.5KB | +400.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net462 | 7.0KB | 412.0KB | +405.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net47 | 7.0KB | 412.0KB | +405.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net471 | 8.5KB | 411.0KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net472 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | +| net48 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | +| net481 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | +| netcoreapp2.0 | 9.0KB | 387.0KB | +378.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| netcoreapp2.1 | 9.0KB | 363.5KB | +354.5KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | +| netcoreapp2.2 | 9.0KB | 363.5KB | +354.5KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | +| netcoreapp3.0 | 9.5KB | 352.0KB | +342.5KB | +7.5KB | +6.5KB | +9.0KB | +12.5KB | +| netcoreapp3.1 | 9.5KB | 352.0KB | +342.5KB | +7.5KB | +6.5KB | +9.0KB | +12.5KB | | net5.0 | 9.5KB | 315.5KB | +306.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | | net6.0 | 10.0KB | 255.0KB | +245.0KB | +9.5KB | +6.5KB | +512bytes | +3.0KB | | net7.0 | 10.0KB | 223.5KB | +213.5KB | +9.5KB | +6.0KB | +1.0KB | +3.5KB | @@ -29,20 +29,20 @@ | | Empty Assembly | With Polyfill | Diff | Ensure | ArgumentExceptions | StringInterpolation | Nullability | |----------------|----------------|---------------|-----------|-----------|--------------------|---------------------|-------------| -| netstandard2.0 | 8.0KB | 595.0KB | +587.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| netstandard2.1 | 8.5KB | 507.3KB | +498.8KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | -| net461 | 8.5KB | 594.5KB | +586.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net462 | 7.0KB | 598.0KB | +591.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net47 | 7.0KB | 598.0KB | +591.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net471 | 8.5KB | 596.4KB | +587.9KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net472 | 8.5KB | 593.8KB | +585.3KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | -| net48 | 8.5KB | 593.8KB | +585.3KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | -| net481 | 8.5KB | 593.8KB | +585.3KB | +17.1KB | +8.7KB | +14.4KB | +19.9KB | -| netcoreapp2.0 | 9.0KB | 561.4KB | +552.4KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| netcoreapp2.1 | 9.0KB | 523.4KB | +514.4KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | -| netcoreapp2.2 | 9.0KB | 523.4KB | +514.4KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | -| netcoreapp3.0 | 9.5KB | 501.4KB | +491.9KB | +15.1KB | +8.2KB | +13.9KB | +19.4KB | -| netcoreapp3.1 | 9.5KB | 501.4KB | +491.9KB | +15.1KB | +8.2KB | +13.9KB | +19.4KB | +| netstandard2.0 | 8.0KB | 597.7KB | +589.7KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | +| netstandard2.1 | 8.5KB | 510.0KB | +501.5KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | +| net461 | 8.5KB | 597.2KB | +588.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net462 | 7.0KB | 600.7KB | +593.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net47 | 7.0KB | 600.7KB | +593.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net471 | 8.5KB | 599.1KB | +590.6KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net472 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | +| net48 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | +| net481 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | +| netcoreapp2.0 | 9.0KB | 564.1KB | +555.1KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| netcoreapp2.1 | 9.0KB | 526.1KB | +517.1KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | +| netcoreapp2.2 | 9.0KB | 526.1KB | +517.1KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | +| netcoreapp3.0 | 9.5KB | 504.1KB | +494.6KB | +15.1KB | +8.2KB | +13.9KB | +17.9KB | +| netcoreapp3.1 | 9.5KB | 504.1KB | +494.6KB | +15.1KB | +8.2KB | +13.9KB | +17.9KB | | net5.0 | 9.5KB | 449.5KB | +440.0KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | | net6.0 | 10.0KB | 367.6KB | +357.6KB | +17.1KB | +8.2KB | +1.1KB | +3.7KB | | net7.0 | 10.0KB | 318.7KB | +308.7KB | +17.0KB | +7.4KB | +1.6KB | +4.2KB | diff --git a/readme.md b/readme.md index 9b8e9f85..32df5071 100644 --- a/readme.md +++ b/readme.md @@ -101,20 +101,20 @@ Only [SDK-style projects](https://learn.microsoft.com/en-us/dotnet/core/project- | | Empty Assembly | With Polyfill | Diff | Ensure | ArgumentExceptions | StringInterpolation | Nullability | |----------------|----------------|---------------|-----------|-----------|--------------------|---------------------|-------------| -| netstandard2.0 | 8.0KB | 407.5KB | +399.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| netstandard2.1 | 8.5KB | 352.0KB | +343.5KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | -| net461 | 8.5KB | 406.0KB | +397.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net462 | 7.0KB | 409.5KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net47 | 7.0KB | 409.5KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net471 | 8.5KB | 408.5KB | +400.0KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| net472 | 8.5KB | 407.0KB | +398.5KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | -| net48 | 8.5KB | 407.0KB | +398.5KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | -| net481 | 8.5KB | 407.0KB | +398.5KB | +9.5KB | +7.0KB | +9.5KB | +14.5KB | -| netcoreapp2.0 | 9.0KB | 384.5KB | +375.5KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | -| netcoreapp2.1 | 9.0KB | 361.0KB | +352.0KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | -| netcoreapp2.2 | 9.0KB | 361.0KB | +352.0KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | -| netcoreapp3.0 | 9.5KB | 349.5KB | +340.0KB | +7.5KB | +6.5KB | +9.0KB | +14.0KB | -| netcoreapp3.1 | 9.5KB | 349.5KB | +340.0KB | +7.5KB | +6.5KB | +9.0KB | +14.0KB | +| netstandard2.0 | 8.0KB | 410.0KB | +402.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | +| netstandard2.1 | 8.5KB | 354.5KB | +346.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | +| net461 | 8.5KB | 408.5KB | +400.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net462 | 7.0KB | 412.0KB | +405.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net47 | 7.0KB | 412.0KB | +405.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net471 | 8.5KB | 411.0KB | +402.5KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| net472 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | +| net48 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +6.5KB | +9.5KB | +14.5KB | +| net481 | 8.5KB | 409.5KB | +401.0KB | +9.0KB | +7.0KB | +9.5KB | +14.5KB | +| netcoreapp2.0 | 9.0KB | 387.0KB | +378.0KB | +9.0KB | +6.5KB | +9.5KB | +14.0KB | +| netcoreapp2.1 | 9.0KB | 363.5KB | +354.5KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | +| netcoreapp2.2 | 9.0KB | 363.5KB | +354.5KB | +7.5KB | +5.5KB | +8.0KB | +13.0KB | +| netcoreapp3.0 | 9.5KB | 352.0KB | +342.5KB | +7.5KB | +6.5KB | +9.0KB | +12.5KB | +| netcoreapp3.1 | 9.5KB | 352.0KB | +342.5KB | +7.5KB | +6.5KB | +9.0KB | +12.5KB | | net5.0 | 9.5KB | 315.5KB | +306.0KB | +9.0KB | +6.5KB | +9.0KB | +14.0KB | | net6.0 | 10.0KB | 255.0KB | +245.0KB | +9.5KB | +6.5KB | +512bytes | +3.0KB | | net7.0 | 10.0KB | 223.5KB | +213.5KB | +9.5KB | +6.0KB | +1.0KB | +3.5KB | @@ -128,20 +128,20 @@ Only [SDK-style projects](https://learn.microsoft.com/en-us/dotnet/core/project- | | Empty Assembly | With Polyfill | Diff | Ensure | ArgumentExceptions | StringInterpolation | Nullability | |----------------|----------------|---------------|-----------|-----------|--------------------|---------------------|-------------| -| netstandard2.0 | 8.0KB | 595.0KB | +587.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| netstandard2.1 | 8.5KB | 507.3KB | +498.8KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | -| net461 | 8.5KB | 594.5KB | +586.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net462 | 7.0KB | 598.0KB | +591.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net47 | 7.0KB | 598.0KB | +591.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net471 | 8.5KB | 596.4KB | +587.9KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| net472 | 8.5KB | 593.8KB | +585.3KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | -| net48 | 8.5KB | 593.8KB | +585.3KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | -| net481 | 8.5KB | 593.8KB | +585.3KB | +17.1KB | +8.7KB | +14.4KB | +19.9KB | -| netcoreapp2.0 | 9.0KB | 561.4KB | +552.4KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | -| netcoreapp2.1 | 9.0KB | 523.4KB | +514.4KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | -| netcoreapp2.2 | 9.0KB | 523.4KB | +514.4KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | -| netcoreapp3.0 | 9.5KB | 501.4KB | +491.9KB | +15.1KB | +8.2KB | +13.9KB | +19.4KB | -| netcoreapp3.1 | 9.5KB | 501.4KB | +491.9KB | +15.1KB | +8.2KB | +13.9KB | +19.4KB | +| netstandard2.0 | 8.0KB | 597.7KB | +589.7KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | +| netstandard2.1 | 8.5KB | 510.0KB | +501.5KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | +| net461 | 8.5KB | 597.2KB | +588.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net462 | 7.0KB | 600.7KB | +593.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net47 | 7.0KB | 600.7KB | +593.7KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net471 | 8.5KB | 599.1KB | +590.6KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| net472 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | +| net48 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.2KB | +14.4KB | +19.9KB | +| net481 | 8.5KB | 596.5KB | +588.0KB | +16.6KB | +8.7KB | +14.4KB | +19.9KB | +| netcoreapp2.0 | 9.0KB | 564.1KB | +555.1KB | +16.6KB | +8.2KB | +14.4KB | +19.4KB | +| netcoreapp2.1 | 9.0KB | 526.1KB | +517.1KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | +| netcoreapp2.2 | 9.0KB | 526.1KB | +517.1KB | +15.1KB | +7.2KB | +12.9KB | +18.4KB | +| netcoreapp3.0 | 9.5KB | 504.1KB | +494.6KB | +15.1KB | +8.2KB | +13.9KB | +17.9KB | +| netcoreapp3.1 | 9.5KB | 504.1KB | +494.6KB | +15.1KB | +8.2KB | +13.9KB | +17.9KB | | net5.0 | 9.5KB | 449.5KB | +440.0KB | +16.6KB | +8.2KB | +13.9KB | +19.4KB | | net6.0 | 10.0KB | 367.6KB | +357.6KB | +17.1KB | +8.2KB | +1.1KB | +3.7KB | | net7.0 | 10.0KB | 318.7KB | +308.7KB | +17.0KB | +7.4KB | +1.6KB | +4.2KB | diff --git a/src/Polyfill/Polyfill_Process.cs b/src/Polyfill/Polyfill_Process.cs index 2f97848c..11363036 100644 --- a/src/Polyfill/Polyfill_Process.cs +++ b/src/Polyfill/Polyfill_Process.cs @@ -396,11 +396,10 @@ struct ProcessEntry //Link: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexitasync?view=net-11.0 public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - - if (target.HasExited) + // a token that is already canceled only throws while the process is still running + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try @@ -409,6 +408,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } @@ -426,12 +426,64 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + + // Exited is raised as soon as the process ends, which can be before redirected output that is being read + // asynchronously (BeginOutputReadLine/BeginErrorReadLine) has been delivered. + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + + // the parameterless WaitForExit is the only public way to wait for the end of that output + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + // Cancellation only applies while output is still pending. When there is none the drain is + // about to finish, so give it the chance to rather than report a cancellation. + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + + await drain; + } + } + + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + // the process was not started by this object, so it has no redirected output + return false; + } + } #endif #if !NET11_0_OR_GREATER diff --git a/src/Split/net461/Polyfill_Process.cs b/src/Split/net461/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net461/Polyfill_Process.cs +++ b/src/Split/net461/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net462/Polyfill_Process.cs b/src/Split/net462/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net462/Polyfill_Process.cs +++ b/src/Split/net462/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net47/Polyfill_Process.cs b/src/Split/net47/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net47/Polyfill_Process.cs +++ b/src/Split/net47/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net471/Polyfill_Process.cs b/src/Split/net471/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net471/Polyfill_Process.cs +++ b/src/Split/net471/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net472/Polyfill_Process.cs b/src/Split/net472/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net472/Polyfill_Process.cs +++ b/src/Split/net472/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net48/Polyfill_Process.cs b/src/Split/net48/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net48/Polyfill_Process.cs +++ b/src/Split/net48/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/net481/Polyfill_Process.cs b/src/Split/net481/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/net481/Polyfill_Process.cs +++ b/src/Split/net481/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netcoreapp2.0/Polyfill_Process.cs b/src/Split/netcoreapp2.0/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/netcoreapp2.0/Polyfill_Process.cs +++ b/src/Split/netcoreapp2.0/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netcoreapp2.1/Polyfill_Process.cs b/src/Split/netcoreapp2.1/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/netcoreapp2.1/Polyfill_Process.cs +++ b/src/Split/netcoreapp2.1/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netcoreapp2.2/Polyfill_Process.cs b/src/Split/netcoreapp2.2/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/netcoreapp2.2/Polyfill_Process.cs +++ b/src/Split/netcoreapp2.2/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netcoreapp3.0/Polyfill_Process.cs b/src/Split/netcoreapp3.0/Polyfill_Process.cs index 517fe9d4..06f240a3 100644 --- a/src/Split/netcoreapp3.0/Polyfill_Process.cs +++ b/src/Split/netcoreapp3.0/Polyfill_Process.cs @@ -21,10 +21,9 @@ static partial class Polyfill /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -32,6 +31,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -46,12 +46,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netcoreapp3.1/Polyfill_Process.cs b/src/Split/netcoreapp3.1/Polyfill_Process.cs index 517fe9d4..06f240a3 100644 --- a/src/Split/netcoreapp3.1/Polyfill_Process.cs +++ b/src/Split/netcoreapp3.1/Polyfill_Process.cs @@ -21,10 +21,9 @@ static partial class Polyfill /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -32,6 +31,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -46,12 +46,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netstandard2.0/Polyfill_Process.cs b/src/Split/netstandard2.0/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/netstandard2.0/Polyfill_Process.cs +++ b/src/Split/netstandard2.0/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/netstandard2.1/Polyfill_Process.cs b/src/Split/netstandard2.1/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/netstandard2.1/Polyfill_Process.cs +++ b/src/Split/netstandard2.1/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Split/uap10.0/Polyfill_Process.cs b/src/Split/uap10.0/Polyfill_Process.cs index 3b1449b8..15ce9cce 100644 --- a/src/Split/uap10.0/Polyfill_Process.cs +++ b/src/Split/uap10.0/Polyfill_Process.cs @@ -331,10 +331,9 @@ struct ProcessEntry /// public static async Task WaitForExitAsync(this Process target, CancellationToken cancellationToken = default) { - cancellationToken.ThrowIfCancellationRequested(); - if (target.HasExited) + if (!target.HasExited) { - return; + cancellationToken.ThrowIfCancellationRequested(); } try { @@ -342,6 +341,7 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken } catch (InvalidOperationException) when (target.HasExited) { + await WaitUntilOutputEof(target, cancellationToken); return; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -356,12 +356,52 @@ public static async Task WaitForExitAsync(this Process target, CancellationToken await tcs.Task.ConfigureAwait(false); } } + await WaitUntilOutputEof(target, cancellationToken); } finally { target.Exited -= handler; } } + static async Task WaitUntilOutputEof(Process target, CancellationToken cancellationToken) + { + if (!HasRedirectedOutput(target)) + { + return; + } + var drain = Task.Run(() => target.WaitForExit()); + if (!cancellationToken.CanBeCanceled) + { + await drain; + return; + } + var canceled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSource) s!).TrySetCanceled(cancellationToken), canceled)) + { + if (await Task.WhenAny(drain, canceled.Task) != drain) + { + await Task.WhenAny(drain, Task.Delay(100)); + if (!drain.IsCompleted) + { + await canceled.Task; + } + } + await drain; + } + } + static bool HasRedirectedOutput(Process target) + { + try + { + var startInfo = target.StartInfo; + return startInfo.RedirectStandardOutput || + startInfo.RedirectStandardError; + } + catch (InvalidOperationException) + { + return false; + } + } #if FeatureValueTuple /// /// Reads the standard output and standard error of the process as text, waiting for the process to exit. diff --git a/src/Tests/PolyfillTests_Process.cs b/src/Tests/PolyfillTests_Process.cs index 62840d75..ddafd9d4 100644 --- a/src/Tests/PolyfillTests_Process.cs +++ b/src/Tests/PolyfillTests_Process.cs @@ -27,6 +27,65 @@ public async Task Process_WaitForExitAsync_NotDirected_ThrowsInvalidOperationExc await Assert.That(async () => await process.WaitForExitAsync()).Throws(); } + [Test] + public async Task Process_WaitForExitAsync_WaitsForAsyncOutput() + { + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "dotnet", + Arguments = "--info", + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + } + }; + var lines = 0; + var endOfOutput = false; + process.OutputDataReceived += (_, e) => + { + if (e.Data is null) + { + Volatile.Write(ref endOfOutput, true); + return; + } + + // a slow consumer, so the output is still being delivered when the process exits + Thread.Sleep(5); + Interlocked.Increment(ref lines); + }; + process.Start(); + process.BeginOutputReadLine(); + + await process.WaitForExitAsync(); + + // The .NET 5 runtime's own WaitForExitAsync returns as soon as the process exits. Waiting for + // redirected output was added in .NET 6, which is the behavior the polyfill matches. + // Checked at run time so the test still asserts when a net5.0 build rolls forward. + if (Environment.Version.Major == 5) + { + process.WaitForExit(); + } + + await Assert.That(Volatile.Read(ref endOfOutput)).IsTrue(); + await Assert.That(Volatile.Read(ref lines)).IsGreaterThan(0); + } + + [Test] + public async Task Process_WaitForExitAsync_ExitedAndCanceled_Completes() + { + using var process = StartVersion(); + process.WaitForExit(); + using var cancelSource = new CancelSource(); + cancelSource.Cancel(); + + // cancellation is only observed while the process is still running + await process.WaitForExitAsync(cancelSource.Token); + + await Assert.That(process.HasExited).IsTrue(); + } + [Test] public async Task Process_Kill_EntireProcessTree() {