Skip to content

fix: [sc-89444] Derive per-cycle context so in-flight commands cancel on disconnect#71

Merged
mlataza merged 1 commit into
mainfrom
bug/sc-89444/root-context-shared-across-reconnection
May 13, 2026
Merged

fix: [sc-89444] Derive per-cycle context so in-flight commands cancel on disconnect#71
mlataza merged 1 commit into
mainfrom
bug/sc-89444/root-context-shared-across-reconnection

Conversation

@mlataza

@mlataza mlataza commented May 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • A single root context was created once at service startup and reused across every MQTT reconnect cycle. Hung commands could not be cancelled on disconnect, leaving worker goroutines blocked and consuming slots after reconnection.
  • runCycle now derives a fresh cycleCtx from the parent context on each invocation. The cycle context is cancelled when the cycle ends (disconnect or stop), which propagates through processMessageexecutor.Executeexec.CommandContext to terminate any in-flight command.
  • Deferred cleanup ordering is updated to MQTT teardown → cycleCancel()close(msgQueue)wg.Wait(). Cancelling before wg.Wait is required so a hung command cannot block worker drain.
  • Commands started in a later cycle bind to that cycle's own context and are unaffected by the previous cycle's cancellation.

Test plan

  • go build ./...
  • go test ./cmd/agent_smith/... (existing suite green, including the worker/queue/lost-channel regression tests)
  • Manual: run a 60s sleep command, force a network drop, verify the command process exits and the worker slot is reclaimed before the next command arrives on reconnection

🤖 Generated with Claude Code

… on disconnect

Each MQTT connection cycle now derives its own context from the parent and
cancels it when the cycle ends. Commands running under exec.CommandContext
are interrupted on disconnect, freeing worker slots before the next cycle
begins. The deferred cleanup runs MQTT teardown first, then cycleCancel
before wg.Wait so a hung command can't block worker drain.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mlataza
mlataza merged commit 5cc3669 into main May 13, 2026
21 checks passed
@mlataza
mlataza deleted the bug/sc-89444/root-context-shared-across-reconnection branch May 13, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant