| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | |
|
| | 8 | | namespace Azure.Identity |
| | 9 | | { |
| | 10 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable. Disposing of _process and _ctRe |
| | 11 | | internal sealed class ProcessRunner |
| | 12 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | 13 | | { |
| | 14 | | private readonly IProcess _process; |
| | 15 | | private readonly TimeSpan _timeout; |
| | 16 | | private readonly TaskCompletionSource<string> _tcs; |
| | 17 | | private readonly CancellationToken _cancellationToken; |
| | 18 | | private readonly CancellationTokenSource _timeoutCts; |
| | 19 | | private CancellationTokenRegistration _ctRegistration; |
| | 20 | |
|
| 188 | 21 | | public ProcessRunner(IProcess process, TimeSpan timeout, CancellationToken cancellationToken) |
| | 22 | | { |
| 188 | 23 | | _process = process; |
| 188 | 24 | | _timeout = timeout; |
| 188 | 25 | | _tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); |
| | 26 | |
|
| 188 | 27 | | if (timeout.TotalMilliseconds >= 0) |
| | 28 | | { |
| 188 | 29 | | _timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| 188 | 30 | | _cancellationToken = _timeoutCts.Token; |
| | 31 | | } |
| | 32 | | else |
| | 33 | | { |
| 0 | 34 | | _cancellationToken = cancellationToken; |
| | 35 | | } |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public Task<string> RunAsync() |
| | 39 | | { |
| 94 | 40 | | StartProcess(); |
| 94 | 41 | | return _tcs.Task; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public string Run() |
| | 45 | | { |
| 94 | 46 | | StartProcess(); |
| | 47 | | #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). |
| 94 | 48 | | return _tcs.Task.GetAwaiter().GetResult(); |
| | 49 | | #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private void StartProcess() |
| | 53 | | { |
| 188 | 54 | | if (TrySetCanceled() || _tcs.Task.IsCompleted) |
| | 55 | | { |
| 8 | 56 | | return; |
| | 57 | | } |
| | 58 | |
|
| 338 | 59 | | _process.Exited += (o, e) => HandleExit(); |
| | 60 | |
|
| 180 | 61 | | _process.StartInfo.UseShellExecute = false; |
| 180 | 62 | | _process.StartInfo.RedirectStandardOutput = true; |
| 180 | 63 | | _process.StartInfo.RedirectStandardError = true; |
| | 64 | |
|
| 180 | 65 | | _timeoutCts?.CancelAfter(_timeout); |
| | 66 | |
|
| 180 | 67 | | _process.Start(); |
| 180 | 68 | | _ctRegistration = _cancellationToken.Register(HandleCancel, false); |
| 180 | 69 | | } |
| | 70 | |
|
| | 71 | | private void HandleExit() |
| | 72 | | { |
| 158 | 73 | | if (_process.ExitCode == 0) |
| | 74 | | { |
| 106 | 75 | | TrySetResult(_process.StandardOutput.ReadToEnd()); |
| | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 52 | 79 | | TrySetException(new InvalidOperationException(_process.StandardError.ReadToEnd())); |
| | 80 | | } |
| 52 | 81 | | } |
| | 82 | |
|
| | 83 | | private void HandleCancel() |
| | 84 | | { |
| 22 | 85 | | if (_tcs.Task.IsCompleted) |
| | 86 | | { |
| 0 | 87 | | return; |
| | 88 | | } |
| | 89 | |
|
| 22 | 90 | | if (!_process.HasExited) |
| | 91 | | { |
| | 92 | | try |
| | 93 | | { |
| 22 | 94 | | _process.Kill(); |
| 18 | 95 | | } |
| 4 | 96 | | catch (Exception ex) |
| | 97 | | { |
| 4 | 98 | | TrySetException(ex); |
| 4 | 99 | | return; |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 18 | 103 | | TrySetCanceled(); |
| 22 | 104 | | } |
| | 105 | |
|
| | 106 | | private void TrySetResult(string result) |
| | 107 | | { |
| 106 | 108 | | DisposeProcess(); |
| 106 | 109 | | _tcs.TrySetResult(result); |
| 106 | 110 | | } |
| | 111 | |
|
| | 112 | | private bool TrySetCanceled() |
| | 113 | | { |
| 206 | 114 | | if (_cancellationToken.IsCancellationRequested) |
| | 115 | | { |
| 26 | 116 | | DisposeProcess(); |
| 26 | 117 | | _tcs.TrySetCanceled(_cancellationToken); |
| | 118 | | } |
| | 119 | |
|
| 206 | 120 | | return _cancellationToken.IsCancellationRequested; |
| | 121 | | } |
| | 122 | |
|
| | 123 | | private void TrySetException(Exception exception) |
| | 124 | | { |
| 56 | 125 | | DisposeProcess(); |
| 56 | 126 | | _tcs.TrySetException(exception); |
| 56 | 127 | | } |
| | 128 | |
|
| | 129 | | private void DisposeProcess() |
| | 130 | | { |
| 188 | 131 | | _process.Dispose(); |
| 188 | 132 | | _ctRegistration.Dispose(); |
| 188 | 133 | | _timeoutCts?.Dispose(); |
| 188 | 134 | | } |
| | 135 | | } |
| | 136 | | } |