From d1263905805ea39cbdee5c2031e8046566f220f7 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jan 2023 20:58:56 -0600 Subject: [PATCH] contrib: Docker forward entrypoint signals. This modifies the entrypoint app used by the Docker image to forward SIGTERM to the running instance so it shuts down gracefully when a container is stopped. --- contrib/docker/entrypoint/entrypoint.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/contrib/docker/entrypoint/entrypoint.go b/contrib/docker/entrypoint/entrypoint.go index 4ebc8d1ff3..0a3d43fdba 100644 --- a/contrib/docker/entrypoint/entrypoint.go +++ b/contrib/docker/entrypoint/entrypoint.go @@ -1,4 +1,4 @@ -// Copyright (c) 2021 The Decred developers +// Copyright (c) 2021-2023 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -8,7 +8,9 @@ import ( "fmt" "os" "os/exec" + "os/signal" "path/filepath" + "syscall" ) const ( @@ -101,11 +103,20 @@ func main() { } // Run the command with the given arguments while redirecting stdin, stdout, - // and stderr to the parent process. + // and stderr to the parent process. Also, listen for SIGTERM and forward + // it to ensure the child process has the opportunity to perform a graceful + // shutdown. cmd := exec.Command(arg0, args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + go func() { + interruptChannel := make(chan os.Signal, 1) + signal.Notify(interruptChannel, syscall.SIGTERM) + for sig := range interruptChannel { + cmd.Process.Signal(sig) + } + }() if err := cmd.Run(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(cmd.ProcessState.ExitCode())