package util import ( "context" "os/exec" "syscall" "time" ) const killWaitDelay = time.Second // KillableCommand is the single entry point for context-aware external commands. // yt-dlp, ffmpeg and ffprobe spawn helpers that inherit the output pipe, and Wait // and Output both block on it until every holder exits. Killing the direct child // alone does not end the call; the group signal does, with WaitDelay as the // backstop for whatever survives it. func KillableCommand(ctx context.Context, path string, args ...string) *exec.Cmd { cmd := exec.CommandContext(ctx, path, args...) cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } cmd.WaitDelay = killWaitDelay return cmd }