Skip to content

Commit

Permalink
app/node: catch panicError
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Dec 16, 2024
1 parent 272a040 commit a291500
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/node/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func buildServer(ctx context.Context, d *coreDependencies) *server {
closers: []func() error{}, // logger.Close is not in here; do it in a defer in Start
logger: d.logger,
}
d.closers = closers

valSet := make(map[string]ktypes.Validator)
for _, v := range d.genesisCfg.Validators {
Expand Down
2 changes: 2 additions & 0 deletions app/node/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type coreDependencies struct {
genesisCfg *config.GenesisConfig
privKey crypto.PrivateKey

closers *closeFuncs // for clean close on failBuild

adminKey *tls.Certificate
// autogen bool

Expand Down
27 changes: 24 additions & 3 deletions app/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"

"github.com/kwilteam/kwil-db/config"
"github.com/kwilteam/kwil-db/core/crypto"
Expand Down Expand Up @@ -41,7 +42,7 @@ type server struct {
jsonRPCAdminServer *rpcserver.Server
}

func runNode(ctx context.Context, rootDir string, cfg *config.Config) error {
func runNode(ctx context.Context, rootDir string, cfg *config.Config) (err error) {
// Writing to stdout and a log file. TODO: config outputs
rot, err := log.NewRotatorWriter(filepath.Join(rootDir, "kwild.log"), 10_000, 0)
if err != nil {
Expand Down Expand Up @@ -105,10 +106,30 @@ func runNode(ctx context.Context, rootDir string, cfg *config.Config) error {
poolOpener: newPoolBOpener(host, port, user, pass),
}

// Catch any panic from buildServer. We use a panic based build failure
// system, which is detectable with panicErr. Spewing out a stack is sloppy
// and a bad look; just return a meaningful error message. If the message is
// ambiguous regarding its source, the errors needs more context.
defer func() {
if r := recover(); r != nil {
if pe, ok := r.(panicErr); ok { // error on bringup, not a bug
if errors.Is(pe.err, context.Canceled) {
logger.Warnf("Shutdown signaled: %v", pe.msg)
err = context.Canceled
} else {
err = pe
}
} else { // actual panic (bug)
stack := make([]byte, 8192)
length := runtime.Stack(stack, false)
err = fmt.Errorf("panic while building kwild: %v\n\nstack:\n\n%v", r, string(stack[:length]))
}
d.closers.closeAll()
}
}()

server := buildServer(ctx, d)

// start the server
// Start is blocking, for now.
return server.Start(ctx)
}

Expand Down

0 comments on commit a291500

Please sign in to comment.