xonecas.io
60255
Zn

Zoea-Nova / commit 2a0db99

fix: prevent WaitGroup negative counter panic on restart

Critical bug: WaitGroup counter goes negative when restarting errored
myses, causing panic: 'sync: negative WaitGroup counter'

Root Cause:
1. StartMysis() calls wg.Add(1)
2. mysis.Start() detects errored state with old goroutine
3. Cancels old context, old goroutine exits and calls wg.Done()
4. NEW goroutine starts and will call wg.Done() when it exits
5. Result: ONE Add(1), TWO Done() calls → negative counter → PANIC

Solution:
- Move wg.Add(1) from StartMysis() to mysis.Start()
- Add(1) called AFTER old goroutine cleanup completes
- Add(1) called immediately before launching new goroutine
- Ensures 1:1 Add/Done pairing

Sequence when restarting errored mysis:
1. Start() detects errored state
2. Cancels old context, waits for old goroutine to exit
3. Old goroutine calls wg.Done() (from previous Add)
4. wg.Add(1) called for NEW goroutine
5. New goroutine starts
6. Eventually new goroutine calls wg.Done()
Result: Balanced Add/Done calls

Changes:
- internal/core/commander.go: Remove wg.Add/Done from StartMysis
- internal/core/mysis.go: Add wg.Add(1) before launching goroutine

All tests passing (77.0% core coverage, 86.2% TUI coverage)

xonecas2026-02-06 2a0db995e3948b6776b241f07ef1fdf2ee5f7d37 parent 80fac31

2 files changed

internal/core/commander.go+3−6
@@ -189,12 +189,9 @@ func (c *Commander) StartMysis(id string) error {
189189 return err
190190 }
191191
192- c.wg.Add(1) // Track this goroutine
193- if err := mysis.Start(); err != nil {
194- c.wg.Done() // Failed to start, don't track
195- return err
196- }
197- return nil
192+ // Don't increment WaitGroup here - mysis.Start() will do it
193+ // This avoids double-counting when restarting errored myses
194+ return mysis.Start()
198195 }
199196
200197 // StopMysis stops a mysis by ID.
internal/core/mysis.go+5−0
@@ -256,6 +256,11 @@ func (m *Mysis) Start() error {
256256 // Emit state change event
257257 a.emitStateChange(oldState, MysisStateRunning)
258258
259+ // Track goroutine in WaitGroup (only after state change succeeded)
260+ if a.commander != nil {
261+ a.commander.wg.Add(1)
262+ }
263+
259264 // Start the processing goroutine (only after all setup succeeded)
260265 go a.run(ctx)