From be6d33691a9f7b3e0319d2975ab68bd7a0afb237 Mon Sep 17 00:00:00 2001 From: Meleki Date: Thu, 26 Mar 2026 11:52:41 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20CLI-Prozess=20bei=20Abbruch=20zuverl?= =?UTF-8?q?=C3=A4ssig=20beenden=20(Kill=20on=20Cancel)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Services/ClaudeCliService.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Services/ClaudeCliService.cs b/Services/ClaudeCliService.cs index 6d66ccd..c553c19 100644 --- a/Services/ClaudeCliService.cs +++ b/Services/ClaudeCliService.cs @@ -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;