Skip to content

Commit 22cc8f9

Browse files
committed
v0.55.2 — Fix /stop race condition + misleading 'No active task' message
Two bugs in the OnCommand /stop handler: 1. Race condition: both /stop's OnCommand handler and handleChatMessage's cancellation handler called LoadAndDelete on chatRunInfos. The loser saw an empty map and reported 'No active task to stop' even though the cancel function was found and called. 2. Missing fallback: when the cancel func was found but chatRunInfos was empty (race or first-iteration timing), the handler fell through to 'No active task' instead of reporting cancellation. Fix: use Load (read-only) instead of LoadAndDelete for chatRunInfos, leaving cleanup to handleChatMessage's defer. Track whether the cancel func was actually called, and report '⏹️ Task cancelled.' when run info is unavailable but cancellation was successful.
1 parent a36e450 commit 22cc8f9

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

cmd/odek/telegram.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,24 @@ func telegramCmd(args []string) error {
384384
// Handle /stop — cancel the running agent task and report a summary.
385385
if cmdName == "stop" {
386386
// Cancel the running agent context, if any.
387+
cancelled := false
387388
if cancelVal, ok := chatCancels.LoadAndDelete(chatID); ok {
388389
cancel := cancelVal.(context.CancelFunc)
389390
cancel()
391+
cancelled = true
390392
}
391393
// Retrieve the latest run info for a summary of what was interrupted.
394+
// Use Load, not LoadAndDelete — handleChatMessage's cancellation handler
395+
// and defer own the cleanup. Racing with LoadAndDelete here would strip
396+
// the info before the cancellation handler can format its own message.
392397
var summary string
393-
if infoVal, ok := chatRunInfos.LoadAndDelete(chatID); ok {
398+
if infoVal, ok := chatRunInfos.Load(chatID); ok {
394399
info := infoVal.(loop.IterationInfo)
395400
summary = formatStopSummary(info)
401+
} else if cancelled {
402+
// Cancel was sent but no run info yet (first iteration callback
403+
// hadn't fired). Report cancellation without detailed summary.
404+
summary = "⏹️ *Task cancelled.*"
396405
} else {
397406
summary = "⏹️ No active task to stop."
398407
}

0 commit comments

Comments
 (0)