Skip to content
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

Add a timeout to graceful shutdown and reload #38544

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/service/signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"

apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
Expand Down Expand Up @@ -84,18 +85,24 @@ func (process *TeleportProcess) WaitForSignals(ctx context.Context) error {
case signal := <-sigC:
switch signal {
case syscall.SIGQUIT:
process.Shutdown(ctx)
timeoutCtx, cancel := context.WithTimeout(ctx, apidefaults.MaxCertDuration)
defer cancel()
process.Shutdown(timeoutCtx)
process.log.Infof("All services stopped, exiting.")
return nil
case syscall.SIGTERM, syscall.SIGINT:
timeout := getShutdownTimeout(process.log)
cancelCtx, cancelFunc := context.WithTimeout(ctx, timeout)
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
process.log.Infof("Got signal %q, exiting within %vs.", signal, timeout.Seconds())
// we run the shutdown in a goroutine and return when the
// context is done even if Shutdown hasn't returned because we
// want to ensure that we exit shortly after SIGTERM even in
// case of bugs
go func() {
defer cancelFunc()
process.Shutdown(cancelCtx)
defer cancel()
process.Shutdown(timeoutCtx)
}()
<-cancelCtx.Done()
<-timeoutCtx.Done()
process.log.Infof("All services stopped or timeout passed, exiting immediately.")
return nil
case syscall.SIGUSR1:
Expand Down Expand Up @@ -124,7 +131,9 @@ func (process *TeleportProcess) WaitForSignals(ctx context.Context) error {
continue
}
process.log.Infof("Successfully started new process, shutting down gracefully.")
process.Shutdown(ctx)
timeoutCtx, cancel := context.WithTimeout(ctx, apidefaults.MaxCertDuration)
defer cancel()
process.Shutdown(timeoutCtx)
process.log.Infof("All services stopped, exiting.")
return nil
default:
Expand Down