xonecas.io
60255
Zn

Zoea-Nova / commit bbe36a5

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

1 file changed

internal/core/mysis.go+8−3
@@ -288,8 +288,6 @@ func (m *Mysis) Stop() error {
288288 // This closes the race window where setError() could override with Errored
289289 oldState := a.state
290290 a.state = MysisStateStopped
291- a.ctx = nil
292- a.cancel = nil
293291 a.mu.Unlock()
294292
295293 // Update store (if fails, in-memory state already correct)
@@ -302,7 +300,8 @@ func (m *Mysis) Stop() error {
302300 a.emitStateChange(oldState, MysisStateStopped)
303301
304302 // Wait for current turn to finish with timeout
305- // Goroutine will see state=Stopped and exit cleanly
303+ // IMPORTANT: Don't clear ctx/cancel until AFTER turn completes
304+ // Otherwise SendMessageFrom gets nil ctx and creates uncanceled Background context
306305 done := make(chan struct{})
307306 go func() {
308307 a.turnMu.Lock()
@@ -318,6 +317,12 @@ func (m *Mysis) Stop() error {
318317 // Continue with cleanup even if turn didn't complete
319318 }
320319
320+ // Clear context references AFTER turn completes
321+ a.mu.Lock()
322+ a.ctx = nil
323+ a.cancel = nil
324+ a.mu.Unlock()
325+
321326 // Release resources
322327 a.releaseCurrentAccount()