X Tutup
Skip to content

Commit 3ffb6a6

Browse files
committed
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 <cpuguy83@gmail.com>
1 parent fd35ca2 commit 3ffb6a6

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

runtime/v2/shim/shim.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func run(ctx context.Context, manager Manager, initFunc Init, name string, confi
305305
"pid": os.Getpid(),
306306
"namespace": namespaceFlag,
307307
})
308-
go handleSignals(ctx, logger, signals)
308+
go reap(ctx, logger, signals)
309309
ss, err := manager.Stop(ctx, id)
310310
if err != nil {
311311
return err
@@ -428,7 +428,7 @@ func run(ctx context.Context, manager Manager, initFunc Init, name string, confi
428428
}
429429
}
430430

431-
if err := serve(ctx, server, signals); err != nil {
431+
if err := serve(ctx, server, signals, sd.Shutdown); err != nil {
432432
if err != shutdown.ErrShutdown {
433433
return err
434434
}
@@ -450,7 +450,7 @@ func run(ctx context.Context, manager Manager, initFunc Init, name string, confi
450450

451451
// serve serves the ttrpc API over a unix socket in the current working directory
452452
// and blocks until the context is canceled
453-
func serve(ctx context.Context, server *ttrpc.Server, signals chan os.Signal) error {
453+
func serve(ctx context.Context, server *ttrpc.Server, signals chan os.Signal, shutdown func()) error {
454454
dump := make(chan os.Signal, 32)
455455
setupDumpStacks(dump)
456456

@@ -480,7 +480,9 @@ func serve(ctx context.Context, server *ttrpc.Server, signals chan os.Signal) er
480480
dumpStacks(logger)
481481
}
482482
}()
483-
return handleSignals(ctx, logger, signals)
483+
484+
go handleExitSignals(ctx, logger, shutdown)
485+
return reap(ctx, logger, signals)
484486
}
485487

486488
func dumpStacks(logger *logrus.Entry) {

runtime/v2/shim/shim_unix.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ func serveListener(path string) (net.Listener, error) {
7171
return l, nil
7272
}
7373

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

7777
for {
7878
select {
7979
case <-ctx.Done():
8080
return ctx.Err()
8181
case s := <-signals:
82+
// Exit signals are handled separately from this loop
83+
// They get registered with this channel so that we can ignore such signals for short-running actions (e.g. `delete`)
8284
switch s {
8385
case unix.SIGCHLD:
8486
if err := reaper.Reap(); err != nil {
@@ -90,6 +92,22 @@ func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Si
9092
}
9193
}
9294

95+
func handleExitSignals(ctx context.Context, logger *logrus.Entry, cancel context.CancelFunc) {
96+
ch := make(chan os.Signal, 32)
97+
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
98+
99+
for {
100+
select {
101+
case s := <-ch:
102+
logger.WithField("signal", s).Debugf("Caught exit signal")
103+
cancel()
104+
return
105+
case <-ctx.Done():
106+
return
107+
}
108+
}
109+
}
110+
93111
func openLog(ctx context.Context, _ string) (io.Writer, error) {
94112
return fifo.OpenFifoDup2(ctx, "log", unix.O_WRONLY, 0700, int(os.Stderr.Fd()))
95113
}

runtime/v2/shim/shim_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ func serveListener(path string) (net.Listener, error) {
4646
return nil, errors.New("not supported")
4747
}
4848

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

53+
func handleExitSignals(ctx context.Context, logger *logrus.Entry, cancel context.CancelFunc) {
54+
}
55+
5356
func openLog(ctx context.Context, _ string) (io.Writer, error) {
5457
return nil, errors.New("not supported")
5558
}

0 commit comments

Comments
 (0)
X Tutup