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"
"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() {

View File

@ -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")