panic handlers everywhere (#1327)

This commit is contained in:
Mike Sawka 2024-11-20 18:05:13 -08:00 committed by GitHub
parent 812732673c
commit 360964d4ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 173 additions and 105 deletions

View File

@ -9,7 +9,6 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"runtime/debug"
"runtime" "runtime"
"sync" "sync"
@ -19,6 +18,7 @@ import (
"github.com/wavetermdev/waveterm/pkg/authkey" "github.com/wavetermdev/waveterm/pkg/authkey"
"github.com/wavetermdev/waveterm/pkg/blockcontroller" "github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/filestore" "github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/remote/conncontroller" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller"
"github.com/wavetermdev/waveterm/pkg/service" "github.com/wavetermdev/waveterm/pkg/service"
"github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/telemetry"
@ -111,15 +111,16 @@ func telemetryLoop() {
} }
} }
func sendTelemetryWrapper() { func panicTelemetryHandler() {
defer func() { activity := telemetry.ActivityUpdate{NumPanics: 1}
r := recover() err := telemetry.UpdateActivity(context.Background(), activity)
if r == nil { if err != nil {
return log.Printf("error updating activity (panicTelemetryHandler): %v\n", err)
} }
log.Printf("[error] in sendTelemetryWrapper: %v\n", r) }
debug.PrintStack()
}() func sendTelemetryWrapper() {
defer panichandler.PanicHandler("sendTelemetryWrapper")
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn() defer cancelFn()
beforeSendActivityUpdate(ctx) beforeSendActivityUpdate(ctx)
@ -254,7 +255,9 @@ func main() {
log.Printf("error initializing wstore: %v\n", err) log.Printf("error initializing wstore: %v\n", err)
return return
} }
panichandler.PanicTelemetryHandler = panicTelemetryHandler
go func() { go func() {
defer panichandler.PanicHandler("InitCustomShellStartupFiles")
err := shellutil.InitCustomShellStartupFiles() err := shellutil.InitCustomShellStartupFiles()
if err != nil { if err != nil {
log.Printf("error initializing wsh and shell-integration files: %v\n", err) log.Printf("error initializing wsh and shell-integration files: %v\n", err)

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/packetparser" "github.com/wavetermdev/waveterm/pkg/util/packetparser"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
@ -53,6 +54,7 @@ func handleNewListenerConn(conn net.Conn, router *wshutil.WshRouter) {
var routeIdContainer atomic.Pointer[string] var routeIdContainer atomic.Pointer[string]
proxy := wshutil.MakeRpcProxy() proxy := wshutil.MakeRpcProxy()
go func() { go func() {
defer panichandler.PanicHandler("handleNewListenerConn:AdaptOutputChToStream")
writeErr := wshutil.AdaptOutputChToStream(proxy.ToRemoteCh, conn) writeErr := wshutil.AdaptOutputChToStream(proxy.ToRemoteCh, conn)
if writeErr != nil { if writeErr != nil {
log.Printf("error writing to domain socket: %v\n", writeErr) log.Printf("error writing to domain socket: %v\n", writeErr)
@ -60,6 +62,7 @@ func handleNewListenerConn(conn net.Conn, router *wshutil.WshRouter) {
}() }()
go func() { go func() {
// when input is closed, close the connection // when input is closed, close the connection
defer panichandler.PanicHandler("handleNewListenerConn:AdaptStreamToMsgCh")
defer func() { defer func() {
conn.Close() conn.Close()
routeIdPtr := routeIdContainer.Load() routeIdPtr := routeIdContainer.Load()
@ -136,6 +139,7 @@ func serverRunRouter() error {
rawCh := make(chan []byte, wshutil.DefaultOutputChSize) rawCh := make(chan []byte, wshutil.DefaultOutputChSize)
go packetparser.Parse(os.Stdin, termProxy.FromRemoteCh, rawCh) go packetparser.Parse(os.Stdin, termProxy.FromRemoteCh, rawCh)
go func() { go func() {
defer panichandler.PanicHandler("serverRunRouter:WritePackets")
for msg := range termProxy.ToRemoteCh { for msg := range termProxy.ToRemoteCh {
packetparser.WritePacket(os.Stdout, msg) packetparser.WritePacket(os.Stdout, msg)
} }

View File

@ -16,6 +16,7 @@ import (
"time" "time"
"github.com/wavetermdev/waveterm/pkg/filestore" "github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/remote" "github.com/wavetermdev/waveterm/pkg/remote"
"github.com/wavetermdev/waveterm/pkg/remote/conncontroller" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller"
"github.com/wavetermdev/waveterm/pkg/shellexec" "github.com/wavetermdev/waveterm/pkg/shellexec"
@ -354,6 +355,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
ptyBuffer := wshutil.MakePtyBuffer(wshutil.WaveOSCPrefix, bc.ShellProc.Cmd, wshProxy.FromRemoteCh) ptyBuffer := wshutil.MakePtyBuffer(wshutil.WaveOSCPrefix, bc.ShellProc.Cmd, wshProxy.FromRemoteCh)
go func() { go func() {
// handles regular output from the pty (goes to the blockfile and xterm) // handles regular output from the pty (goes to the blockfile and xterm)
defer panichandler.PanicHandler("blockcontroller:shellproc-pty-read-loop")
defer func() { defer func() {
log.Printf("[shellproc] pty-read loop done\n") log.Printf("[shellproc] pty-read loop done\n")
bc.ShellProc.Close() bc.ShellProc.Close()
@ -386,6 +388,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
go func() { go func() {
// handles input from the shellInputCh, sent to pty // handles input from the shellInputCh, sent to pty
// use shellInputCh instead of bc.ShellInputCh (because we want to be attached to *this* ch. bc.ShellInputCh can be updated) // use shellInputCh instead of bc.ShellInputCh (because we want to be attached to *this* ch. bc.ShellInputCh can be updated)
defer panichandler.PanicHandler("blockcontroller:shellproc-input-loop")
for ic := range shellInputCh { for ic := range shellInputCh {
if len(ic.InputData) > 0 { if len(ic.InputData) > 0 {
bc.ShellProc.Cmd.Write(ic.InputData) bc.ShellProc.Cmd.Write(ic.InputData)
@ -403,6 +406,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
} }
}() }()
go func() { go func() {
defer panichandler.PanicHandler("blockcontroller:shellproc-output-loop")
// handles outputCh -> shellInputCh // handles outputCh -> shellInputCh
for msg := range wshProxy.ToRemoteCh { for msg := range wshProxy.ToRemoteCh {
encodedMsg, err := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg) encodedMsg, err := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
@ -413,6 +417,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
} }
}() }()
go func() { go func() {
defer panichandler.PanicHandler("blockcontroller:shellproc-wait-loop")
// wait for the shell to finish // wait for the shell to finish
defer func() { defer func() {
wshutil.DefaultRouter.UnregisterRoute(wshutil.MakeControllerRouteId(bc.BlockId)) wshutil.DefaultRouter.UnregisterRoute(wshutil.MakeControllerRouteId(bc.BlockId))
@ -484,6 +489,7 @@ func (bc *BlockController) run(bdata *waveobj.Block, blockMeta map[string]any, r
runOnStart := getBoolFromMeta(blockMeta, waveobj.MetaKey_CmdRunOnStart, true) runOnStart := getBoolFromMeta(blockMeta, waveobj.MetaKey_CmdRunOnStart, true)
if runOnStart { if runOnStart {
go func() { go func() {
defer panichandler.PanicHandler("blockcontroller:run-shell-command")
var termSize waveobj.TermSize var termSize waveobj.TermSize
if rtOpts != nil { if rtOpts != nil {
termSize = rtOpts.TermSize termSize = rtOpts.TermSize

View File

@ -12,12 +12,12 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
"runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/wavetermdev/waveterm/pkg/ijson" "github.com/wavetermdev/waveterm/pkg/ijson"
"github.com/wavetermdev/waveterm/pkg/panichandler"
) )
const ( const (
@ -514,12 +514,7 @@ func (s *FileStore) runFlushWithNewContext() (FlushStats, error) {
} }
func (s *FileStore) runFlusher() { func (s *FileStore) runFlusher() {
defer func() { defer panichandler.PanicHandler("filestore flusher")
if r := recover(); r != nil {
log.Printf("panic in filestore flusher: %v\n", r)
debug.PrintStack()
}
}()
for { for {
stats, err := s.runFlushWithNewContext() stats, err := s.runFlushWithNewContext()
if err != nil || stats.NumDirtyEntries > 0 { if err != nil || stats.NumDirtyEntries > 0 {

View File

@ -0,0 +1,43 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package panichandler
import (
"fmt"
"log"
"runtime/debug"
)
// to log NumPanics into the local telemetry system
// gets around import cycles
var PanicTelemetryHandler func()
func PanicHandlerNoTelemetry(debugStr string) {
r := recover()
if r == nil {
return
}
log.Printf("[panic] in %s: %v\n", debugStr, r)
debug.PrintStack()
}
// returns an error (wrapping the panic) if a panic occurred
func PanicHandler(debugStr string) error {
r := recover()
if r == nil {
return nil
}
log.Printf("[panic] in %s: %v\n", debugStr, r)
debug.PrintStack()
if PanicTelemetryHandler != nil {
go func() {
defer PanicHandlerNoTelemetry("PanicTelemetryHandler")
PanicTelemetryHandler()
}()
}
if err, ok := r.(error); ok {
return fmt.Errorf("panic in %s: %w", debugStr, err)
}
return fmt.Errorf("panic in %s: %v", debugStr, r)
}

View File

@ -20,6 +20,7 @@ import (
"github.com/kevinburke/ssh_config" "github.com/kevinburke/ssh_config"
"github.com/skeema/knownhosts" "github.com/skeema/knownhosts"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/remote" "github.com/wavetermdev/waveterm/pkg/remote"
"github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/telemetry"
"github.com/wavetermdev/waveterm/pkg/userinput" "github.com/wavetermdev/waveterm/pkg/userinput"
@ -193,6 +194,7 @@ func (conn *SSHConn) OpenDomainSocketListener() error {
conn.DomainSockListener = listener conn.DomainSockListener = listener
}) })
go func() { go func() {
defer panichandler.PanicHandler("conncontroller:OpenDomainSocketListener")
defer conn.WithLock(func() { defer conn.WithLock(func() {
conn.DomainSockListener = nil conn.DomainSockListener = nil
conn.SockName = "" conn.SockName = ""
@ -252,6 +254,7 @@ func (conn *SSHConn) StartConnServer() error {
}) })
// service the I/O // service the I/O
go func() { go func() {
defer panichandler.PanicHandler("conncontroller:sshSession.Wait")
// wait for termination, clear the controller // wait for termination, clear the controller
defer conn.WithLock(func() { defer conn.WithLock(func() {
conn.ConnController = nil conn.ConnController = nil
@ -260,6 +263,7 @@ func (conn *SSHConn) StartConnServer() error {
log.Printf("conn controller (%q) terminated: %v", conn.GetName(), waitErr) log.Printf("conn controller (%q) terminated: %v", conn.GetName(), waitErr)
}() }()
go func() { go func() {
defer panichandler.PanicHandler("conncontroller:sshSession-output")
readErr := wshutil.StreamToLines(pipeRead, func(line []byte) { readErr := wshutil.StreamToLines(pipeRead, func(line []byte) {
lineStr := string(line) lineStr := string(line)
if !strings.HasSuffix(lineStr, "\n") { if !strings.HasSuffix(lineStr, "\n") {

View File

@ -16,6 +16,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
@ -288,6 +289,7 @@ func CpHostToRemote(client *ssh.Client, sourcePath string, destPath string) erro
} }
go func() { go func() {
defer panichandler.PanicHandler("connutil:CpHostToRemote")
io.Copy(installStdin, input) io.Copy(installStdin, input)
session.Close() // this allows the command to complete for reasons i don't fully understand session.Close() // this allows the command to complete for reasons i don't fully understand
}() }()

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta" "github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wcore" "github.com/wavetermdev/waveterm/pkg/wcore"
@ -94,6 +95,7 @@ func (svc *ObjectService) AddTabToWorkspace(windowId string, tabName string, act
} }
updates := waveobj.ContextGetUpdatesRtn(ctx) updates := waveobj.ContextGetUpdatesRtn(ctx)
go func() { go func() {
defer panichandler.PanicHandler("ObjectService:AddTabToWorkspace:SendUpdateEvents")
wps.Broker.SendUpdateEvents(updates) wps.Broker.SendUpdateEvents(updates)
}() }()
return tabId, updates, nil return tabId, updates, nil
@ -142,6 +144,7 @@ func (svc *ObjectService) SetActiveTab(windowId string, tabId string) (waveobj.U
} }
updates := waveobj.ContextGetUpdatesRtn(ctx) updates := waveobj.ContextGetUpdatesRtn(ctx)
go func() { go func() {
defer panichandler.PanicHandler("ObjectService:SetActiveTab:SendUpdateEvents")
wps.Broker.SendUpdateEvents(updates) wps.Broker.SendUpdateEvents(updates)
}() }()
var extraUpdates waveobj.UpdatesRtnType var extraUpdates waveobj.UpdatesRtnType

View File

@ -11,6 +11,7 @@ import (
"github.com/wavetermdev/waveterm/pkg/blockcontroller" "github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/eventbus" "github.com/wavetermdev/waveterm/pkg/eventbus"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta" "github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/waveobj"
@ -75,6 +76,7 @@ func (svc *WindowService) CloseTab(ctx context.Context, windowId string, tabId s
} }
} }
go func() { go func() {
defer panichandler.PanicHandler("WindowService:CloseTab:StopBlockControllers")
for _, blockId := range tab.BlockIds { for _, blockId := range tab.BlockIds {
blockcontroller.StopBlockController(blockId) blockcontroller.StopBlockController(blockId)
} }
@ -107,6 +109,7 @@ func (svc *WindowService) CloseTab(ctx context.Context, windowId string, tabId s
} }
updates := waveobj.ContextGetUpdatesRtn(ctx) updates := waveobj.ContextGetUpdatesRtn(ctx)
go func() { go func() {
defer panichandler.PanicHandler("WindowService:CloseTab:SendUpdateEvents")
wps.Broker.SendUpdateEvents(updates) wps.Broker.SendUpdateEvents(updates)
}() }()
return rtn, updates, nil return rtn, updates, nil

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/creack/pty" "github.com/creack/pty"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wsl" "github.com/wavetermdev/waveterm/pkg/wsl"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
@ -51,6 +52,7 @@ func (cw CmdWrap) KillGraceful(timeout time.Duration) {
cw.Cmd.Process.Signal(syscall.SIGTERM) cw.Cmd.Process.Signal(syscall.SIGTERM)
} }
go func() { go func() {
defer panichandler.PanicHandler("KillGraceful:Kill")
time.Sleep(timeout) time.Sleep(timeout)
if cw.Cmd.ProcessState == nil || !cw.Cmd.ProcessState.Exited() { if cw.Cmd.ProcessState == nil || !cw.Cmd.ProcessState.Exited() {
cw.Cmd.Process.Kill() // force kill if it is already not exited cw.Cmd.Process.Kill() // force kill if it is already not exited
@ -159,6 +161,7 @@ func (wcw WslCmdWrap) KillGraceful(timeout time.Duration) {
} }
process.Signal(os.Interrupt) process.Signal(os.Interrupt)
go func() { go func() {
defer panichandler.PanicHandler("KillGraceful-wsl:Kill")
time.Sleep(timeout) time.Sleep(timeout)
process := wcw.WslCmd.GetProcess() process := wcw.WslCmd.GetProcess()
processState := wcw.WslCmd.GetProcessState() processState := wcw.WslCmd.GetProcessState()

View File

@ -19,6 +19,7 @@ import (
"time" "time"
"github.com/creack/pty" "github.com/creack/pty"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/remote" "github.com/wavetermdev/waveterm/pkg/remote"
"github.com/wavetermdev/waveterm/pkg/remote/conncontroller" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller"
"github.com/wavetermdev/waveterm/pkg/util/shellutil" "github.com/wavetermdev/waveterm/pkg/util/shellutil"
@ -51,6 +52,7 @@ type ShellProc struct {
func (sp *ShellProc) Close() { func (sp *ShellProc) Close() {
sp.Cmd.KillGraceful(DefaultGracefulKillWait) sp.Cmd.KillGraceful(DefaultGracefulKillWait)
go func() { go func() {
defer panichandler.PanicHandler("ShellProc.Close")
waitErr := sp.Cmd.Wait() waitErr := sp.Cmd.Wait()
sp.SetWaitErrorAndSignalDone(waitErr) sp.SetWaitErrorAndSignalDone(waitErr)
@ -450,6 +452,7 @@ func RunSimpleCmdInPty(ecmd *exec.Cmd, termSize waveobj.TermSize) ([]byte, error
ioDone := make(chan bool) ioDone := make(chan bool)
var outputBuf bytes.Buffer var outputBuf bytes.Buffer
go func() { go func() {
panichandler.PanicHandler("RunSimpleCmdInPty:ioCopy")
// ignore error (/dev/ptmx has read error when process is done) // ignore error (/dev/ptmx has read error when process is done)
defer close(ioDone) defer close(ioDone)
io.Copy(&outputBuf, cmdPty) io.Copy(&outputBuf, cmdPty)

View File

@ -9,6 +9,7 @@ import (
"log" "log"
"time" "time"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/daystr" "github.com/wavetermdev/waveterm/pkg/util/daystr"
"github.com/wavetermdev/waveterm/pkg/util/dbutil" "github.com/wavetermdev/waveterm/pkg/util/dbutil"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
@ -36,6 +37,7 @@ type ActivityUpdate struct {
NumSSHConn int `json:"numsshconn,omitempty"` NumSSHConn int `json:"numsshconn,omitempty"`
NumWSLConn int `json:"numwslconn,omitempty"` NumWSLConn int `json:"numwslconn,omitempty"`
NumMagnify int `json:"nummagnify,omitempty"` NumMagnify int `json:"nummagnify,omitempty"`
NumPanics int `json:"numpanics,omitempty"`
Startup int `json:"startup,omitempty"` Startup int `json:"startup,omitempty"`
Shutdown int `json:"shutdown,omitempty"` Shutdown int `json:"shutdown,omitempty"`
SetTabTheme int `json:"settabtheme,omitempty"` SetTabTheme int `json:"settabtheme,omitempty"`
@ -71,6 +73,7 @@ type TelemetryData struct {
NewTab int `json:"newtab"` NewTab int `json:"newtab"`
NumStartup int `json:"numstartup,omitempty"` NumStartup int `json:"numstartup,omitempty"`
NumShutdown int `json:"numshutdown,omitempty"` NumShutdown int `json:"numshutdown,omitempty"`
NumPanics int `json:"numpanics,omitempty"`
SetTabTheme int `json:"settabtheme,omitempty"` SetTabTheme int `json:"settabtheme,omitempty"`
Displays []ActivityDisplayType `json:"displays,omitempty"` Displays []ActivityDisplayType `json:"displays,omitempty"`
Renderers map[string]int `json:"renderers,omitempty"` Renderers map[string]int `json:"renderers,omitempty"`
@ -104,6 +107,7 @@ func AutoUpdateChannel() string {
// Wraps UpdateCurrentActivity, spawns goroutine, and logs errors // Wraps UpdateCurrentActivity, spawns goroutine, and logs errors
func GoUpdateActivityWrap(update ActivityUpdate, debugStr string) { func GoUpdateActivityWrap(update ActivityUpdate, debugStr string) {
go func() { go func() {
defer panichandler.PanicHandlerNoTelemetry("GoUpdateActivityWrap")
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn() defer cancelFn()
err := UpdateActivity(ctx, update) err := UpdateActivity(ctx, update)
@ -138,6 +142,7 @@ func UpdateActivity(ctx context.Context, update ActivityUpdate) error {
tdata.NumShutdown += update.Shutdown tdata.NumShutdown += update.Shutdown
tdata.SetTabTheme += update.SetTabTheme tdata.SetTabTheme += update.SetTabTheme
tdata.NumMagnify += update.NumMagnify tdata.NumMagnify += update.NumMagnify
tdata.NumPanics += update.NumPanics
if update.NumTabs > 0 { if update.NumTabs > 0 {
tdata.NumTabs = update.NumTabs tdata.NumTabs = update.NumTabs
} }

View File

@ -10,11 +10,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"runtime/debug"
"strings" "strings"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -115,17 +114,10 @@ func (AnthropicBackend) StreamCompletion(ctx context.Context, request wshrpc.Ope
go func() { go func() {
defer func() { defer func() {
if r := recover(); r != nil { panicErr := panichandler.PanicHandler("AnthropicBackend.StreamCompletion")
// Convert panic to error and send it if panicErr != nil {
log.Printf("panic: %v\n", r) rtn <- makeAIError(panicErr)
debug.PrintStack()
err, ok := r.(error)
if !ok {
err = fmt.Errorf("anthropic backend panic: %v", r)
} }
rtn <- makeAIError(err)
}
// Always close the channel
close(rtn) close(rtn)
}() }()

View File

@ -11,6 +11,7 @@ import (
"log" "log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wcloud" "github.com/wavetermdev/waveterm/pkg/wcloud"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -37,7 +38,13 @@ func (WaveAICloudBackend) StreamCompletion(ctx context.Context, request wshrpc.O
rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]) rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType])
wsEndpoint := wcloud.GetWSEndpoint() wsEndpoint := wcloud.GetWSEndpoint()
go func() { go func() {
defer close(rtn) defer func() {
panicErr := panichandler.PanicHandler("WaveAICloudBackend.StreamCompletion")
if panicErr != nil {
rtn <- makeAIError(panicErr)
}
close(rtn)
}()
if wsEndpoint == "" { if wsEndpoint == "" {
rtn <- makeAIError(fmt.Errorf("no cloud ws endpoint found")) rtn <- makeAIError(fmt.Errorf("no cloud ws endpoint found"))
return return

View File

@ -8,12 +8,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"regexp" "regexp"
"runtime/debug"
"strings" "strings"
openaiapi "github.com/sashabaranov/go-openai" openaiapi "github.com/sashabaranov/go-openai"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -75,17 +74,10 @@ func (OpenAIBackend) StreamCompletion(ctx context.Context, request wshrpc.OpenAi
rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]) rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType])
go func() { go func() {
defer func() { defer func() {
if r := recover(); r != nil { panicErr := panichandler.PanicHandler("OpenAIBackend.StreamCompletion")
// Convert panic to error and send it if panicErr != nil {
log.Printf("panic: %v\n", r) rtn <- makeAIError(panicErr)
debug.PrintStack()
err, ok := r.(error)
if !ok {
err = fmt.Errorf("openai backend panic: %v", r)
} }
rtn <- makeAIError(err)
}
// Always close the channel
close(rtn) close(rtn)
}() }()
if request.Opts == nil { if request.Opts == nil {

View File

@ -10,6 +10,7 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/vdom" "github.com/wavetermdev/waveterm/pkg/vdom"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -23,12 +24,11 @@ func (*WaveAppServerImpl) WshServerImpl() {}
func (impl *WaveAppServerImpl) VDomRenderCommand(ctx context.Context, feUpdate vdom.VDomFrontendUpdate) chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate] { func (impl *WaveAppServerImpl) VDomRenderCommand(ctx context.Context, feUpdate vdom.VDomFrontendUpdate) chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate] {
respChan := make(chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate], 5) respChan := make(chan wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate], 5)
defer func() { defer func() {
if r := recover(); r != nil { panicErr := panichandler.PanicHandler("VDomRenderCommand")
log.Printf("panic in VDomRenderCommand: %v\n", r) if panicErr != nil {
respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{ respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{
Error: fmt.Errorf("internal error: %v", r), Error: panicErr,
} }
close(respChan) close(respChan)
} }
@ -88,6 +88,7 @@ func (impl *WaveAppServerImpl) VDomRenderCommand(ctx context.Context, feUpdate v
// Split the update into chunks and send them sequentially // Split the update into chunks and send them sequentially
updates := vdom.SplitBackendUpdate(update) updates := vdom.SplitBackendUpdate(update)
go func() { go func() {
defer panichandler.PanicHandler("VDomRenderCommand:splitUpdates")
defer close(respChan) defer close(respChan)
for _, splitUpdate := range updates { for _, splitUpdate := range updates {
respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{ respChan <- wshrpc.RespOrErrorUnion[*vdom.VDomBackendUpdate]{
@ -108,9 +109,10 @@ func (impl *WaveAppServerImpl) VDomUrlRequestCommand(ctx context.Context, data w
defer writer.Close() // Ensures writer is closed before the channel is closed defer writer.Close() // Ensures writer is closed before the channel is closed
defer func() { defer func() {
if r := recover(); r != nil { panicErr := panichandler.PanicHandler("VDomUrlRequestCommand")
if panicErr != nil {
writer.WriteHeader(http.StatusInternalServerError) writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte(fmt.Sprintf("internal server error: %v", r))) writer.Write([]byte(fmt.Sprintf("internal server error: %v", panicErr)))
} }
}() }()

View File

@ -10,6 +10,7 @@ import (
"sync" "sync"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/wps" "github.com/wavetermdev/waveterm/pkg/wps"
) )
@ -64,6 +65,7 @@ func (w *Watcher) Start() {
w.sendInitialValues() w.sendInitialValues()
go func() { go func() {
defer panichandler.PanicHandler("filewatcher:Start")
for { for {
select { select {
case event, ok := <-w.watcher.Events: case event, ok := <-w.watcher.Events:

View File

@ -12,6 +12,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/blockcontroller" "github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/telemetry"
"github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wps" "github.com/wavetermdev/waveterm/pkg/wps"
@ -267,6 +268,7 @@ func CreateBlock(ctx context.Context, tabId string, blockDef *waveobj.BlockDef,
return nil, fmt.Errorf("error creating block: %w", err) return nil, fmt.Errorf("error creating block: %w", err)
} }
go func() { go func() {
defer panichandler.PanicHandler("CreateBlock:telemetry")
blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "") blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "")
if blockView == "" { if blockView == "" {
return return

View File

@ -15,7 +15,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug"
"strconv" "strconv"
"time" "time"
@ -25,6 +24,7 @@ import (
"github.com/wavetermdev/waveterm/pkg/authkey" "github.com/wavetermdev/waveterm/pkg/authkey"
"github.com/wavetermdev/waveterm/pkg/docsite" "github.com/wavetermdev/waveterm/pkg/docsite"
"github.com/wavetermdev/waveterm/pkg/filestore" "github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/service" "github.com/wavetermdev/waveterm/pkg/service"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
@ -358,21 +358,18 @@ type ClientActiveState struct {
func WebFnWrap(opts WebFnOpts, fn WebFnType) WebFnType { func WebFnWrap(opts WebFnOpts, fn WebFnType) WebFnType {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
recErr := recover() recErr := panichandler.PanicHandler("WebFnWrap")
if recErr == nil { if recErr == nil {
return return
} }
panicStr := fmt.Sprintf("panic: %v", recErr)
log.Printf("panic: %v\n", recErr)
debug.PrintStack()
if opts.JsonErrors { if opts.JsonErrors {
jsonRtn := marshalReturnValue(nil, fmt.Errorf(panicStr)) jsonRtn := marshalReturnValue(nil, recErr)
w.Header().Set(ContentTypeHeaderKey, ContentTypeJson) w.Header().Set(ContentTypeHeaderKey, ContentTypeJson)
w.Header().Set(ContentLengthHeaderKey, fmt.Sprintf("%d", len(jsonRtn))) w.Header().Set(ContentLengthHeaderKey, fmt.Sprintf("%d", len(jsonRtn)))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(jsonRtn) w.Write(jsonRtn)
} else { } else {
http.Error(w, panicStr, http.StatusInternalServerError) http.Error(w, recErr.Error(), http.StatusInternalServerError)
} }
}() }()
if !opts.AllowCaching { if !opts.AllowCaching {

View File

@ -9,7 +9,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"runtime/debug"
"sync" "sync"
"time" "time"
@ -18,6 +17,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/wavetermdev/waveterm/pkg/authkey" "github.com/wavetermdev/waveterm/pkg/authkey"
"github.com/wavetermdev/waveterm/pkg/eventbus" "github.com/wavetermdev/waveterm/pkg/eventbus"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/web/webcmd" "github.com/wavetermdev/waveterm/pkg/web/webcmd"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
"github.com/wavetermdev/waveterm/pkg/wshutil" "github.com/wavetermdev/waveterm/pkg/wshutil"
@ -81,11 +81,9 @@ func getStringFromMap(jmsg map[string]any, key string) string {
func processWSCommand(jmsg map[string]any, outputCh chan any, rpcInputCh chan []byte) { func processWSCommand(jmsg map[string]any, outputCh chan any, rpcInputCh chan []byte) {
var rtnErr error var rtnErr error
defer func() { defer func() {
r := recover() panicErr := panichandler.PanicHandler("processWSCommand")
if r != nil { if panicErr != nil {
rtnErr = fmt.Errorf("panic: %v", r) rtnErr = panicErr
log.Printf("[websocket] panic in processMessage: %v\n", r)
debug.PrintStack()
} }
if rtnErr == nil { if rtnErr == nil {
return return
@ -304,6 +302,7 @@ func HandleWsInternal(w http.ResponseWriter, r *http.Request) error {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)
go func() { go func() {
defer panichandler.PanicHandler("HandleWsInternal:outputCh")
// no waitgroup add here // no waitgroup add here
// move values from rpcOutputCh to outputCh // move values from rpcOutputCh to outputCh
for msgBytes := range wproxy.ToRemoteCh { for msgBytes := range wproxy.ToRemoteCh {
@ -315,11 +314,13 @@ func HandleWsInternal(w http.ResponseWriter, r *http.Request) error {
} }
}() }()
go func() { go func() {
defer panichandler.PanicHandler("HandleWsInternal:ReadLoop")
// read loop // read loop
defer wg.Done() defer wg.Done()
ReadLoop(conn, outputCh, closeCh, wproxy.FromRemoteCh, routeId) ReadLoop(conn, outputCh, closeCh, wproxy.FromRemoteCh, routeId)
}() }()
go func() { go func() {
defer panichandler.PanicHandler("HandleWsInternal:WriteLoop")
// write loop // write loop
defer wg.Done() defer wg.Done()
WriteLoop(conn, outputCh, closeCh, routeId) WriteLoop(conn, outputCh, closeCh, routeId)

View File

@ -6,6 +6,7 @@ package wshclient
import ( import (
"errors" "errors"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
"github.com/wavetermdev/waveterm/pkg/wshutil" "github.com/wavetermdev/waveterm/pkg/wshutil"
@ -39,6 +40,7 @@ func sendRpcRequestCallHelper[T any](w *wshutil.WshRpc, command string, data int
func rtnErr[T any](ch chan wshrpc.RespOrErrorUnion[T], err error) { func rtnErr[T any](ch chan wshrpc.RespOrErrorUnion[T], err error) {
go func() { go func() {
defer panichandler.PanicHandler("wshclientutil:rtnErr")
ch <- wshrpc.RespOrErrorUnion[T]{Error: err} ch <- wshrpc.RespOrErrorUnion[T]{Error: err}
close(ch) close(ch)
}() }()
@ -63,6 +65,7 @@ func sendRpcRequestResponseStreamHelper[T any](w *wshutil.WshRpc, command string
reqHandler.SendCancel() reqHandler.SendCancel()
} }
go func() { go func() {
defer panichandler.PanicHandler("sendRpcRequestResponseStreamHelper")
defer close(respChan) defer close(respChan)
for { for {
if reqHandler.ResponseDone() { if reqHandler.ResponseDone() {

View File

@ -18,6 +18,7 @@ import (
"github.com/wavetermdev/waveterm/pkg/blockcontroller" "github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/filestore" "github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/remote" "github.com/wavetermdev/waveterm/pkg/remote"
"github.com/wavetermdev/waveterm/pkg/remote/conncontroller" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller"
"github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/telemetry"
@ -45,11 +46,7 @@ func (*WshServer) WshServerImpl() {}
var WshServerImpl = WshServer{} var WshServerImpl = WshServer{}
func (ws *WshServer) TestCommand(ctx context.Context, data string) error { func (ws *WshServer) TestCommand(ctx context.Context, data string) error {
defer func() { defer panichandler.PanicHandler("TestCommand")
if r := recover(); r != nil {
log.Printf("panic in TestCommand: %v", r)
}
}()
rpcSource := wshutil.GetRpcSourceFromContext(ctx) rpcSource := wshutil.GetRpcSourceFromContext(ctx)
log.Printf("TEST src:%s | %s\n", rpcSource, data) log.Printf("TEST src:%s | %s\n", rpcSource, data)
return nil return nil
@ -65,6 +62,7 @@ func (ws *WshServer) MessageCommand(ctx context.Context, data wshrpc.CommandMess
func (ws *WshServer) StreamTestCommand(ctx context.Context) chan wshrpc.RespOrErrorUnion[int] { func (ws *WshServer) StreamTestCommand(ctx context.Context) chan wshrpc.RespOrErrorUnion[int] {
rtn := make(chan wshrpc.RespOrErrorUnion[int]) rtn := make(chan wshrpc.RespOrErrorUnion[int])
go func() { go func() {
defer panichandler.PanicHandler("StreamTestCommand")
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
rtn <- wshrpc.RespOrErrorUnion[int]{Response: i} rtn <- wshrpc.RespOrErrorUnion[int]{Response: i}
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)

View File

@ -8,6 +8,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -131,6 +132,7 @@ func serverImplAdapter(impl any) func(*RpcResponseHandler) bool {
return true return true
} }
go func() { go func() {
defer panichandler.PanicHandler("serverImplAdapter:responseStream")
defer handler.Finalize() defer handler.Finalize()
// must use reflection here because we don't know the generic type of RespOrErrorUnion // must use reflection here because we don't know the generic type of RespOrErrorUnion
for { for {

View File

@ -9,6 +9,7 @@ import (
"sync" "sync"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
) )
@ -112,6 +113,7 @@ func (p *WshRpcMultiProxy) handleUnauthMessage(msgBytes []byte) {
p.setRouteInfo(routeInfo.AuthToken, routeInfo) p.setRouteInfo(routeInfo.AuthToken, routeInfo)
p.sendAuthResponse(msg, routeId, routeInfo.AuthToken) p.sendAuthResponse(msg, routeId, routeInfo.AuthToken)
go func() { go func() {
defer panichandler.PanicHandler("WshRpcMultiProxy:handleUnauthMessage")
for msgBytes := range routeInfo.Proxy.ToRemoteCh { for msgBytes := range routeInfo.Proxy.ToRemoteCh {
p.ToRemoteCh <- msgBytes p.ToRemoteCh <- msgBytes
} }

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wps" "github.com/wavetermdev/waveterm/pkg/wps"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
@ -91,6 +92,7 @@ func noRouteErr(routeId string) error {
} }
func (router *WshRouter) SendEvent(routeId string, event wps.WaveEvent) { func (router *WshRouter) SendEvent(routeId string, event wps.WaveEvent) {
defer panichandler.PanicHandler("WshRouter.SendEvent")
rpc := router.GetRpc(routeId) rpc := router.GetRpc(routeId)
if rpc == nil { if rpc == nil {
return return
@ -296,6 +298,7 @@ func (router *WshRouter) RegisterRoute(routeId string, rpc AbstractRpcClient, sh
} }
router.RouteMap[routeId] = rpc router.RouteMap[routeId] = rpc
go func() { go func() {
defer panichandler.PanicHandler("WshRouter:registerRoute:recvloop")
// announce // announce
if shouldAnnounce && !alreadyExists && router.GetUpstreamClient() != nil { if shouldAnnounce && !alreadyExists && router.GetUpstreamClient() != nil {
announceMsg := RpcMessage{Command: wshrpc.Command_RouteAnnounce, Source: routeId} announceMsg := RpcMessage{Command: wshrpc.Command_RouteAnnounce, Source: routeId}
@ -341,6 +344,7 @@ func (router *WshRouter) UnregisterRoute(routeId string) {
} }
} }
go func() { go func() {
defer panichandler.PanicHandler("WshRouter:unregisterRoute:routegone")
wps.Broker.UnsubscribeAll(routeId) wps.Broker.UnsubscribeAll(routeId)
wps.Broker.Publish(wps.WaveEvent{Event: wps.Event_RouteGone, Scopes: []string{routeId}}) wps.Broker.Publish(wps.WaveEvent{Event: wps.Event_RouteGone, Scopes: []string{routeId}})
}() }()

View File

@ -10,12 +10,12 @@ import (
"fmt" "fmt"
"log" "log"
"reflect" "reflect"
"runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/wps" "github.com/wavetermdev/waveterm/pkg/wps"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
@ -281,15 +281,6 @@ func (w *WshRpc) handleRequest(req *RpcMessage) {
} }
var respHandler *RpcResponseHandler var respHandler *RpcResponseHandler
defer func() {
if r := recover(); r != nil {
log.Printf("panic in handleRequest: %v\n", r)
debug.PrintStack()
if respHandler != nil {
respHandler.SendResponseError(fmt.Errorf("panic: %v", r))
}
}
}()
timeoutMs := req.Timeout timeoutMs := req.Timeout
if timeoutMs <= 0 { if timeoutMs <= 0 {
timeoutMs = DefaultTimeoutMs timeoutMs = DefaultTimeoutMs
@ -313,13 +304,13 @@ func (w *WshRpc) handleRequest(req *RpcMessage) {
w.registerResponseHandler(req.ReqId, respHandler) w.registerResponseHandler(req.ReqId, respHandler)
isAsync := false isAsync := false
defer func() { defer func() {
if r := recover(); r != nil { panicErr := panichandler.PanicHandler("handleRequest")
log.Printf("panic in handleRequest: %v\n", r) if panicErr != nil {
debug.PrintStack() respHandler.SendResponseError(panicErr)
respHandler.SendResponseError(fmt.Errorf("panic: %v", r))
} }
if isAsync { if isAsync {
go func() { go func() {
defer panichandler.PanicHandler("handleRequest:finalize")
<-ctx.Done() <-ctx.Done()
respHandler.Finalize() respHandler.Finalize()
}() }()
@ -394,6 +385,7 @@ func (w *WshRpc) registerRpc(ctx context.Context, reqId string) chan *RpcMessage
Ctx: ctx, Ctx: ctx,
} }
go func() { go func() {
defer panichandler.PanicHandler("registerRpc:timeout")
<-ctx.Done() <-ctx.Done()
w.unregisterRpc(reqId, fmt.Errorf("EC-TIME: timeout waiting for response")) w.unregisterRpc(reqId, fmt.Errorf("EC-TIME: timeout waiting for response"))
}() }()
@ -463,12 +455,7 @@ func (handler *RpcRequestHandler) Context() context.Context {
} }
func (handler *RpcRequestHandler) SendCancel() { func (handler *RpcRequestHandler) SendCancel() {
defer func() { defer panichandler.PanicHandler("SendCancel")
if r := recover(); r != nil {
// this is likely a write to closed channel
log.Printf("panic in SendCancel: %v\n", r)
}
}()
msg := &RpcMessage{ msg := &RpcMessage{
Cancel: true, Cancel: true,
ReqId: handler.reqId, ReqId: handler.reqId,
@ -573,13 +560,7 @@ func (handler *RpcResponseHandler) SendMessage(msg string) {
} }
func (handler *RpcResponseHandler) SendResponse(data any, done bool) error { func (handler *RpcResponseHandler) SendResponse(data any, done bool) error {
defer func() { defer panichandler.PanicHandler("SendResponse")
if r := recover(); r != nil {
// this is likely a write to closed channel
log.Printf("panic in SendResponse: %v\n", r)
handler.close()
}
}()
if handler.reqId == "" { if handler.reqId == "" {
return nil // no response expected return nil // no response expected
} }
@ -604,13 +585,7 @@ func (handler *RpcResponseHandler) SendResponse(data any, done bool) error {
} }
func (handler *RpcResponseHandler) SendResponseError(err error) { func (handler *RpcResponseHandler) SendResponseError(err error) {
defer func() { defer panichandler.PanicHandler("SendResponseError")
if r := recover(); r != nil {
// this is likely a write to closed channel
log.Printf("panic in SendResponseError: %v\n", r)
handler.close()
}
}()
if handler.reqId == "" || handler.done.Load() { if handler.reqId == "" || handler.done.Load() {
return return
} }
@ -659,12 +634,7 @@ func (w *WshRpc) SendComplexRequest(command string, data any, opts *wshrpc.RpcOp
if timeoutMs <= 0 { if timeoutMs <= 0 {
timeoutMs = DefaultTimeoutMs timeoutMs = DefaultTimeoutMs
} }
defer func() { defer panichandler.PanicHandler("SendComplexRequest")
if r := recover(); r != nil {
log.Printf("panic in SendComplexRequest: %v\n", r)
rtnErr = fmt.Errorf("panic: %v", r)
}
}()
if command == "" { if command == "" {
return nil, fmt.Errorf("command cannot be empty") return nil, fmt.Errorf("command cannot be empty")
} }

View File

@ -19,6 +19,7 @@ import (
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/packetparser" "github.com/wavetermdev/waveterm/pkg/util/packetparser"
"github.com/wavetermdev/waveterm/pkg/wavebase" "github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wshrpc"
@ -150,6 +151,7 @@ func installShutdownSignalHandlers(quiet bool) {
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT) signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
go func() { go func() {
defer panichandler.PanicHandlerNoTelemetry("installShutdownSignalHandlers")
for sig := range sigCh { for sig := range sigCh {
DoShutdown(fmt.Sprintf("got signal %v", sig), 1, quiet) DoShutdown(fmt.Sprintf("got signal %v", sig), 1, quiet)
break break
@ -198,6 +200,7 @@ func SetupTerminalRpcClient(serverImpl ServerImpl) (*WshRpc, io.Reader) {
ptyBuf := MakePtyBuffer(WaveServerOSCPrefix, os.Stdin, messageCh) ptyBuf := MakePtyBuffer(WaveServerOSCPrefix, os.Stdin, messageCh)
rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl) rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl)
go func() { go func() {
defer panichandler.PanicHandler("SetupTerminalRpcClient")
for msg := range outputCh { for msg := range outputCh {
barr, err := EncodeWaveOSCBytes(WaveOSC, msg) barr, err := EncodeWaveOSCBytes(WaveOSC, msg)
if err != nil { if err != nil {
@ -218,6 +221,7 @@ func SetupPacketRpcClient(input io.Reader, output io.Writer, serverImpl ServerIm
rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl) rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl)
go packetparser.Parse(input, messageCh, rawCh) go packetparser.Parse(input, messageCh, rawCh)
go func() { go func() {
defer panichandler.PanicHandler("SetupPacketRpcClient:outputloop")
for msg := range outputCh { for msg := range outputCh {
packetparser.WritePacket(output, msg) packetparser.WritePacket(output, msg)
} }
@ -230,6 +234,7 @@ func SetupConnRpcClient(conn net.Conn, serverImpl ServerImpl) (*WshRpc, chan err
outputCh := make(chan []byte, DefaultOutputChSize) outputCh := make(chan []byte, DefaultOutputChSize)
writeErrCh := make(chan error, 1) writeErrCh := make(chan error, 1)
go func() { go func() {
defer panichandler.PanicHandler("SetupConnRpcClient:AdaptOutputChToStream")
writeErr := AdaptOutputChToStream(outputCh, conn) writeErr := AdaptOutputChToStream(outputCh, conn)
if writeErr != nil { if writeErr != nil {
writeErrCh <- writeErr writeErrCh <- writeErr
@ -237,6 +242,7 @@ func SetupConnRpcClient(conn net.Conn, serverImpl ServerImpl) (*WshRpc, chan err
} }
}() }()
go func() { go func() {
defer panichandler.PanicHandler("SetupConnRpcClient:AdaptStreamToMsgCh")
// when input is closed, close the connection // when input is closed, close the connection
defer conn.Close() defer conn.Close()
AdaptStreamToMsgCh(conn, inputCh) AdaptStreamToMsgCh(conn, inputCh)
@ -264,6 +270,7 @@ func SetupDomainSocketRpcClient(sockName string, serverImpl ServerImpl) (*WshRpc
} }
rtn, errCh, err := SetupConnRpcClient(conn, serverImpl) rtn, errCh, err := SetupConnRpcClient(conn, serverImpl)
go func() { go func() {
defer panichandler.PanicHandler("SetupDomainSocketRpcClient:closeConn")
defer conn.Close() defer conn.Close()
err := <-errCh err := <-errCh
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
@ -410,9 +417,11 @@ func HandleStdIOClient(logName string, input io.Reader, output io.Writer) {
proxy.DisposeRoutes() proxy.DisposeRoutes()
} }
go func() { go func() {
defer panichandler.PanicHandler("HandleStdIOClient:RunUnauthLoop")
proxy.RunUnauthLoop() proxy.RunUnauthLoop()
}() }()
go func() { go func() {
defer panichandler.PanicHandler("HandleStdIOClient:ToRemoteChLoop")
defer closeDoneCh() defer closeDoneCh()
for msg := range proxy.ToRemoteCh { for msg := range proxy.ToRemoteCh {
err := packetparser.WritePacket(output, msg) err := packetparser.WritePacket(output, msg)
@ -423,6 +432,7 @@ func HandleStdIOClient(logName string, input io.Reader, output io.Writer) {
} }
}() }()
go func() { go func() {
defer panichandler.PanicHandler("HandleStdIOClient:RawChLoop")
defer closeDoneCh() defer closeDoneCh()
for msg := range rawCh { for msg := range rawCh {
log.Printf("[%s:stdout] %s", logName, msg) log.Printf("[%s:stdout] %s", logName, msg)
@ -435,6 +445,7 @@ func handleDomainSocketClient(conn net.Conn) {
var routeIdContainer atomic.Pointer[string] var routeIdContainer atomic.Pointer[string]
proxy := MakeRpcProxy() proxy := MakeRpcProxy()
go func() { go func() {
defer panichandler.PanicHandler("handleDomainSocketClient:AdaptOutputChToStream")
writeErr := AdaptOutputChToStream(proxy.ToRemoteCh, conn) writeErr := AdaptOutputChToStream(proxy.ToRemoteCh, conn)
if writeErr != nil { if writeErr != nil {
log.Printf("error writing to domain socket: %v\n", writeErr) log.Printf("error writing to domain socket: %v\n", writeErr)
@ -442,6 +453,7 @@ func handleDomainSocketClient(conn net.Conn) {
}() }()
go func() { go func() {
// when input is closed, close the connection // when input is closed, close the connection
defer panichandler.PanicHandler("handleDomainSocketClient:AdaptStreamToMsgCh")
defer func() { defer func() {
conn.Close() conn.Close()
routeIdPtr := routeIdContainer.Load() routeIdPtr := routeIdContainer.Load()

View File

@ -15,6 +15,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/wavetermdev/waveterm/pkg/panichandler"
) )
func DetectShell(ctx context.Context, client *Distro) (string, error) { func DetectShell(ctx context.Context, client *Distro) (string, error) {
@ -233,6 +235,7 @@ func CpHostToRemote(ctx context.Context, client *Distro, sourcePath string, dest
return fmt.Errorf("cannot open local file %s to send to host: %v", sourcePath, err) return fmt.Errorf("cannot open local file %s to send to host: %v", sourcePath, err)
} }
go func() { go func() {
defer panichandler.PanicHandler("wslutil:cpHostToRemote:catStdin")
io.Copy(catStdin, input) io.Copy(catStdin, input)
installStepCmds["cat"].Cancel() installStepCmds["cat"].Cancel()

View File

@ -13,6 +13,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/telemetry"
"github.com/wavetermdev/waveterm/pkg/userinput" "github.com/wavetermdev/waveterm/pkg/userinput"
"github.com/wavetermdev/waveterm/pkg/util/shellutil" "github.com/wavetermdev/waveterm/pkg/util/shellutil"
@ -234,6 +235,7 @@ func (conn *WslConn) StartConnServer() error {
}) })
// service the I/O // service the I/O
go func() { go func() {
defer panichandler.PanicHandler("wsl:StartConnServer:wait")
// wait for termination, clear the controller // wait for termination, clear the controller
defer conn.WithLock(func() { defer conn.WithLock(func() {
conn.ConnController = nil conn.ConnController = nil
@ -242,6 +244,7 @@ func (conn *WslConn) StartConnServer() error {
log.Printf("conn controller (%q) terminated: %v", conn.GetName(), waitErr) log.Printf("conn controller (%q) terminated: %v", conn.GetName(), waitErr)
}() }()
go func() { go func() {
defer panichandler.PanicHandler("wsl:StartConnServer:handleStdIOClient")
logName := fmt.Sprintf("conncontroller:%s", conn.GetName()) logName := fmt.Sprintf("conncontroller:%s", conn.GetName())
wshutil.HandleStdIOClient(logName, pipeRead, inputPipeWrite) wshutil.HandleStdIOClient(logName, pipeRead, inputPipeWrite)
}() }()

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/wavetermdev/waveterm/pkg/filestore" "github.com/wavetermdev/waveterm/pkg/filestore"
"github.com/wavetermdev/waveterm/pkg/panichandler"
"github.com/wavetermdev/waveterm/pkg/util/dbutil" "github.com/wavetermdev/waveterm/pkg/util/dbutil"
"github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/waveobj"
) )
@ -211,6 +212,7 @@ func DBDelete(ctx context.Context, otype string, id string) error {
return err return err
} }
go func() { go func() {
defer panichandler.PanicHandler("DBDelete:filestore.DeleteZone")
// we spawn a go routine here because we don't want to reuse the DB connection // we spawn a go routine here because we don't want to reuse the DB connection
// since DBDelete is called in a transaction from DeleteTab // since DBDelete is called in a transaction from DeleteTab
deleteCtx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second) deleteCtx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)