-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
operator: print callstack when exiting before preparing done #51371
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,16 @@ package utils | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"runtime" | ||
"strings" | ||
"sync/atomic" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/docker/go-units" | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/kvproto/pkg/metapb" | ||
"github.com/pingcap/log" | ||
|
@@ -157,3 +165,52 @@ func WithCleanUp(errOut *error, timeout time.Duration, fn func(context.Context) | |
log.Warn("Encountered but ignored error while cleaning up.", zap.Error(err)) | ||
} | ||
} | ||
|
||
func AllStackInfo() []byte { | ||
res := make([]byte, 256*units.KiB) | ||
for { | ||
n := runtime.Stack(res, true) | ||
if n < len(res) { | ||
return res[:n] | ||
} | ||
res = make([]byte, len(res)*2) | ||
} | ||
} | ||
|
||
var ( | ||
DumpGoroutineWhenExit atomic.Bool | ||
) | ||
|
||
func StartExitSingleListener(ctx context.Context) (context.Context, context.CancelFunc) { | ||
cx, cancel := context.WithCancel(ctx) | ||
sc := make(chan os.Signal, 1) | ||
signal.Notify(sc, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To confirm, will a normal init job exit print the go routines also? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, goroutine dump won't be printed once prepare done. |
||
syscall.SIGHUP, | ||
syscall.SIGINT, | ||
syscall.SIGTERM, | ||
syscall.SIGQUIT) | ||
go func() { | ||
sig := <-sc | ||
dumpGoroutine := DumpGoroutineWhenExit.Load() | ||
padding := strings.Repeat("=", 8) | ||
printDelimate := func(s string) { | ||
fmt.Printf("%s[ %s ]%s\n", padding, s, padding) | ||
} | ||
fmt.Println() | ||
printDelimate(fmt.Sprintf("Got signal %v to exit.", sig)) | ||
printDelimate(fmt.Sprintf("Required Goroutine Dump = %v", dumpGoroutine)) | ||
if DumpGoroutineWhenExit.Load() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use |
||
printDelimate("Start Dumping Goroutine") | ||
os.Stdout.Write(AllStackInfo()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
printDelimate("End of Dumping Goroutine") | ||
} | ||
log.Warn("received signal to exit", zap.Stringer("signal", sig)) | ||
cancel() | ||
fmt.Fprintln(os.Stderr, "gracefully shutting down, press ^C again to force exit") | ||
<-sc | ||
// Even user use SIGTERM to exit, there isn't any checkpoint for resuming, | ||
// hence returning fail exit code. | ||
os.Exit(1) | ||
}() | ||
return cx, cancel | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems no need set it back