diff --git a/go.mod b/go.mod index 064c3c1..08ed614 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/aws/aws-sdk-go-v2/config v1.27.10 github.com/aws/aws-sdk-go-v2/service/kms v1.30.1 github.com/hashicorp/consul/api v1.28.2 + github.com/hashicorp/go-reap v0.0.0-20230117204525-bf69c61a7b71 github.com/hashicorp/vault/api v1.12.0 github.com/hashicorp/vault/api/auth/aws v0.6.0 github.com/hashicorp/vault/api/auth/kubernetes v0.6.0 diff --git a/go.sum b/go.sum index d5b72da..a01ad05 100644 --- a/go.sum +++ b/go.sum @@ -108,6 +108,8 @@ github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHh github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/hashicorp/go-reap v0.0.0-20230117204525-bf69c61a7b71 h1:ntMIobjNd0QLB/i6OQM/OV1B+k6RjmvtY84z/SUeYPA= +github.com/hashicorp/go-reap v0.0.0-20230117204525-bf69c61a7b71/go.mod h1:qIFzeFcJU3OIFk/7JreWXcUjFmcCaeHTH9KoNyHYVCs= github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM= github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= diff --git a/main.go b/main.go index 716339f..44a5bf2 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "syscall" "time" + "github.com/hashicorp/go-reap" "golang.org/x/term" ) @@ -68,7 +69,7 @@ func main() { } if os.Getpid() == 1 { - go reapChildren(ctx, logger) + go reap.ReapChildren(nil, nil, nil, nil) } cmd := os.Args[1] @@ -221,39 +222,3 @@ func run(ctx context.Context, name string, args, env []string, l *slog.Logger) i } return 0 } - -func reapChildren(ctx context.Context, l *slog.Logger) { - ch := make(chan os.Signal, 1) - signal.Notify(ch, syscall.SIGCHLD) - defer signal.Stop(ch) - - for { - select { - case <-ch: - // run our reap process below - case <-ctx.Done(): - return - } - - func() { - POLL: - var status syscall.WaitStatus - pid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, nil) - switch { - case err == nil: - if pid > 0 { - l.DebugContext(ctx, "Reaped child process", "pid", pid, "status", status) - goto POLL - } - return - case errors.Is(err, syscall.ECHILD): - return - case errors.Is(err, syscall.EINTR): - goto POLL - default: - l.WarnContext(ctx, "Error while reaping child process", "error", err) - return - } - }() - } -}