fix: add panic recovery to core-path goroutines
Add defer-recover to 11 goroutines across 4 files to prevent ungoroutine panics from crashing the entire process: - pkg/tools/toolloop.go: parallel tool execution - pkg/channels/manager.go: HTTP server (x2), channel registration - pkg/events/subscription.go: concurrent dispatch, timeout handler, watchContext - pkg/tools/shell.go: cmd.Wait, PTY cmd.Wait, PTY read, pipe read Key design decisions: - Recover handlers send fallback values to channels (shell done, subscription done) to prevent deadlocks when the producer panics - PTY cmd.Wait sets session.Status='error' on panic for consistency - toolloop sets ErrorResult on panic so the LLM gets a meaningful response instead of a nil result - subscription.go uses log.Printf to match existing invokeHandler style - Other files use project logger (ErrorCF) with stack traces Refs: FIX-PLAN-0.3.0 #2
This commit is contained in:
1 parent
c1ff5aa6f4
commit
b292defd95
4 files changed
+100
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -417,6 +418,16 @@ func (t *ExecTool) runSync(ctx context.Context, command, cwd string) *ToolResult
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.ErrorCF("shell", "cmd.Wait goroutine panic recovered",
|
||||
map[string]any{
|
||||
"panic": fmt.Sprintf("%v", r),
|
||||
"stack": string(debug.Stack()),
|
||||
})
|
||||
done <- fmt.Errorf("panic in cmd.Wait: %v", r)
|
||||
}
|
||||
}()
|
||||
done <- cmd.Wait()
|
||||
}()
|
||||
|
||||
@@ -573,6 +584,18 @@ func (t *ExecTool) runBackground(ctx context.Context, command, cwd string, ptyEn
|
||||
// so we need cmd.Wait() in a separate goroutine to detect process exit.
|
||||
if session.PTY && session.ptyMaster != nil {
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.ErrorCF("shell", "PTY cmd.Wait goroutine panic recovered",
|
||||
map[string]any{
|
||||
"panic": fmt.Sprintf("%v", r),
|
||||
"stack": string(debug.Stack()),
|
||||
})
|
||||
session.mu.Lock()
|
||||
session.Status = "error"
|
||||
session.mu.Unlock()
|
||||
}
|
||||
}()
|
||||
cmd.Wait() // Wait for process to exit
|
||||
session.mu.Lock()
|
||||
if cmd.ProcessState != nil {
|
||||
@@ -583,6 +606,15 @@ func (t *ExecTool) runBackground(ctx context.Context, command, cwd string, ptyEn
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.ErrorCF("shell", "PTY read goroutine panic recovered",
|
||||
map[string]any{
|
||||
"panic": fmt.Sprintf("%v", r),
|
||||
"stack": string(debug.Stack()),
|
||||
})
|
||||
}
|
||||
}()
|
||||
buf := make([]byte, 4096)
|
||||
for {
|
||||
n, err := session.ptyMaster.Read(buf)
|
||||
@@ -613,6 +645,15 @@ func (t *ExecTool) runBackground(ctx context.Context, command, cwd string, ptyEn
|
||||
// When Read() returns EOF (pipe closed), we break.
|
||||
// When process exits, OS closes pipe write end → Read() returns EOF → we exit.
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.ErrorCF("shell", "pipe read goroutine panic recovered",
|
||||
map[string]any{
|
||||
"panic": fmt.Sprintf("%v", r),
|
||||
"stack": string(debug.Stack()),
|
||||
})
|
||||
}
|
||||
}()
|
||||
buf := make([]byte, 4096)
|
||||
|
||||
// Read stdout
|
||||
|
||||
Reference in new issue
Block a user