diff --git a/runtime/v2/shim/shim.go b/runtime/v2/shim/shim.go index c14aacca99b8f..35144f98efefc 100644 --- a/runtime/v2/shim/shim.go +++ b/runtime/v2/shim/shim.go @@ -40,9 +40,9 @@ import ( // Client for a shim server type Client struct { - service shimapi.TaskService - context context.Context - signals chan os.Signal + service shimapi.TaskService + context context.Context + signals chan os.Signal } // Publisher for events @@ -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 @@ -273,15 +273,16 @@ func run(id string, initFunc Init, config Config) error { // NewShimClient creates a new shim server client func NewShimClient(ctx context.Context, svc shimapi.TaskService, signals chan os.Signal) *Client { s := &Client{ - service: svc, - context: ctx, - signals: signals, + service: svc, + context: ctx, + signals: signals, } return s } // Serve the shim server func (s *Client) Serve() error { + ctx, cancel := context.WithCancel(s.context) dump := make(chan os.Signal, 32) setupDumpStacks(dump) @@ -297,7 +298,7 @@ func (s *Client) Serve() error { logrus.Debug("registering ttrpc server") shimapi.RegisterTaskService(server, s.service) - if err := serve(s.context, server, socketFlag); err != nil { + if err := serve(ctx, server, socketFlag); err != nil { return err } logger := logrus.WithFields(logrus.Fields{ @@ -310,7 +311,8 @@ func (s *Client) Serve() error { dumpStacks(logger) } }() - return handleSignals(s.context, logger, s.signals) + 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 a61b642089217..ce599a501ddd9 100644 --- a/runtime/v2/shim/shim_unix.go +++ b/runtime/v2/shim/shim_unix.go @@ -70,7 +70,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 +78,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 +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())) } diff --git a/runtime/v2/shim/shim_windows.go b/runtime/v2/shim/shim_windows.go index 7339eb2a2e4c2..955e2aecabe4d 100644 --- a/runtime/v2/shim/shim_windows.go +++ b/runtime/v2/shim/shim_windows.go @@ -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") }