From bc9e5e8753c33c73303cc0b3c002147053b36d0b Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 19 Jan 2022 00:23:52 +0000 Subject: [PATCH] shimv2: handle sigint/sigterm This causes sigint/sigterm to trigger a shutdown of the shim. It is needed because otherwise the v2 shim hangs system shutdown. Signed-off-by: Brian Goff (cherry picked from commit 3ffb6a61130298cd27636df2b64a53e2161cdced) Signed-off-by: Varsha Teratipally --- runtime/v2/shim/shim.go | 6 ++++-- runtime/v2/shim/shim_unix.go | 21 ++++++++++++++++++++- runtime/v2/shim/shim_windows.go | 6 +++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/runtime/v2/shim/shim.go b/runtime/v2/shim/shim.go index c14aacca99b..fbdee6047a2 100644 --- a/runtime/v2/shim/shim.go +++ b/runtime/v2/shim/shim.go @@ -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 @@ -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 diff --git a/runtime/v2/shim/shim_unix.go b/runtime/v2/shim/shim_unix.go index a61b6420892..56505a89444 100644 --- a/runtime/v2/shim/shim_unix.go +++ b/runtime/v2/shim/shim_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* @@ -70,7 +71,7 @@ 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 { @@ -78,6 +79,8 @@ func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Si 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 { @@ -89,6 +92,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())) } diff --git a/runtime/v2/shim/shim_windows.go b/runtime/v2/shim/shim_windows.go index 7339eb2a2e4..4f8a944d154 100644 --- a/runtime/v2/shim/shim_windows.go +++ b/runtime/v2/shim/shim_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows /* @@ -48,10 +49,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") }