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

[release/1.5] shimv2: handle sigint/sigterm #6509

Merged
merged 1 commit into from Feb 9, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions runtime/v2/shim/shim.go
Expand Up @@ -214,7 +214,7 @@ func run(id string, initFunc Init, config Config) error {
"pid": os.Getpid(),
"namespace": namespaceFlag,
})
go handleSignals(ctx, logger, signals)
go reap(ctx, logger, signals)
response, err := service.Cleanup(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -310,7 +310,9 @@ func (s *Client) Serve() error {
dumpStacks(logger)
}
}()
return handleSignals(s.context, logger, s.signals)
ctx, cancel := context.WithCancel(s.context)
go handleExitSignals(ctx, logger, cancel)
return reap(ctx, logger, s.signals)
}

// serve serves the ttrpc API over a unix socket at the provided path
Expand Down
20 changes: 19 additions & 1 deletion runtime/v2/shim/shim_unix.go
Expand Up @@ -70,14 +70,16 @@ func serveListener(path string) (net.Listener, error) {
return l, nil
}

func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
func reap(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
logger.Info("starting signal loop")

for {
select {
case <-ctx.Done():
return ctx.Err()
case s := <-signals:
// Exit signals are handled separately from this loop
// They get registered with this channel so that we can ignore such signals for short-running actions (e.g. `delete`)
switch s {
case unix.SIGCHLD:
if err := reaper.Reap(); err != nil {
Expand All @@ -89,6 +91,22 @@ func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Si
}
}

func handleExitSignals(ctx context.Context, logger *logrus.Entry, cancel context.CancelFunc) {
ch := make(chan os.Signal, 32)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

for {
select {
case s := <-ch:
logger.WithField("signal", s).Debugf("Caught exit signal")
cancel()
return
case <-ctx.Done():
return
}
}
}

func openLog(ctx context.Context, _ string) (io.Writer, error) {
return fifo.OpenFifoDup2(ctx, "log", unix.O_WRONLY, 0700, int(os.Stderr.Fd()))
}
5 changes: 4 additions & 1 deletion runtime/v2/shim/shim_windows.go
Expand Up @@ -48,10 +48,13 @@ func serveListener(path string) (net.Listener, error) {
return nil, errors.New("not supported")
}

func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
func reap(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
return errors.New("not supported")
}

func handleExitSignals(ctx context.Context, logger *logrus.Entry, cancel context.CancelFunc) {
}

func openLog(ctx context.Context, _ string) (io.Writer, error) {
return nil, errors.New("not supported")
}