diff --git a/cmd/hook.go b/cmd/hook.go index 495dbbd79d..c1de29c584 100644 --- a/cmd/hook.go +++ b/cmd/hook.go @@ -11,7 +11,6 @@ import ( "time" "github.com/go-acme/lego/v4/certificate" - "github.com/go-acme/lego/v4/log" ) const ( @@ -49,13 +48,13 @@ func launchHook(hook string, timeout time.Duration, meta map[string]string) erro return fmt.Errorf("start command: %w", err) } - timer := time.AfterFunc(timeout, func() { - log.Println("hook timed out: killing command") - _ = cmd.Process.Kill() - _ = stdout.Close() - }) - - defer timer.Stop() + go func() { + <-ctxCmd.Done() + if ctxCmd.Err() != nil { + _ = cmd.Process.Kill() + _ = stdout.Close() + } + }() scanner := bufio.NewScanner(stdout) for scanner.Scan() { diff --git a/cmd/hook_test.go b/cmd/hook_test.go index dd8551da6d..d643bba303 100644 --- a/cmd/hook_test.go +++ b/cmd/hook_test.go @@ -8,6 +8,11 @@ import ( "github.com/stretchr/testify/require" ) +func Test_launchHook(t *testing.T) { + err := launchHook("echo foo", 1*time.Second, map[string]string{}) + require.NoError(t, err) +} + func Test_launchHook_errors(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("skipping test on Windows")