Skip to content

Commit

Permalink
Merge pull request #218 from articulate/feature/replace-reap
Browse files Browse the repository at this point in the history
refactor: replace reap function with package
  • Loading branch information
mloberg authored May 20, 2024
2 parents 47db45c + 8c86066 commit ec7bec4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 37 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
39 changes: 2 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"syscall"
"time"

"github.com/hashicorp/go-reap"
"golang.org/x/term"
)

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
}()
}
}

0 comments on commit ec7bec4

Please sign in to comment.