1
0
mirror of https://github.com/go-acme/lego.git synced 2025-07-14 19:04:19 +02:00

chore: related timer with context.Done (#2471)

Co-authored-by: Dominik Menke <git@dmke.org>
This commit is contained in:
Ludovic Fernandez
2025-03-05 15:07:13 +01:00
committed by GitHub
parent 3b9752b625
commit 2bc147f58a
2 changed files with 12 additions and 8 deletions

View File

@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/log"
) )
const ( const (
@ -49,13 +48,13 @@ func launchHook(hook string, timeout time.Duration, meta map[string]string) erro
return fmt.Errorf("start command: %w", err) return fmt.Errorf("start command: %w", err)
} }
timer := time.AfterFunc(timeout, func() { go func() {
log.Println("hook timed out: killing command") <-ctxCmd.Done()
_ = cmd.Process.Kill() if ctxCmd.Err() != nil {
_ = stdout.Close() _ = cmd.Process.Kill()
}) _ = stdout.Close()
}
defer timer.Stop() }()
scanner := bufio.NewScanner(stdout) scanner := bufio.NewScanner(stdout)
for scanner.Scan() { for scanner.Scan() {

View File

@ -8,6 +8,11 @@ import (
"github.com/stretchr/testify/require" "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) { func Test_launchHook_errors(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows") t.Skip("skipping test on Windows")