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