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;