Skip to content

Commit

Permalink
shimv2: handle sigint/sigterm
Browse files Browse the repository at this point in the history
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 <cpuguy83@gmail.com>
(cherry picked from commit 3ffb6a6)
Signed-off-by: Varsha Teratipally <teratipally@google.com>
  • Loading branch information
cpuguy83 authored and vteratipally committed Feb 3, 2022
1 parent dff924c commit 9374c16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
15 changes: 9 additions & 6 deletions runtime/v2/shim/shim.go
Expand Up @@ -43,6 +43,7 @@ type Client struct {
service shimapi.TaskService
context context.Context
signals chan os.Signal
shutdown context.CancelFunc
}

// Publisher for events
Expand Down Expand Up @@ -214,7 +215,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 @@ -248,7 +249,7 @@ func run(id string, initFunc Init, config Config) error {
return err
}
}
client := NewShimClient(ctx, service, signals)
client := NewShimClient(ctx, service, signals, cancel)
if err := client.Serve(); err != nil {
if err != context.Canceled {
return err
Expand All @@ -271,11 +272,12 @@ 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 {
func NewShimClient(ctx context.Context, svc shimapi.TaskService, signals chan os.Signal, shutdown context.CancelFunc) *Client {
s := &Client{
service: svc,
context: ctx,
signals: signals,
shutdown: shutdown,
}
return s
}
Expand Down Expand Up @@ -310,7 +312,8 @@ func (s *Client) Serve() error {
dumpStacks(logger)
}
}()
return handleSignals(s.context, logger, s.signals)
go handleExitSignals(s.context, logger, s.shutdown)
return reap(s.context, logger, s.signals)
}

// serve serves the ttrpc API over a unix socket at the provided path
Expand All @@ -323,7 +326,7 @@ func serve(ctx context.Context, server *ttrpc.Server, path string) error {
go func() {
defer l.Close()
if err := server.Serve(ctx, l); err != nil &&
!strings.Contains(err.Error(), "use of closed network connection") {
!strings.Contains(err.Error(), "use of closed network connection") {
logrus.WithError(err).Fatal("containerd-shim: ttrpc server failure")
}
}()
Expand All @@ -343,4 +346,4 @@ func dumpStacks(logger *logrus.Entry) {
}
buf = buf[:stackSize]
logger.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
}
}
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")
}

0 comments on commit 9374c16

Please sign in to comment.