From e6bd6fbfcca2b0bf932dbfb6ee47653ca0c2852b Mon Sep 17 00:00:00 2001 From: Alex Sharov <AskAlexSharov@gmail.com> Date: Fri, 22 Oct 2021 09:51:49 +0700 Subject: [PATCH] Add p2p msg to panic (#2861) --- cmd/sentry/download/downloader.go | 20 ++++++-- common/debug/log_panic.go | 84 +------------------------------ go.mod | 2 +- go.sum | 4 +- turbo/stages/stageloop.go | 14 ++++-- turbo/txpool/p2p.go | 14 ++++-- 6 files changed, 43 insertions(+), 95 deletions(-) diff --git a/cmd/sentry/download/downloader.go b/cmd/sentry/download/downloader.go index dbf5203aa8..2095d511e1 100644 --- a/cmd/sentry/download/downloader.go +++ b/cmd/sentry/download/downloader.go @@ -12,12 +12,12 @@ import ( "github.com/c2h5oh/datasize" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon-lib/common/dbg" "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces" proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon/common" - debug2 "github.com/ledgerwatch/erigon/common/debug" "github.com/ledgerwatch/erigon/consensus" "github.com/ledgerwatch/erigon/core/forkid" "github.com/ledgerwatch/erigon/core/types" @@ -90,7 +90,11 @@ func RecvUploadMessage(ctx context.Context, handleInboundMessage func(ctx context.Context, inreq *proto_sentry.InboundMessage, sentry direct.SentryClient) error, wg *sync.WaitGroup, ) (err error) { - defer func() { err = debug2.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things streamCtx, cancel := context.WithCancel(ctx) defer cancel() @@ -181,7 +185,11 @@ func RecvUploadHeadersMessage(ctx context.Context, handleInboundMessage func(ctx context.Context, inreq *proto_sentry.InboundMessage, sentry direct.SentryClient) error, wg *sync.WaitGroup, ) (err error) { - defer func() { err = debug2.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things streamCtx, cancel := context.WithCancel(ctx) defer cancel() @@ -274,7 +282,11 @@ func RecvMessage( handleInboundMessage func(ctx context.Context, inreq *proto_sentry.InboundMessage, sentry direct.SentryClient) error, wg *sync.WaitGroup, ) (err error) { - defer func() { err = debug2.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things streamCtx, cancel := context.WithCancel(ctx) defer cancel() defer sentry.MarkDisconnected() diff --git a/common/debug/log_panic.go b/common/debug/log_panic.go index ef863f1ead..c7b91a35f4 100644 --- a/common/debug/log_panic.go +++ b/common/debug/log_panic.go @@ -1,12 +1,10 @@ package debug import ( - "fmt" "os" - "runtime/debug" - "strings" "syscall" + "github.com/ledgerwatch/erigon-lib/common/dbg" "github.com/ledgerwatch/log/v3" ) @@ -16,43 +14,6 @@ func GetSigC(sig *chan os.Signal) { sigc = *sig } -/* -var crashReportDir string -func prettyTime() string { - time := fmt.Sprintf("%v", time.Now()) - return strings.Replace(time[:19], " ", "-", 1) -} -func CheckForCrashes(datadir string) { - crashReportDir = filepath.Join(datadir, "crashreports") - if _, err := os.Stat(crashReportDir); err != nil && os.IsNotExist(err) { - _ = os.MkdirAll(crashReportDir, 0755) - } else if err != nil { - log.Error("log_panic.go: CheckForCrashes", "error", err) - return - } - f, err := os.Open(crashReportDir) - if err != nil { - log.Error("log_panic.go: CheckForCrashes", "error", err) - return - } - fileInfo, err := f.ReadDir(-1) - if err != nil { - log.Error("log_panic.go: CheckForCrashes", "error", err) - return - } - for _, v := range fileInfo { - if !v.IsDir() { - msg := fmt.Sprintf("Crashes From Previous Boots Detected. Find the stack trace in %v", - crashReportDir) - log.Warn(msg) - _ = f.Close() - return - } - } -} -*/ -var panicReplacer = strings.NewReplacer("\n", " ", "\t", "", "\r", "") - // LogPanic - does log panic to logger and to <datadir>/crashreports then stops the process func LogPanic() { panicResult := recover() @@ -60,49 +21,8 @@ func LogPanic() { return } - stack := string(debug.Stack()) - log.Error("catch panic", "err", panicResult, "stack", panicReplacer.Replace(stack)) - //WriteStackTraceOnPanic(stack) + log.Error("catch panic", "err", panicResult, "stack", dbg.Stack()) if sigc != nil { sigc <- syscall.SIGINT } } - -// ReportPanicAndRecover - does save panic to datadir/crashreports, bud doesn't log to logger and doesn't stop the process -// it returns recovered panic as error in format friendly for our logger -// common pattern of use - assign to named output param: -// func A() (err error) { -// defer func() { err = debug.ReportPanicAndRecover() }() // avoid crash because Erigon's core does many things -// } -func ReportPanicAndRecover(err error) error { - panicResult := recover() - if panicResult == nil { - return err - } - - stack := string(debug.Stack()) - switch typed := panicResult.(type) { - case error: - err = fmt.Errorf("%w, trace: %s", typed, panicReplacer.Replace(stack)) - default: - err = fmt.Errorf("%+v, trace: %s", typed, panicReplacer.Replace(stack)) - } - //log.Error("panic", "err", panicResult, "stack", panicReplacer.Replace(stack)) - //WriteStackTraceOnPanic(stack) - return err -} - -/* -func WriteStackTraceOnPanic(stack string) { - fileName := filepath.Join(crashReportDir, prettyTime()+".txt") - f, errFs := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) - if errFs != nil { - log.Error("log_panic.go:WriteStackTraceOnPanic", "error", errFs) - f.Close() - return - } - f.WriteString(stack) - f.Sync() - f.Close() -} -*/ diff --git a/go.mod b/go.mod index 9d774969d9..de20d2b4a8 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/json-iterator/go v1.1.12 github.com/julienschmidt/httprouter v1.3.0 github.com/kevinburke/go-bindata v3.21.0+incompatible - github.com/ledgerwatch/erigon-lib v0.0.0-20211022021239-ec0155d4ed60 + github.com/ledgerwatch/erigon-lib v0.0.0-20211022024120-505bf9724987 github.com/ledgerwatch/log/v3 v3.3.1 github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d github.com/logrusorgru/aurora/v3 v3.0.0 diff --git a/go.sum b/go.sum index 1bc71eab0e..0561aff5e6 100644 --- a/go.sum +++ b/go.sum @@ -497,8 +497,8 @@ github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3P github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= -github.com/ledgerwatch/erigon-lib v0.0.0-20211022021239-ec0155d4ed60 h1:704Nh0khFy4p0Pgifb+gowcKn1i+eMmc7wATsga6/oY= -github.com/ledgerwatch/erigon-lib v0.0.0-20211022021239-ec0155d4ed60/go.mod h1:+Dgxlx2A74QQAp1boH34onSy3k/qDftDiWvGXGC+5FA= +github.com/ledgerwatch/erigon-lib v0.0.0-20211022024120-505bf9724987 h1:R4gmBeavM2kRodijPKvGrPNAax6B7V7HxQOExoUAkho= +github.com/ledgerwatch/erigon-lib v0.0.0-20211022024120-505bf9724987/go.mod h1:+Dgxlx2A74QQAp1boH34onSy3k/qDftDiWvGXGC+5FA= github.com/ledgerwatch/log/v3 v3.3.1 h1:HmvLeTEvtCtqSvtu4t/a5MAdcLfeBcbIeowXbLYuzLc= github.com/ledgerwatch/log/v3 v3.3.1/go.mod h1:S3VJqhhVX32rbp1JyyvhJou12twtFwNEPESBgpbNkRk= github.com/ledgerwatch/secp256k1 v0.0.0-20210626115225-cd5cd00ed72d h1:/IKMrJdfRsoYNc36PXqP4xMH3vhW/8IQyBKGQbKZUno= diff --git a/turbo/stages/stageloop.go b/turbo/stages/stageloop.go index a700544174..5c99b7eb82 100644 --- a/turbo/stages/stageloop.go +++ b/turbo/stages/stageloop.go @@ -9,10 +9,10 @@ import ( "github.com/holiman/uint256" libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/dbg" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon/cmd/sentry/download" "github.com/ledgerwatch/erigon/common" - "github.com/ledgerwatch/erigon/common/debug" "github.com/ledgerwatch/erigon/consensus/misc" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core/rawdb" @@ -123,7 +123,11 @@ func StageLoopStep( updateHead func(ctx context.Context, head uint64, hash common.Hash, td *uint256.Int), snapshotMigratorFinal func(tx kv.Tx) error, ) (err error) { - defer func() { err = debug.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things - + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things var origin, finishProgressBefore uint64 if err := db.View(ctx, func(tx kv.Tx) error { @@ -225,7 +229,11 @@ func StageLoopStep( } func MiningStep(ctx context.Context, kv kv.RwDB, mining *stagedsync.Sync) (err error) { - defer func() { err = debug.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things - + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things tx, err := kv.BeginRw(ctx) if err != nil { diff --git a/turbo/txpool/p2p.go b/turbo/txpool/p2p.go index 1243cc4084..4df920bb6d 100644 --- a/turbo/txpool/p2p.go +++ b/turbo/txpool/p2p.go @@ -11,11 +11,11 @@ import ( "sync" "time" + "github.com/ledgerwatch/erigon-lib/common/dbg" "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces" proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" "github.com/ledgerwatch/erigon/common" - "github.com/ledgerwatch/erigon/common/debug" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/eth/fetcher" "github.com/ledgerwatch/erigon/eth/protocols/eth" @@ -287,7 +287,11 @@ func RecvTxMessage(ctx context.Context, handleInboundMessage func(ctx context.Context, inreq *proto_sentry.InboundMessage, sentry direct.SentryClient) error, wg *sync.WaitGroup, ) (err error) { - defer func() { err = debug.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things streamCtx, cancel := context.WithCancel(ctx) defer cancel() @@ -376,7 +380,11 @@ func RecvPeers(ctx context.Context, recentPeers *txpropagate.RecentlyConnectedPeers, wg *sync.WaitGroup, ) (err error) { - defer func() { err = debug.ReportPanicAndRecover(err) }() // avoid crash because Erigon's core does many things + defer func() { + if rec := recover(); rec != nil { + err = fmt.Errorf("%+v, trace: %s", rec, dbg.Stack()) + } + }() // avoid crash because Erigon's core does many things streamCtx, cancel := context.WithCancel(ctx) defer cancel() -- GitLab