Files
CommandForge/src/CommandForge.Infrastructure/Execution/SystemProcessRunner.cs
T
poli0981andClaude Opus 4.8 68e62f3f22 fix: cmd user-commands hang, recipe duplicate steps, sidebar scrollbar
Post-v1.1.0 fixes reported from testing.

- User commands hanging on cmd: SystemProcessRunner now redirects and
  immediately closes stdin, so an interactive process (e.g. "cmd" with
  no /c) gets EOF and exits instead of blocking forever. Additionally,
  UserCommandFactory.NormalizeArguments prepends /c for cmd (e.g. "cmd"
  + "ping google.com" -> "cmd /c ping google.com") so the command runs
  and exits; the run confirmation shows the effective command line.
  PowerShell and other executables are unchanged.
- Recipes: AddStep no longer adds a command already in the chain, so a
  recipe never runs one command multiple times.
- Sidebar: hide the vertical scrollbar on the command-groups list.
- Tests for the cmd /c normalization (cmd/cmd.exe/path, existing /c|/k,
  powershell, empty args) + ToDefinition applying it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 16:30:27 +07:00

116 lines
3.5 KiB
C#

using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Channels;
using CommandForge.Domain;
namespace CommandForge.Infrastructure.Execution;
/// <summary>Real <see cref="IProcessRunner"/> backed by <see cref="Process"/>.</summary>
internal sealed class SystemProcessRunner : IProcessRunner
{
/// <inheritdoc />
public async Task<int> RunAsync(
string fileName,
string arguments,
ChannelWriter<OutputLine> output,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(output);
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
// Redirect stdin so we can close it immediately: a process that reads input (e.g. an
// interactive "cmd" with no /c) gets EOF and exits instead of hanging forever.
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
// Pin the working directory to System32 so the CreateProcess search order can't
// resolve a bare executable name (e.g. "dism") from an attacker-controlled CWD —
// important because this runner also executes elevated commands via the broker.
WorkingDirectory = Environment.SystemDirectory,
},
};
process.OutputDataReceived += (_, e) =>
{
if (e.Data is not null)
{
output.TryWrite(new OutputLine(e.Data, IsError: false));
}
};
process.ErrorDataReceived += (_, e) =>
{
if (e.Data is not null)
{
output.TryWrite(new OutputLine(e.Data, IsError: true));
}
};
try
{
process.Start();
// Signal EOF on the child's stdin right away — nothing here ever feeds it input, and
// leaving it open lets an interactive process block indefinitely.
process.StandardInput.Close();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
try
{
await process.WaitForExitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
TryKillTree(process);
throw;
}
// Ensure the async stdout/stderr handlers have flushed before returning.
process.WaitForExit();
return process.ExitCode;
}
finally
{
output.TryComplete();
}
}
/// <inheritdoc />
public void Launch(string fileName, string arguments)
{
using var process = Process.Start(new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = true,
});
}
private static void TryKillTree(Process process)
{
try
{
if (!process.HasExited)
{
process.Kill(entireProcessTree: true);
}
}
catch (InvalidOperationException)
{
// Process already exited between the check and the kill.
}
catch (Win32Exception)
{
// Best-effort: the OS refused the kill (timing/permissions).
}
}
}