fix: move ctx=nil assignment AFTER turnMu wait to prevent uncanceled context leak
CRITICAL BUG identified by 10-agent emergency investigation: Moving ctx=nil and cancel=nil before the turnMu wait caused running turns to get parentCtx=nil, creating NEW uncanceled Background contexts that would never terminate, causing StopAll() to hang forever. Root Cause (Agent 4 + Agent 5): In commit 4fedd81, we moved these lines too early: - a.ctx = nil - a.cancel = nil These were set at line 291-292 BEFORE waiting for turnMu. Impact: 1. Stop() sets ctx=nil (line 291) 2. Running turn in SendMessageFrom reads a.ctx → gets nil (line 373) 3. SendMessageFrom creates context.Background() as fallback (line 376) 4. This context is NEVER canceled (Stop() already set cancel=nil) 5. Turn runs for full LLMRequestTimeout (5 minutes) 6. Stop() waits max 5 seconds, then times out 7. Multiple myses in this state = cascade failure 8. StopAll() hangs on wg.Wait() waiting for uncanceled goroutines 9. Terminal hangs waiting for app to exit User Impact: - All myses transitioned to errored state - App hung terminal on exit (wg.Wait infinite block) - Required kill -9 to recover Solution: - Keep state=Stopped set early (fixes setError race) - Move ctx=nil and cancel=nil AFTER turnMu wait completes - This ensures running turns can read valid parentCtx - Canceled context propagates to all child contexts - Turns exit cleanly within timeout Investigation Evidence: - Agent 1: DB shows all myses errored - Agent 2: 9-hour runtime, clean shutdown logs - Agent 4: FOUND ctx=nil moved too early - Agent 5: WaitGroup accounting correct - Agent 9: Confirmed run() can loop if context uncanceled - Agent 10: Emergency recovery completed Test Results: - TestStateTransition_Running_To_Stopped: 10/10 pass (was 0/10) - All state machine tests: PASS - Full test suite: PASS (77.3% coverage)
xonecas2026-02-06
bbe36a5fa120da312e3ee4dd25975c53b7a8e96b
parent dacef96