Doppel-Klick Hard-Stop + ReadLineAsync Cancellation-Fix
Stop-Button: Zwei Stufen statt einer:
- 1. Klick: Graceful Stop (workCt) - laufende API-Calls/Tools abbrechen
- 2. Klick: Hard Stop (hardCt) - sofortiger Abbruch aus while-Loop
StreamingTracker: Zwei separate CancellationTokenSources (StopCts + HardCts),
Cancel() gibt 'graceful'/'hard'/null zurück je nach Stufe.
ReadLineAsync Cancellation-Fix: StreamReader.ReadLineAsync propagiert den
CancellationToken nicht zuverlässig an den underlying NetworkStream.
Bei extended thinking blockiert ReadLineAsync minutenlang ohne Reaktion
auf Cancellation. Fix: workCt.Register() disposed den HTTP-Stream,
damit ReadLineAsync sofort mit Exception abbricht.
Bug-Fix: OperationCanceledException in RunAgentChat/ChatNonStreaming wurde
vom generischen catch(Exception) geschluckt - Agent lief trotz Stop weiter.
Jetzt: catch(OperationCanceledException) { throw; } vor catch(Exception).
Frontend: Stop-Button zeigt Hard-Stop visuell (rot + Pulse-Animation),
SSE-Stream wird erst beim 2. Klick clientseitig abgebrochen.
This commit is contained in:
1 parent
eb32a2526a
commit
b1d449b997
5 files changed
+92
-23
No files matched your search
@@ -52,6 +52,7 @@ public class AnthropicService
|
||||
bool allowUserChoice = true,
|
||||
bool planMode = false,
|
||||
CancellationToken workCt = default,
|
||||
CancellationToken hardCt = default,
|
||||
CancellationToken sseCt = default)
|
||||
{
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
@@ -71,6 +72,8 @@ public class AnthropicService
|
||||
{
|
||||
while (iterations < maxIterations)
|
||||
{
|
||||
// Hard-Stop: Sofortiger Abbruch aus while-Loop (Doppel-Klick Stop)
|
||||
hardCt.ThrowIfCancellationRequested();
|
||||
workCt.ThrowIfCancellationRequested();
|
||||
iterations++;
|
||||
|
||||
@@ -260,6 +263,9 @@ public class AnthropicService
|
||||
}
|
||||
|
||||
messages.Add(new { role = "user", content = toolResults });
|
||||
|
||||
// Hard-Stop Check nach Tool-Ausführung: Sofort raus statt nächste Iteration
|
||||
hardCt.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
await FinalizeChain(chain, usage, iterations, "max_iterations", sw);
|
||||
@@ -268,7 +274,14 @@ public class AnthropicService
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Stop-Button gedrückt (workCt wurde gecancelled)
|
||||
// Stop-Button gedrückt (workCt oder hardCt wurde gecancelled)
|
||||
await FinalizeChain(chain, usage, iterations, "stopped", sw);
|
||||
}
|
||||
catch (Exception) when (workCt.IsCancellationRequested || hardCt.IsCancellationRequested)
|
||||
{
|
||||
// Stream-Dispose durch CancellationToken-Registration löst
|
||||
// ObjectDisposedException/IOException aus statt OperationCanceledException.
|
||||
// Wenn workCt/hardCt gecancelled ist, ist das ein regulärer Stop.
|
||||
await FinalizeChain(chain, usage, iterations, "stopped", sw);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -752,6 +765,7 @@ public class AnthropicService
|
||||
string toolResult;
|
||||
bool isError = false;
|
||||
try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, agentWorkspace, _memoryService, agentId, workCt); }
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex) { toolResult = $"Fehler: {ex.Message}"; isError = true; }
|
||||
|
||||
toolResults.Add(new
|
||||
@@ -946,6 +960,7 @@ public class AnthropicService
|
||||
string toolResult;
|
||||
bool isError = false;
|
||||
try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, ctx.Workspace, _memoryService, ctx.AgentId); }
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex) { toolResult = $"Fehler: {ex.Message}"; isError = true; }
|
||||
|
||||
toolResults.Add(new
|
||||
@@ -1067,6 +1082,11 @@ public class AnthropicService
|
||||
CancellationToken sseCt = default)
|
||||
{
|
||||
var stream = await httpResp.Content.ReadAsStreamAsync(workCt);
|
||||
// Bei Cancellation den Stream disposen, damit ReadLineAsync sofort abbricht.
|
||||
// StreamReader.ReadLineAsync propagiert den CancellationToken nicht zuverlässig
|
||||
// an den underlying NetworkStream – der Read blockiert sonst bis Anthropic
|
||||
// das nächste Chunk sendet (kann bei extended thinking Minuten dauern).
|
||||
using var ctReg = workCt.Register(() => { try { stream.Dispose(); } catch { } });
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
var stopReason = "";
|
||||
|
||||
@@ -7,37 +7,56 @@ public class StreamingTracker
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, StreamingState> _streaming = new ConcurrentDictionary<string, StreamingState>();
|
||||
|
||||
public CancellationTokenSource Start(string sessionId, string chainId)
|
||||
public StreamingTokens Start(string sessionId, string chainId)
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
_streaming[sessionId] = new StreamingState(chainId, cts);
|
||||
return cts;
|
||||
var stopCts = new CancellationTokenSource();
|
||||
var hardCts = new CancellationTokenSource();
|
||||
var state = new StreamingState(chainId, stopCts, hardCts);
|
||||
_streaming[sessionId] = state;
|
||||
return new StreamingTokens(stopCts.Token, hardCts.Token);
|
||||
}
|
||||
|
||||
public void Stop(string sessionId)
|
||||
{
|
||||
if (_streaming.TryRemove(sessionId, out var state))
|
||||
{
|
||||
state.Cts.Dispose();
|
||||
state.StopCts.Dispose();
|
||||
state.HardCts.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels the running stream for a session. Returns true if a stream was cancelled.
|
||||
/// Cancels the running stream for a session.
|
||||
/// First call = graceful (laufende Iteration wird fertig).
|
||||
/// Second call = hard (sofortiger Abbruch aus while-Loop).
|
||||
/// Returns "graceful", "hard" or null if not streaming.
|
||||
/// </summary>
|
||||
public bool Cancel(string sessionId)
|
||||
public string Cancel(string sessionId)
|
||||
{
|
||||
if (_streaming.TryGetValue(sessionId, out var state))
|
||||
{
|
||||
try { state.Cts.Cancel(); } catch { }
|
||||
return true;
|
||||
if (!state.StopCts.IsCancellationRequested)
|
||||
{
|
||||
// Erster Klick → graceful stop
|
||||
try { state.StopCts.Cancel(); } catch { }
|
||||
return "graceful";
|
||||
}
|
||||
else if (!state.HardCts.IsCancellationRequested)
|
||||
{
|
||||
// Zweiter Klick → hard stop
|
||||
try { state.HardCts.Cancel(); } catch { }
|
||||
return "hard";
|
||||
}
|
||||
return "hard"; // Bereits beides gecancelled
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsStreaming(string sessionId) => _streaming.ContainsKey(sessionId);
|
||||
|
||||
public string GetChainId(string sessionId) => _streaming.TryGetValue(sessionId, out var state) ? state.ChainId : null;
|
||||
|
||||
private record StreamingState(string ChainId, CancellationTokenSource Cts);
|
||||
public record StreamingTokens(CancellationToken StopToken, CancellationToken HardToken);
|
||||
|
||||
private record StreamingState(string ChainId, CancellationTokenSource StopCts, CancellationTokenSource HardCts);
|
||||
}
|
||||
Reference in new issue
Block a user