Files
runner/internal/pkg/process/tracked.go
T
silverwind dcba546cb3 fix: align process handling and fix Windows crashes (#1376)
The intermittent Windows test crashes came from step errors formatting a nil `rc.Run`. `fmt` recovers the nil dereference, but on CPUs with AMX the recovered fault overflows into the heap (https://github.com/golang/go/issues/81238). The errors now name the job, and the Windows test VM hides AMX and runs on UTC, so cached boots no longer jump back 8 hours mid-test.

Windows commands now start suspended and resume once they are in their job objects, so no child escapes them. That drops the process scan reading other processes' memory and the last `unsafe` code, which `depguard` now denies. Step processes outlive a crashed runner, and commands are no longer looked up in the runner's working directory, both as with GitHub's runner.

Like GitHub's runner, host jobs on Linux and macOS now end the processes carrying the job's `RUNNER_TRACKING_ID`, and image pulls and Dockerfile action builds are retried.

Reviewed-on: https://gitea.com/gitea/runner/pulls/1376
Reviewed-by: bircni <[email protected]>
Co-authored-by: silverwind <[email protected]>
2026-11-04 16:32:22 +00:00

46 lines
1.0 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
//go:build linux || darwin
package process
import (
"bytes"
"errors"
"fmt"
"os"
"slices"
"syscall"
)
// KillTrackedProcesses kills every process whose environment carries the job's RUNNER_TRACKING_ID, like GitHub's orphan cleanup.
func KillTrackedProcesses(trackingID string) (int, error) {
if trackingID == "" {
return 0, nil
}
pids, err := listPIDs()
if err != nil {
return 0, err
}
marker := []byte("RUNNER_TRACKING_ID=" + trackingID)
self := os.Getpid()
var killed int
var errs []error
for _, pid := range pids {
if pid == self {
continue
}
env, err := environment(pid)
if err != nil || !slices.ContainsFunc(bytes.Split(env, []byte{0}), func(entry []byte) bool { return bytes.Equal(entry, marker) }) {
continue
}
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
errs = append(errs, fmt.Errorf("kill pid %d: %w", pid, err))
continue
}
killed++
}
return killed, errors.Join(errs...)
}