Fix double unlock error

This commit is contained in:
2025-07-21 22:25:34 +02:00
parent d05f0af637
commit 4008d18042

View File

@@ -369,9 +369,9 @@ func (i *Instance) monitorProcess() {
err := i.cmd.Wait() err := i.cmd.Wait()
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock()
if !i.Running { if !i.Running {
i.mu.Unlock()
return return
} }
@@ -387,17 +387,20 @@ func (i *Instance) monitorProcess() {
// Log the exit // Log the exit
if err != nil { if err != nil {
log.Printf("Instance %s crashed with error: %v", i.Name, err) log.Printf("Instance %s crashed with error: %v", i.Name, err)
i.attemptRestart() // Handle restart while holding the lock, then release it
i.handleRestart()
} else { } else {
log.Printf("Instance %s exited cleanly", i.Name) log.Printf("Instance %s exited cleanly", i.Name)
i.mu.Unlock()
} }
} }
// attemptRestart handles the auto-restart logic with safety checks // handleRestart manages the restart process while holding the lock
func (i *Instance) attemptRestart() { func (i *Instance) handleRestart() {
// Validate restart conditions and get safe parameters // Validate restart conditions and get safe parameters
shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions() shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions()
if !shouldRestart { if !shouldRestart {
i.mu.Unlock()
return return
} }
@@ -409,7 +412,7 @@ func (i *Instance) attemptRestart() {
restartCtx, cancel := context.WithCancel(context.Background()) restartCtx, cancel := context.WithCancel(context.Background())
i.restartCancel = cancel i.restartCancel = cancel
// Sleep and restart without holding the lock // Release the lock before sleeping
i.mu.Unlock() i.mu.Unlock()
// Use context-aware sleep so it can be cancelled // Use context-aware sleep so it can be cancelled