- Notifications
You must be signed in to change notification settings - Fork924
feat(agent): Handle signals and shutdown gracefully#5914
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
41 changes: 31 additions & 10 deletionscli/agent.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,6 +7,7 @@ import ( | ||
"net/http/pprof" | ||
"net/url" | ||
"os" | ||
"os/signal" | ||
"path/filepath" | ||
"runtime" | ||
"time" | ||
@@ -35,12 +36,10 @@ func workspaceAgent() *cobra.Command { | ||
Use: "agent", | ||
// This command isn't useful to manually execute. | ||
Hidden: true, | ||
RunE: func(cmd *cobra.Command,_ []string) error { | ||
ctx, cancel := context.WithCancel(cmd.Context()) | ||
defer cancel() | ||
rawURL, err := cmd.Flags().GetString(varAgentURL) | ||
if err != nil { | ||
return xerrors.Errorf("CODER_AGENT_URL must be set: %w", err) | ||
@@ -50,18 +49,18 @@ func workspaceAgent() *cobra.Command { | ||
return xerrors.Errorf("parse %q: %w", rawURL, err) | ||
} | ||
isLinux := runtime.GOOS == "linux" | ||
// Spawn a reaper so that we don't accumulate a ton | ||
// of zombie processes. | ||
if reaper.IsInitProcess() && !noReap && isLinux { | ||
logWriter := &lumberjack.Logger{ | ||
Filename: filepath.Join(os.TempDir(), "coder-agent-init.log"), | ||
MaxSize: 5, // MB | ||
} | ||
defer logWriter.Close() | ||
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug) | ||
Comment on lines +57 to +62 MemberAuthor 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. This change might not actually be needed, but it feels safer than having both PID 1 and the forked process share an opened file. | ||
logger.Info(ctx, "spawning reaper process") | ||
// Do not start a reaper on the child process. It's important | ||
// to do this else we fork bomb ourselves. | ||
@@ -76,6 +75,28 @@ func workspaceAgent() *cobra.Command { | ||
return nil | ||
} | ||
// Handle interrupt signals to allow for graceful shutdown, | ||
// note that calling stopNotify disables the signal handler | ||
// and the next interrupt will terminate the program (you | ||
// probably want cancel instead). | ||
// | ||
// Note that we don't want to handle these signals in the | ||
// process that runs as PID 1, that's why we do this after | ||
// the reaper forked. | ||
ctx, stopNotify := signal.NotifyContext(ctx, InterruptSignals...) | ||
defer stopNotify() | ||
// dumpHandler does signal handling, so we call it after the | ||
// reaper. | ||
go dumpHandler(ctx) | ||
logWriter := &lumberjack.Logger{ | ||
Filename: filepath.Join(os.TempDir(), "coder-agent.log"), | ||
MaxSize: 5, // MB | ||
} | ||
defer logWriter.Close() | ||
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug) | ||
version := buildinfo.Version() | ||
logger.Info(ctx, "starting agent", | ||
slog.F("url", coderURL), | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.