From bfaee6e8dd9dbce8dd8f9de8d493a8de617178da Mon Sep 17 00:00:00 2001 From: Paul Lorenz Date: Mon, 17 May 2021 17:30:37 -0400 Subject: [PATCH] Add stackdump on SIGQUIT. Add timeout to ziti ps stack --- ziti-router/subcmd/run.go | 16 ++++++++++++---- ziti/cmd/ziti/cmd/ps_stack.go | 8 +++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ziti-router/subcmd/run.go b/ziti-router/subcmd/run.go index 8ad626972..723d102f2 100644 --- a/ziti-router/subcmd/run.go +++ b/ziti-router/subcmd/run.go @@ -17,6 +17,7 @@ package subcmd import ( + "fmt" "github.com/michaelquigley/pfxlog" "github.com/openziti/edge/edge_common" "github.com/openziti/edge/router/fabric" @@ -26,6 +27,7 @@ import ( "github.com/openziti/fabric/router" "github.com/openziti/fabric/router/xgress" "github.com/openziti/foundation/agent" + "github.com/openziti/foundation/util/debugz" "github.com/openziti/ziti/common/version" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -96,7 +98,7 @@ func run(cmd *cobra.Command, args []string) { logrus.Panicf("error registering edge tunnel in framework (%v)", err) } - go waitForShutdown(r) + go waitForShutdown(r, config) if err := r.Run(); err != nil { logrus.WithError(err).Fatal("error starting") @@ -115,11 +117,17 @@ func getFlags(cmd *cobra.Command) map[string]*pflag.Flag { return ret } -func waitForShutdown(r *router.Router) { +func waitForShutdown(r *router.Router, config *router.Config) { ch := make(chan os.Signal, 1) - signal.Notify(ch, os.Interrupt, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM) + signal.Notify(ch, os.Interrupt, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM) - <-ch + s := <-ch + + if s == syscall.SIGQUIT { + fmt.Println("=== STACK DUMP BEGIN ===") + debugz.DumpStack() + fmt.Println("=== STACK DUMP CLOSE ===") + } pfxlog.Logger().Info("shutting down ziti-router") diff --git a/ziti/cmd/ziti/cmd/ps_stack.go b/ziti/cmd/ziti/cmd/ps_stack.go index 791cbbe22..856a9edf2 100644 --- a/ziti/cmd/ziti/cmd/ps_stack.go +++ b/ziti/cmd/ziti/cmd/ps_stack.go @@ -23,12 +23,14 @@ import ( "github.com/spf13/cobra" "io" "os" + "time" ) // PsStackOptions the options for the create spring command type PsStackOptions struct { PsOptions CtrlListener string + StackTimeout time.Duration } // NewCmdPsStack creates a command object for the "create" command @@ -45,7 +47,7 @@ func NewCmdPsStack(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Co cmd := &cobra.Command{ Args: cobra.MaximumNArgs(1), - Use: "stack ", + Use: "stack []", Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args @@ -55,12 +57,16 @@ func NewCmdPsStack(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Co } options.addCommonFlags(cmd) + cmd.Flags().DurationVar(&options.StackTimeout, "stack-timeout", 5*time.Second, "Timeout for stack operation") return cmd } // Run implements the command func (o *PsStackOptions) Run() error { + time.AfterFunc(o.StackTimeout, func() { + os.Exit(-1) + }) addr, err := agent.ParseGopsAddress(o.Args) if err != nil { return err