Skip to content

Commit

Permalink
fixup! Cancellation for Executable
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Aug 29, 2021
1 parent bc520f9 commit 2d3ea9d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 23 deletions.
17 changes: 13 additions & 4 deletions GitCommands/Git/Executable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,24 @@ public StreamReader StandardError
public void WaitForInputIdle() => _process.WaitForInputIdle();

/// <inheritdoc />
public Task<int> WaitForProcessExitAsync() => _exitTaskCompletionSource.Task;
public Task<int> WaitForExitAsync() => _exitTaskCompletionSource.Task;

/// <inheritdoc />
public Task WaitForExitAsync(CancellationToken token) => _process.WaitForExitAsync(token);
public Task<int> WaitForExitAsync(CancellationToken token)
{
// _process.WaitForExitAsync(token);
ThreadHelper.JoinableTaskFactory.RunAsync(() => _process.WaitForExitAsync(token));
if (token.IsCancellationRequested)
{
}

return WaitForExitAsync();
}

/// <inheritdoc />
public int WaitForProcessExit()
public int WaitForExit()
{
return ThreadHelper.JoinableTaskFactory.Run(() => WaitForProcessExitAsync());
return ThreadHelper.JoinableTaskFactory.Run(() => WaitForExitAsync());
}

/// <inheritdoc />
Expand Down
10 changes: 5 additions & 5 deletions GitCommands/Git/ExecutableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static class ExecutableExtensions

MemoryStream outputBuffer = new();
var outputTask = process.StandardOutput.BaseStream.CopyToAsync(outputBuffer);
var exitTask = process.WaitForProcessExitAsync();
var exitTask = process.WaitForExitAsync();

await Task.WhenAll(outputTask, exitTask);

Expand Down Expand Up @@ -223,7 +223,7 @@ string ComposeOutput()
process.StandardInput.Close();
}

return await process.WaitForProcessExitAsync() == 0;
return await process.WaitForExitAsync() == 0;
}

/// <summary>
Expand Down Expand Up @@ -316,15 +316,15 @@ string ComposeOutput()
#endif

// Wait for the process to exit (or be cancelled)
await process.WaitForExitAsync(cancellationToken);
await process.WaitForProcessExitAsync(cancellationToken);

// Await the output and exit status
var exitTask = process.WaitForProcessExitAsync();
var exitTask = process.WaitForExitAsync();
await Task.WhenAll(outputTask, errorTask, exitTask);

var output = outputEncoding.GetString(outputBuffer.GetBuffer(), 0, (int)outputBuffer.Length);
var error = outputEncoding.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Length);
var exitCode = await process.WaitForProcessExitAsync();
var exitCode = await process.WaitForExitAsync();

if (cache is not null && exitCode == 0)
{
Expand Down
10 changes: 5 additions & 5 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public void RunMergeTool(string? fileName = "", string? customTool = null)
fileName.ToPosixPath().QuoteNE()
};
using var process = _gitExecutable.Start(args, createWindow: true);
process.WaitForProcessExit();
process.WaitForExit();
}

public string Init(bool bare, bool shared)
Expand Down Expand Up @@ -1590,7 +1590,7 @@ public string ApplyPatch(string dir, ArgumentString amCommand)
}

process.StandardInput.Close();
process.WaitForProcessExit();
process.WaitForExit();

return process.StandardOutput.ReadToEnd().Trim();
}
Expand Down Expand Up @@ -1825,7 +1825,7 @@ public async Task<bool> AddInteractiveAsync(GitItemStatus file)
};

using var process = _gitExecutable.Start(args, createWindow: true);
return await process.WaitForProcessExitAsync() == 0;
return await process.WaitForExitAsync() == 0;
}

public async Task<bool> ResetInteractiveAsync(GitItemStatus file)
Expand All @@ -1837,7 +1837,7 @@ public async Task<bool> ResetInteractiveAsync(GitItemStatus file)
};

using var process = _gitExecutable.Start(args, createWindow: true);
return await process.WaitForProcessExitAsync() == 0;
return await process.WaitForExitAsync() == 0;
}

private static void UpdateIndex(StreamWriter inputWriter, string? filename)
Expand Down Expand Up @@ -3521,7 +3521,7 @@ public string GetFileText(ObjectId id, Encoding encoding)
using var process = _gitCommandRunner.RunDetached(args, redirectOutput: true);
MemoryStream stream = new();
process.StandardOutput.BaseStream.CopyTo(stream);
process.WaitForProcessExit();
process.WaitForExit();
stream.Position = 0;
return stream;
}
Expand Down
2 changes: 1 addition & 1 deletion GitCommands/Plink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<bool> ConnectAsync(string host)
var args = $"/k \"\"{AppSettings.Plink}\" -T {host}\"";

using var process = _executable.Start(args, createWindow: true, redirectInput: false, redirectOutput: false, outputEncoding: null);
return await process.WaitForProcessExitAsync() == 0;
return await process.WaitForExitAsync() == 0;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ private void OnShowSettingsClick(object sender, EventArgs e)
}).FileAndForget();
ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
revisionDiff.CancelLoadCustomDifftools();
RevisionGrid.CancelLoadCustomDifftools();
await new CustomDiffMergeToolProvider().ClearAsync(isDiff: true);
// The tool lists are created when the forms are init, must be redone after clearing the cache
Expand Down
5 changes: 5 additions & 0 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public void LoadCustomDifftools()
new CustomDiffMergeToolProvider().LoadCustomDiffMergeTools(Module, menus, components, isDiff: true, cancellationToken: _customDiffToolsSequence.Next());
}

public void CancelLoadCustomDifftools()
{
_customDiffToolsSequence.CancelCurrent();
}

private string GetShortcutKeyDisplayString(Command cmd)
{
return GetShortcutKeys((int)cmd).ToShortcutKeyDisplayString();
Expand Down
5 changes: 5 additions & 0 deletions GitUI/UserControls/RevisionGrid/RevisionGridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@ public void LoadCustomDifftools()
new CustomDiffMergeToolProvider().LoadCustomDiffMergeTools(Module, menus, components, isDiff: true, cancellationToken: _customDiffToolsSequence.Next());
}

public void CancelLoadCustomDifftools()
{
_customDiffToolsSequence.CancelCurrent();
}

private void SetSelectedIndex(int index, bool toggleSelection = false)
{
try
Expand Down
7 changes: 4 additions & 3 deletions Plugins/GitUIPluginInterfaces/IProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ public interface IProcess : IDisposable
/// Blocks the calling thread until the process exits, or when this object is disposed.
/// </summary>
/// <returns>The process's exit code, or <c>null</c> if this object was disposed before the process exited.</returns>
int WaitForProcessExit();
int WaitForExit();

/// <summary>
/// Returns a task that completes when the process exits, or when this object is disposed.
/// </summary>
/// <returns>A task that yields the process's exit code, or <c>null</c> if this object was disposed before the process exited.</returns>
Task<int> WaitForProcessExitAsync();
Task<int> WaitForExitAsync();

/// <summary>
/// Instructs the process component to wait for the associated process to exit, or for the cancellationToken to be cancelled (cancells the process).
/// Note that the exit task must be awaited too.
/// </summary>
/// <param name="token">An optional token to cancel the asynchronous operation.</param>
/// <returns>A task that will complete when the process has exited, cancellation has been requested, or an error occurs.</returns>
Task WaitForExitAsync(CancellationToken token);
Task WaitForProcessExitAsync(CancellationToken token);

/// <summary>
/// Waits for the process to reach an idle state.
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/CommonTestUtils/MockExecutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public MockProcess()
public StreamReader StandardOutput { get; }
public StreamReader StandardError { get; }

public int WaitForProcessExit()
public int WaitForExit()
{
return ThreadHelper.JoinableTaskFactory.Run(() => WaitForProcessExitAsync());
return ThreadHelper.JoinableTaskFactory.Run(() => WaitForExitAsync());
}

public Task<int> WaitForProcessExitAsync()
public Task<int> WaitForExitAsync()
{
if (_exitCode.HasValue)
{
Expand All @@ -144,9 +144,9 @@ public Task<int> WaitForProcessExitAsync()
}
}

public Task WaitForExitAsync(CancellationToken token)
public Task<int> WaitForExitAsync(CancellationToken token)
{
return Task.CompletedTask;
return WaitForExitAsync();
}

public void WaitForInputIdle()
Expand Down

0 comments on commit 2d3ea9d

Please sign in to comment.