mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-04 22:37:55 +00:00
Contains a number of small bug fixes where jobs behaved differently from GitHub or failed silently.
1. Hooks fail on a refused `::set-env::` or `::add-path::` and no longer inherit the last step's identity
1. Docker build, exec and wait errors are returned instead of swallowed, token-only registry credentials work, logs of `--tty` services decode
1. Env files split lines like GitHub on each OS, so a carriage return is kept on Linux and CRLF ends a line on Windows, empty heredoc names or delimiters are rejected
1. Post steps run after their pre step even when main was skipped, composites see undeclared inputs, `runs.args` wins without `with.args` being parsed
1. Docker actions skip evaluating defaults of inputs given in `with` and read `entryPoint` and `args` in any case, a lowercase `dockerfile` is found, undeclared inputs get GitHub's warning
1. A user-set `CI` is kept, `GITHUB_REPOSITORY_ID` and the other missing `GITHUB_*` variables are exported
1. `timeout-minutes` can read the step's own `env`, a step-level `ACTIONS_ALLOW_UNSECURE_COMMANDS: false` beats a job-level `true` while the runner's own opt-in still wins, `pwsh` quotes the script path
1. A step `env:` given as one expression such as `${{ fromJSON(matrix.env) }}` is applied, `hashFiles` fails with GitHub's messages on script errors and after 120 seconds, a job image without node still gets an empty hash
1. A step whose command files cannot be reset fails instead of importing stale ones, a timeout between steps fails the job instead of reporting success
1. One named volume can be mounted at several paths, host-mode PTY steps keep their last output when they fail, host scratch directories are removed when job setup fails
1. Cancelling a step right after it starts kills its whole process tree
1. Cache prefix restores pick the newest entry created within the same second, also on S3, cache commits with a wrong size are rejected
1. Artifact chunks are written at their offset, download URLs are escaped, listing a run without artifacts no longer crashes
1. Poll backoff reaches `fetch_interval_max`, proxy passwords are masked in more forms, secrets are also masked in step names and in their quoted form in the runner log, a failed final report is logged
1. On Windows hosts a differently cased unsecure-commands opt-in or proxy variable counts, and an early cancel no longer leaks a job object handle
1. Interactive `register` prompts for a missing token and stops on SIGTERM, `exec --list --detect-event` no longer panics without events, `config set` works on `cache.s3` keys
Reviewed-on: https://gitea.com/gitea/runner/pulls/1338
Reviewed-by: bircni <[email protected]>
Co-authored-by: silverwind <[email protected]>
130 lines
4.3 KiB
Go
130 lines
4.3 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build !windows && !plan9
|
|
|
|
package process
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// processAlive reports whether pid refers to a still-running process. Signal 0
|
|
// performs error checking without delivering a signal: a nil error (or EPERM)
|
|
// means the process exists, ESRCH means it is gone.
|
|
//
|
|
// On Linux, zombie processes (state Z in /proc/<pid>/stat) appear alive to
|
|
// kill(0) but have already terminated — their corpse lingers until the parent
|
|
// calls wait(). In a Docker container the child may be reparented to a PID 1
|
|
// that does not reap promptly, so we treat zombies as not alive.
|
|
func processAlive(pid int) bool {
|
|
err := syscall.Kill(pid, 0)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
// On Linux /proc is available; check whether the process is a zombie.
|
|
if b, readErr := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)); readErr == nil {
|
|
// Format: "pid (comm) state ..." — state follows the closing ')' of the
|
|
// command name (which may itself contain spaces and parens).
|
|
if _, after, found := strings.CutLast(string(b), ") "); found {
|
|
fields := strings.Fields(after)
|
|
if len(fields) > 0 && fields[0] == "Z" {
|
|
return false // zombie: terminated but not yet reaped
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// TestKillerKillsTree verifies that a process group captured by the killer is
|
|
// terminated together with a child the process spawns afterwards. This mirrors
|
|
// a step or post-task script that launches a child which spawns further
|
|
// processes, where cancelling must take down the whole tree, not just the
|
|
// direct child.
|
|
func TestKillerKillsTree(t *testing.T) {
|
|
dir := t.TempDir()
|
|
pidFile := filepath.Join(dir, "child.pid")
|
|
|
|
// Parent shell backgrounds a long-lived child (writing its PID to a file)
|
|
// and then sleeps. With job control off (non-interactive sh) the backgrounded
|
|
// child stays in the parent's process group, so the group kill must reach it.
|
|
script := fmt.Sprintf(`sleep 600 & echo $! > %q; sleep 600`, pidFile)
|
|
cmd := exec.Command("/bin/sh", "-c", script)
|
|
// Launch as its own process-group leader, exactly like a real process does
|
|
// (see SysProcAttr), so the killer's PGID == the process PID.
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
require.NoError(t, cmd.Start())
|
|
t.Cleanup(func() {
|
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
|
_ = cmd.Wait()
|
|
})
|
|
|
|
killer, err := NewKiller(cmd.Process)
|
|
require.NoError(t, err)
|
|
defer killer.Close()
|
|
|
|
// Wait for the backgrounded child PID to be reported.
|
|
var childPID int
|
|
require.Eventually(t, func() bool {
|
|
b, e := os.ReadFile(pidFile)
|
|
if e != nil {
|
|
return false
|
|
}
|
|
s := strings.TrimSpace(string(b))
|
|
if s == "" {
|
|
return false
|
|
}
|
|
childPID, _ = strconv.Atoi(s)
|
|
return childPID > 0 && processAlive(childPID)
|
|
}, 20*time.Second, 100*time.Millisecond, "child process should start")
|
|
|
|
// Killing the group must terminate both the parent and the backgrounded child.
|
|
require.NoError(t, killer.Kill())
|
|
// Reap the parent so it does not linger as a zombie (which would still report
|
|
// as alive); SIGKILL makes Wait return promptly.
|
|
_ = cmd.Wait()
|
|
|
|
require.Eventually(t, func() bool {
|
|
return !processAlive(childPID)
|
|
}, 20*time.Second, 100*time.Millisecond, "backgrounded child should be terminated")
|
|
}
|
|
|
|
func TestTreeKillCancelBeforeCaptureKillsTree(t *testing.T) {
|
|
stdin, input, err := os.Pipe()
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = stdin.Close() })
|
|
t.Cleanup(func() { _ = input.Close() })
|
|
cmd := exec.CommandContext(t.Context(), "/bin/sh", "-c", "cat <&0 & echo $!; read value")
|
|
cmd.Stdin = stdin
|
|
cmd.SysProcAttr = SysProcAttr("", false)
|
|
output, err := cmd.StdoutPipe()
|
|
require.NoError(t, err)
|
|
treeKill := NewTreeKill(cmd)
|
|
require.NoError(t, cmd.Start())
|
|
t.Cleanup(func() {
|
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
|
_ = cmd.Wait()
|
|
})
|
|
var childPID int
|
|
_, err = fmt.Fscan(output, &childPID)
|
|
require.NoError(t, err)
|
|
require.True(t, processAlive(childPID))
|
|
|
|
require.NoError(t, cmd.Cancel())
|
|
killer, err := treeKill.Capture(cmd.Process)
|
|
require.NoError(t, err)
|
|
defer killer.Close()
|
|
require.Error(t, cmd.Wait())
|
|
require.Eventually(t, func() bool { return !processAlive(childPID) }, 5*time.Second, 10*time.Millisecond)
|
|
}
|