Skip to content

Commit

Permalink
Drop to a shell on panic
Browse files Browse the repository at this point in the history
Note: there is no real great way to test this in a unit test without starting a shell and so this change needs to be taken on faith, or tested by hand
  • Loading branch information
jspc committed Feb 26, 2023
1 parent 2618952 commit 3399f48
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
Expand All @@ -24,6 +25,8 @@ var (
)

func main() {
defer panicHandler()

var err error
sugar, err = NewLogger(kmesgF)
if err != nil {
Expand Down Expand Up @@ -141,6 +144,27 @@ func loadTLSCredentials() (credentials.TransportCredentials, error) {
return credentials.NewTLS(config), nil
}

func panicHandler() {
err := recover()
if err != nil {
stack := string(debug.Stack())
errStr := err.(error).Error()

if sugar.c != nil {
sugar.Errorw("vinit panic!",
"error", errStr,
"trace", stack,
)
} else {
// This branch occurs on any panic before our
// kmesg handler is enabled
fmt.Printf("panic:\n%s\n%s", errStr, stack)
}

recoveryShell()
}
}

func recoveryShell() {
fmt.Println("Press Ctrl+D to reboot")

Expand Down

0 comments on commit 3399f48

Please sign in to comment.