-
Notifications
You must be signed in to change notification settings - Fork 140
CBG-4714: collect goroutine stack traces on signal #7870
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
base: main
Are you sure you want to change the base?
Changes from all commits
ce80821
1beaae4
df92f51
033a788
2e41703
0ec5e51
ac7795c
201310a
8207199
e54a721
2c1bfeb
027cd4f
085e411
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| //go:build !windows | ||
| // +build !windows | ||
|
Collaborator
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. I think this line is actually unnecessary and will be flagged by govet in go 1.25, but not sure, we can leave this.
Contributor
Author
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. I use go 1.25 locally and my linter didn't pick that up so should be okay fro now (I think) |
||
|
|
||
| /* | ||
| Copyright 2025-Present Couchbase, Inc. | ||
| Use of this software is governed by the Business Source License included in | ||
| the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that | ||
| file, in accordance with the Business Source License, use of this software will | ||
| be governed by the Apache License, Version 2.0, included in the file | ||
| licenses/APL2.txt. | ||
| */ | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/couchbase/sync_gateway/base" | ||
| ) | ||
|
|
||
| // RegisterSignalHandler invokes functions based on the given signals for unix environments: | ||
| // - SIGHUP causes Sync Gateway to rotate log files. | ||
| // - SIGINT or SIGTERM causes Sync Gateway to exit cleanly. | ||
| // - SIGKILL cannot be handled by the application. | ||
| // - SIGUSR1 causes Sync Gateway to log stack traces for all goroutines. | ||
| func RegisterSignalHandler(ctx context.Context, logDirectory string) chan os.Signal { | ||
| signalChannel := make(chan os.Signal, 1) | ||
| signal.Notify(signalChannel, syscall.SIGHUP, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1) | ||
|
|
||
| go func() { | ||
| for sig := range signalChannel { | ||
| base.InfofCtx(ctx, base.KeyAll, "Handling signal: %v", sig) | ||
| switch sig { | ||
| case syscall.SIGHUP: | ||
| HandleSighup(ctx) | ||
| case syscall.SIGUSR1: | ||
| stackTrace, err := base.GetStackTrace() | ||
| if err != nil { | ||
| base.WarnfCtx(ctx, "Error collecting stack trace: %v", err) | ||
| } else { | ||
| base.InfofCtx(ctx, base.KeyAll, "Collecting stack trace for all goroutines") | ||
| } | ||
| // log to console and log to file in the log directory | ||
| currentTime := time.Now() | ||
| timestamp := currentTime.Format(time.RFC3339) | ||
| base.LogStackTraces(ctx, logDirectory, stackTrace, timestamp) | ||
| default: | ||
| // Ensure log buffers are flushed before exiting. | ||
| base.FlushLogBuffers() | ||
| os.Exit(130) // 130 == exit code 128 + 2 (interrupt) | ||
| } | ||
| } | ||
| }() | ||
| return signalChannel | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //go:build windows | ||
| // +build windows | ||
|
|
||
| /* | ||
| Copyright 2025-Present Couchbase, Inc. | ||
|
|
||
| Use of this software is governed by the Business Source License included in | ||
| the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that | ||
| file, in accordance with the Business Source License, use of this software will | ||
| be governed by the Apache License, Version 2.0, included in the file | ||
| licenses/APL2.txt. | ||
| */ | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/couchbase/sync_gateway/base" | ||
| ) | ||
|
|
||
| // RegisterSignalHandler invokes functions based on the given signals for windows environments: | ||
| // - SIGHUP causes Sync Gateway to rotate log files. | ||
| // - SIGINT or SIGTERM causes Sync Gateway to exit cleanly. | ||
| // - SIGKILL cannot be handled by the application. | ||
| func RegisterSignalHandler(ctx context.Context, logDirectory string) chan os.Signal { | ||
| signalChannel := make(chan os.Signal, 1) | ||
| signal.Notify(signalChannel, syscall.SIGHUP, os.Interrupt, syscall.SIGTERM) | ||
|
|
||
| go func() { | ||
| for sig := range signalChannel { | ||
| base.InfofCtx(ctx, base.KeyAll, "Handling signal: %v", sig) | ||
| switch sig { | ||
| case syscall.SIGHUP: | ||
| HandleSighup(ctx) | ||
| default: | ||
| // Ensure log buffers are flushed before exiting. | ||
| base.FlushLogBuffers() | ||
| os.Exit(130) // 130 == exit code 128 + 2 (interrupt) | ||
| } | ||
| } | ||
| }() | ||
| return signalChannel | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.