xonecas.io
60255
Zn

Zoea-Nova / commit 245a584

fix: prevent Stop() state from being overridden by context cancel errors

Race condition: When Stop() cancels a mysis context, any in-flight
LLM call returns with 'context canceled' error. The error handler
(setError/setErrorState) would then override the Stopped state with
Errored state, causing TUI to show 'errored' instead of 'stopped'.

Race Timeline:
1. Stop() called → cancels context → releases mu lock
2. Stop() waits for turnMu (in-flight turn to complete)
3. In-flight turn sees context cancelled → returns error
4. Error handler calls setError(err) → sets state = Errored
5. Stop() re-acquires mu lock → sees Errored state → exits early
6. Result: Mysis shows 'errored' instead of 'stopped' in TUI

Solution:
- Check if state == Stopped in setError() before overriding
- Check if state == Stopped in setErrorState() before overriding
- If mysis was intentionally stopped, ignore context cancel errors
- Log debug message explaining why error was ignored

This prevents the race where context cancellation errors override
the intentional Stop() state transition. The Stopped state takes
precedence over errors that occur during shutdown.

Changes:
- internal/core/mysis.go: Add state check in setError()
- internal/core/mysis.go: Add state check in setErrorState()
- internal/core/mysis_test.go: Add TestMysisStopDoesNotOverrideWithError

Test validates:
1. Mysis can be started normally
2. Stop() transitions to Stopped (not Errored)
3. LastError is nil after clean stop
4. Context cancel errors don't override Stopped state

All tests passing (77.3% core coverage)

xonecas2026-02-06 245a5845fd9d58370dede9a69f59d82a250269fc parent 8ab07b9

2 files changed

internal/core/mysis.go+18−0
@@ -838,6 +838,16 @@ func (m *Mysis) setError(err error) {
838838 a := m
839839 a.mu.Lock()
840840 oldState := a.state
841+
842+ // If mysis was intentionally stopped, don't override with error state
843+ // This prevents race where Stop() cancels context, causing in-flight turn
844+ // to error with "context canceled" and override the Stopped state
845+ if oldState == MysisStateStopped {
846+ a.mu.Unlock()
847+ log.Debug().Str("mysis", a.name).Err(err).Msg("Ignoring error - mysis was intentionally stopped")
848+ return
849+ }
850+
841851 a.lastError = err
842852 a.state = MysisStateErrored
843853 a.mu.Unlock()
@@ -1502,6 +1512,14 @@ func (m *Mysis) setErrorState(err error) {
15021512 a := m
15031513 a.mu.Lock()
15041514 oldState := a.state
1515+
1516+ // If mysis was intentionally stopped, don't override with error state
1517+ if oldState == MysisStateStopped {
1518+ a.mu.Unlock()
1519+ log.Debug().Str("mysis", a.name).Err(err).Msg("Ignoring error - mysis was intentionally stopped")
1520+ return
1521+ }
1522+
15051523 a.state = MysisStateErrored
15061524 a.lastError = err
15071525 a.mu.Unlock()
internal/core/mysis_test.go+35−0
@@ -918,3 +918,38 @@ func TestFindCurrentTick(t *testing.T) {
918918 })
919919 }
920920 }
921+
922+// TestMysisStopDoesNotOverrideWithError tests that stopping a mysis
923+// doesn't allow concurrent error to override Stopped state.
924+func TestMysisStopDoesNotOverrideWithError(t *testing.T) {
925+ s, bus, cleanup := setupMysisTest(t)
926+ defer cleanup()
927+
928+ stored, _ := s.CreateMysis("stop-test", "mock", "test-model", 0.7)
929+
930+ mock := provider.NewMock("mock", "test response")
931+ m := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus)
932+
933+ // Start the mysis
934+ if err := m.Start(); err != nil {
935+ t.Fatalf("Start() error: %v", err)
936+ }
937+
938+ // Give it a moment to stabilize
939+ time.Sleep(100 * time.Millisecond)
940+
941+ // Stop the mysis
942+ if err := m.Stop(); err != nil {
943+ t.Fatalf("Stop() error: %v", err)
944+ }
945+
946+ // Verify state is Stopped (not Errored)
947+ if m.State() != MysisStateStopped {
948+ t.Errorf("expected state=stopped, got %s (lastError: %v)", m.State(), m.LastError())
949+ }
950+
951+ // Verify no error is set
952+ if m.LastError() != nil {
953+ t.Errorf("expected no lastError after clean stop, got: %v", m.LastError())
954+ }
955+}