fix: CLI-Prozess bei Abbruch zuverlässig beenden (Kill on Cancel)

Process.Dispose() gibt nur den Handle frei, killt aber nicht den Prozess.
Jetzt wird per CancellationToken.Register() der gesamte Prozessbaum
(entireProcessTree: true) sofort beendet wenn der User auf Stoppen klickt.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MelekiandClaude Opus 4.6 committed 2026-03-26 11:52:41 +01:00
1 parent aec71feff3
commit be6d33691a
1 file changed
+12 -5
+12 -5
View File
@@ -237,6 +237,13 @@ public class ClaudeCliService
process.Start();
process.BeginErrorReadLine();
// Bei Cancellation den gesamten Prozessbaum beenden
// (ReadLineAsync wirft OperationCanceledException, aber process.Dispose() killt NICHT den Prozess)
using var killOnCancel = cancellationToken.Register(() =>
{
try { if (!process.HasExited) process.Kill(entireProcessTree: true); } catch { }
});
await process.StandardInput.WriteAsync(stdinPrompt);
await process.StandardInput.FlushAsync();
process.StandardInput.Close();
@@ -245,11 +252,6 @@ public class ClaudeCliService
string line;
while ((line = await reader.ReadLineAsync(cancellationToken)) != null)
{
if (cancellationToken.IsCancellationRequested)
{
try { process.Kill(entireProcessTree: true); } catch { }
throw new OperationCanceledException();
}
if (!line.StartsWith("{"))
continue;
@@ -587,6 +589,11 @@ public class ClaudeCliService
process.BeginOutputReadLine();
process.BeginErrorReadLine();
using var killOnCancel = cancellationToken.Register(() =>
{
try { if (!process.HasExited) process.Kill(entireProcessTree: true); } catch { }
});
await process.WaitForExitAsync(cancellationToken);
result.ExitCode = process.ExitCode;