chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,412 @@
|
||||
// shellctl-runner is the Go replacement for the previously generated bash+python
|
||||
// runner script. It is invoked by tmux as:
|
||||
//
|
||||
// shellctl-runner <job_dir> <job_id> <cwd> [pty|stdio]
|
||||
//
|
||||
// The binary operates in two process roles:
|
||||
//
|
||||
// 1. Parent mode (default): waits for start-gate, loads env, forks child,
|
||||
// waits for exit, writes exit artifacts.
|
||||
// 2. Child mode (--exec flag): applies Landlock restrictions, then exec's
|
||||
// the user script.
|
||||
//
|
||||
// This eliminates the bash/python dependency chain and integrates Landlock
|
||||
// directly.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/langgenius/dify/dify-agent-runtime/internal/cmdutil"
|
||||
"github.com/langgenius/dify/dify-agent-runtime/internal/envvar"
|
||||
"github.com/langgenius/dify/dify-agent-runtime/internal/jobmode"
|
||||
"github.com/langgenius/dify/dify-agent-runtime/internal/landlock"
|
||||
)
|
||||
|
||||
// startGatePollInterval is how often the parent runner checks for the
|
||||
// start-gate handshake file. It is a cheap local stat on the same path the
|
||||
// server has just written, so a fine interval keeps per-job startup latency
|
||||
// low without meaningful cost.
|
||||
const startGatePollInterval = 5 * time.Millisecond
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "--exec" {
|
||||
childMode()
|
||||
return
|
||||
}
|
||||
parentMode()
|
||||
}
|
||||
|
||||
// parentMode is the entry point when called by tmux.
|
||||
// Args: shellctl-runner <job_dir> <job_id> <cwd> [pty|stdio]
|
||||
func parentMode() {
|
||||
if len(os.Args) < 4 {
|
||||
cmdutil.HandleError(fmt.Errorf("bad args"), 125, "usage: shellctl-runner <job_dir> <job_id> <cwd> [pty|stdio]")
|
||||
}
|
||||
|
||||
jobDir := os.Args[1]
|
||||
// jobID := os.Args[2] // unused in parent but passed for compat
|
||||
cwd := os.Args[3]
|
||||
modeRaw := ""
|
||||
if len(os.Args) >= 5 {
|
||||
modeRaw = os.Args[4]
|
||||
}
|
||||
mode, err := jobmode.Parse(modeRaw)
|
||||
cmdutil.HandleError(err, 125, "parse job mode")
|
||||
|
||||
scriptPath := filepath.Join(jobDir, "script")
|
||||
envPath := filepath.Join(jobDir, ".job-env.json")
|
||||
startGate := filepath.Join(jobDir, "start-gate")
|
||||
|
||||
// Wait for start-gate.
|
||||
for {
|
||||
if _, err := os.Stat(startGate); err == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(startGatePollInterval)
|
||||
}
|
||||
|
||||
// Load environment overlay from JSON.
|
||||
env := os.Environ()
|
||||
// Remove internal shellctl vars from inherited env.
|
||||
env = filterEnv(env, []string{
|
||||
"TMUX",
|
||||
"SHELLCTL_STATE_DIR",
|
||||
"SHELLCTL_RUNTIME_DIR",
|
||||
"SHELLCTL_TMUX_SOCKET",
|
||||
"SHELLCTL_RUNNER",
|
||||
"SHELLCTL_AUTH_TOKEN",
|
||||
})
|
||||
|
||||
envOverlay := loadEnvJSON(envPath)
|
||||
env = mergeEnv(env, envOverlay)
|
||||
env = mergeEnv(env, map[string]string{
|
||||
"TMPDIR": cwd,
|
||||
"TMP": cwd,
|
||||
"TEMP": cwd,
|
||||
})
|
||||
|
||||
// Ensure HOME exists.
|
||||
home := envGet(env, "HOME")
|
||||
if home != "" {
|
||||
cmdutil.HandleError(os.MkdirAll(home, 0755), 125, "mkdir HOME %s", home)
|
||||
}
|
||||
|
||||
// Determine if path isolation is enabled.
|
||||
enableIsolation := envvar.PathIsolationEnabled()
|
||||
|
||||
// Build child command: re-exec self in --exec mode.
|
||||
self, _ := os.Executable()
|
||||
childArgs := []string{"--exec", scriptPath, cwd}
|
||||
if enableIsolation {
|
||||
childArgs = append([]string{"--exec", "--landlock"}, childArgs[1:]...)
|
||||
}
|
||||
|
||||
cmd := exec.Command(self, childArgs...)
|
||||
cmd.Env = env
|
||||
cmd.Dir = cwd
|
||||
|
||||
// Forward signals to child.
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||
go func() {
|
||||
for sig := range sigCh {
|
||||
if cmd.Process != nil {
|
||||
_ = cmd.Process.Signal(sig)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
exitCode := runCommandAndRecordExit(cmd, jobDir, mode)
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
// runCommandAndRecordExit publishes the existing runner artifacts after the
|
||||
// child path returns. In stdio mode that return includes both stream drains;
|
||||
// PTY mode still relies on its separate pipe-drain finalizer.
|
||||
func runCommandAndRecordExit(cmd *exec.Cmd, jobDir string, mode jobmode.Mode) int {
|
||||
var exitCode int
|
||||
if mode == jobmode.Stdio {
|
||||
exitCode = runStdio(cmd, jobDir)
|
||||
} else {
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
exitCode = runPTY(cmd)
|
||||
}
|
||||
|
||||
endedAt := time.Now().UTC().Format("2006-01-02T15:04:05Z")
|
||||
|
||||
writeAtomic(filepath.Join(jobDir, "runner-exit-code"), fmt.Sprintf("%d", exitCode))
|
||||
writeAtomic(filepath.Join(jobDir, "runner-ended-at"), endedAt)
|
||||
return exitCode
|
||||
}
|
||||
|
||||
func runPTY(cmd *exec.Cmd) int {
|
||||
return commandExitCode(cmd.Run())
|
||||
}
|
||||
|
||||
// runStdio captures stdout and stderr independently and does not return until
|
||||
// both streams reach EOF and their files are closed.
|
||||
func runStdio(cmd *exec.Cmd, jobDir string) int {
|
||||
outputFile, err := openCaptureFile(filepath.Join(jobDir, "output.log"))
|
||||
if err != nil {
|
||||
return runnerError("open stdout capture", err)
|
||||
}
|
||||
stderrFile, err := openCaptureFile(filepath.Join(jobDir, "stderr.log"))
|
||||
if err != nil {
|
||||
_ = outputFile.Close()
|
||||
return runnerError("open stderr capture", err)
|
||||
}
|
||||
|
||||
stdoutReader, stdoutWriter, err := os.Pipe()
|
||||
if err != nil {
|
||||
_ = outputFile.Close()
|
||||
_ = stderrFile.Close()
|
||||
return runnerError("create stdout pipe", err)
|
||||
}
|
||||
stderrReader, stderrWriter, err := os.Pipe()
|
||||
if err != nil {
|
||||
_ = stdoutReader.Close()
|
||||
_ = stdoutWriter.Close()
|
||||
_ = outputFile.Close()
|
||||
_ = stderrFile.Close()
|
||||
return runnerError("create stderr pipe", err)
|
||||
}
|
||||
stdinFile, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
_ = stdoutReader.Close()
|
||||
_ = stdoutWriter.Close()
|
||||
_ = stderrReader.Close()
|
||||
_ = stderrWriter.Close()
|
||||
_ = outputFile.Close()
|
||||
_ = stderrFile.Close()
|
||||
return runnerError("open stdin", err)
|
||||
}
|
||||
|
||||
cmd.Stdin = stdinFile
|
||||
cmd.Stdout = stdoutWriter
|
||||
cmd.Stderr = stderrWriter
|
||||
|
||||
var captureErrors []error
|
||||
var captureErrorsMu sync.Mutex
|
||||
recordError := func(operation string, err error) {
|
||||
if err != nil {
|
||||
captureErrorsMu.Lock()
|
||||
captureErrors = append(captureErrors, fmt.Errorf("%s: %w", operation, err))
|
||||
captureErrorsMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
var drains sync.WaitGroup
|
||||
drains.Add(2)
|
||||
go func() {
|
||||
defer drains.Done()
|
||||
_, copyErr := io.Copy(outputFile, stdoutReader)
|
||||
recordError("copy stdout", copyErr)
|
||||
recordError("close stdout reader", stdoutReader.Close())
|
||||
}()
|
||||
go func() {
|
||||
defer drains.Done()
|
||||
_, copyErr := io.Copy(stderrFile, stderrReader)
|
||||
recordError("copy stderr", copyErr)
|
||||
recordError("close stderr reader", stderrReader.Close())
|
||||
}()
|
||||
|
||||
startErr := cmd.Start()
|
||||
recordError("close stdout writer", stdoutWriter.Close())
|
||||
recordError("close stderr writer", stderrWriter.Close())
|
||||
recordError("close stdin", stdinFile.Close())
|
||||
|
||||
var waitErr error
|
||||
if startErr != nil {
|
||||
recordError("start child", startErr)
|
||||
} else {
|
||||
waitErr = cmd.Wait()
|
||||
}
|
||||
|
||||
// Descendants may retain either write end after the direct child exits. In
|
||||
// that case the runner intentionally remains alive until both reach EOF.
|
||||
drains.Wait()
|
||||
recordError("close stdout capture", outputFile.Close())
|
||||
recordError("close stderr capture", stderrFile.Close())
|
||||
|
||||
for _, captureErr := range captureErrors {
|
||||
fmt.Fprintf(os.Stderr, "shellctl-runner: %v\n", captureErr)
|
||||
}
|
||||
if len(captureErrors) > 0 {
|
||||
return 125
|
||||
}
|
||||
return commandExitCode(waitErr)
|
||||
}
|
||||
|
||||
func openCaptureFile(path string) (*os.File, error) {
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := file.Chmod(0600); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func runnerError(operation string, err error) int {
|
||||
fmt.Fprintf(os.Stderr, "shellctl-runner: %s: %v\n", operation, err)
|
||||
return 125
|
||||
}
|
||||
|
||||
func commandExitCode(err error) int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return exitErr.ExitCode()
|
||||
}
|
||||
return 125
|
||||
}
|
||||
|
||||
// childMode applies Landlock (if --landlock flag) and exec's the user script.
|
||||
// Args: shellctl-runner --exec [--landlock] <script_path> <cwd>
|
||||
func childMode() {
|
||||
args := os.Args[2:] // skip program name and "--exec"
|
||||
applyLandlockFlag := false
|
||||
|
||||
if len(args) > 0 && args[0] == "--landlock" {
|
||||
applyLandlockFlag = true
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
if len(args) < 2 {
|
||||
cmdutil.HandleError(fmt.Errorf("bad args"), 125, "--exec: missing script_path and cwd")
|
||||
}
|
||||
|
||||
scriptPath := args[0]
|
||||
cwd := args[1]
|
||||
|
||||
// chdir
|
||||
cmdutil.HandleError(os.Chdir(cwd), 111, "chdir %s", cwd)
|
||||
|
||||
// Determine argv for exec.
|
||||
argv := buildExecArgv(scriptPath)
|
||||
|
||||
// Resolve binary before Landlock (PATH search).
|
||||
binary, err := exec.LookPath(argv[0])
|
||||
cmdutil.HandleError(err, 127, "%s: not found", argv[0])
|
||||
|
||||
// Apply Landlock if requested.
|
||||
if applyLandlockFlag {
|
||||
home := os.Getenv("HOME")
|
||||
jobDir := filepath.Dir(scriptPath)
|
||||
cfg := landlock.ConfigFromEnv(home, cwd, jobDir)
|
||||
cmdutil.HandleError(landlock.Restrict(cfg), 125, "apply Landlock restrictions")
|
||||
}
|
||||
|
||||
// exec replaces the current process.
|
||||
cmdutil.HandleError(syscall.Exec(binary, argv, os.Environ()), 126, "exec %s", binary)
|
||||
}
|
||||
|
||||
// buildExecArgv determines whether the script has a shebang or needs sh.
|
||||
func buildExecArgv(scriptPath string) []string {
|
||||
f, err := os.Open(scriptPath)
|
||||
if err != nil {
|
||||
return []string{"sh", scriptPath}
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
buf := make([]byte, 2)
|
||||
n, _ := f.Read(buf)
|
||||
if n == 2 && string(buf) == "#!" {
|
||||
// Has shebang — make executable and run directly.
|
||||
_ = os.Chmod(scriptPath, 0755)
|
||||
return []string{scriptPath}
|
||||
}
|
||||
return []string{"sh", scriptPath}
|
||||
}
|
||||
|
||||
// loadEnvJSON reads a JSON object from path and returns key=value pairs.
|
||||
func loadEnvJSON(path string) map[string]string {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
cmdutil.HandleError(err, 125, "read env json %s", path)
|
||||
}
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
cmdutil.HandleError(err, 125, "parse env json %s", path)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// filterEnv removes entries with the given key prefixes from env.
|
||||
func filterEnv(env []string, remove []string) []string {
|
||||
result := env[:0]
|
||||
for _, e := range env {
|
||||
skip := false
|
||||
for _, r := range remove {
|
||||
if strings.HasPrefix(e, r+"=") {
|
||||
skip = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skip {
|
||||
result = append(result, e)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// mergeEnv applies overlay key=value pairs onto the env slice.
|
||||
func mergeEnv(env []string, overlay map[string]string) []string {
|
||||
for k, v := range overlay {
|
||||
found := false
|
||||
prefix := k + "="
|
||||
for i, e := range env {
|
||||
if strings.HasPrefix(e, prefix) {
|
||||
env[i] = prefix + v
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
env = append(env, prefix+v)
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
// envGet retrieves a value from a []string env slice.
|
||||
func envGet(env []string, key string) string {
|
||||
prefix := key + "="
|
||||
for _, e := range env {
|
||||
if strings.HasPrefix(e, prefix) {
|
||||
return e[len(prefix):]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// writeAtomic writes value to dest via a temp file + rename.
|
||||
func writeAtomic(dest, value string) {
|
||||
tmp := fmt.Sprintf("%s.tmp.%d", dest, os.Getpid())
|
||||
if err := os.WriteFile(tmp, []byte(value+"\n"), 0644); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "shellctl-runner: write %s: %v\n", tmp, err)
|
||||
return
|
||||
}
|
||||
if err := os.Rename(tmp, dest); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "shellctl-runner: rename %s -> %s: %v\n", tmp, dest, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/langgenius/dify/dify-agent-runtime/internal/jobmode"
|
||||
)
|
||||
|
||||
func TestRunnerLandlockFailureDoesNotExecuteScript(test *testing.T) {
|
||||
runnerPath := filepath.Join(test.TempDir(), "shellctl-runner")
|
||||
build := exec.Command("go", "build", "-o", runnerPath, ".")
|
||||
if output, err := build.CombinedOutput(); err != nil {
|
||||
test.Fatalf("build runner: %v\n%s", err, output)
|
||||
}
|
||||
|
||||
for _, mode := range []string{"pty", "stdio"} {
|
||||
for _, isolation := range []string{"true", "false"} {
|
||||
test.Run(mode+"/isolation="+isolation, func(test *testing.T) {
|
||||
jobDir := test.TempDir()
|
||||
invalidDirectory := filepath.Join(jobDir, "regular-file")
|
||||
for name, content := range map[string]string{
|
||||
"regular-file": "not a directory",
|
||||
"script": "printf executed > script-executed",
|
||||
"start-gate": "",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(jobDir, name), []byte(content), 0600); err != nil {
|
||||
test.Fatal(err)
|
||||
}
|
||||
}
|
||||
cmd := exec.Command(runnerPath, jobDir, "test-job", jobDir, mode)
|
||||
cmd.Env = mergeEnv(os.Environ(), map[string]string{
|
||||
"HOME": jobDir,
|
||||
"SHELLCTL_ENABLE_PATH_ISOLATION": isolation,
|
||||
"SHELLCTL_LANDLOCK_RW_PATHS": invalidDirectory,
|
||||
"SHELLCTL_LANDLOCK_RO_PATHS": "/usr,/bin,/lib,/lib64",
|
||||
"SHELLCTL_LANDLOCK_RW_DEV_PATHS": "/dev/null",
|
||||
})
|
||||
output, err := cmd.CombinedOutput()
|
||||
wantExitCode := 125
|
||||
if isolation == "false" {
|
||||
wantExitCode = 0
|
||||
}
|
||||
if got := commandExitCode(err); got != wantExitCode {
|
||||
test.Fatalf("exit code = %d, want %d: %s", got, wantExitCode, output)
|
||||
}
|
||||
exitArtifact, err := os.ReadFile(filepath.Join(jobDir, "runner-exit-code"))
|
||||
if err != nil || strings.TrimSpace(string(exitArtifact)) != strconv.Itoa(wantExitCode) {
|
||||
test.Fatalf("exit artifact = %q, err = %v", exitArtifact, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(jobDir, "runner-ended-at")); err != nil {
|
||||
test.Fatal(err)
|
||||
}
|
||||
_, markerErr := os.Stat(filepath.Join(jobDir, "script-executed"))
|
||||
if isolation == "true" {
|
||||
if !os.IsNotExist(markerErr) {
|
||||
test.Fatalf("user script side effect exists: %v", markerErr)
|
||||
}
|
||||
if mode == "stdio" {
|
||||
output, err = os.ReadFile(filepath.Join(jobDir, "stderr.log"))
|
||||
if err != nil {
|
||||
test.Fatal(err)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(string(output), "apply Landlock restrictions") {
|
||||
test.Fatalf("missing Landlock diagnostic: %s", output)
|
||||
}
|
||||
} else if markerErr != nil {
|
||||
test.Fatalf("disabled isolation did not execute script: %v", markerErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStdioCapturesCompleteSeparatedStreams(t *testing.T) {
|
||||
jobDir := t.TempDir()
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestStdioHelperProcess$")
|
||||
cmd.Env = append(os.Environ(), "SHELLCTL_STDIO_HELPER=large-output")
|
||||
|
||||
if exitCode := runStdio(cmd, jobDir); exitCode != 0 {
|
||||
t.Fatalf("runStdio exit code = %d, want 0", exitCode)
|
||||
}
|
||||
|
||||
wantStdout := bytes.Repeat([]byte("stdout-payload\n"), 16*1024)
|
||||
wantStderr := bytes.Repeat([]byte("stderr-payload\n"), 16*1024)
|
||||
gotStdout, err := os.ReadFile(filepath.Join(jobDir, "output.log"))
|
||||
if err != nil {
|
||||
t.Fatalf("read output.log: %v", err)
|
||||
}
|
||||
gotStderr, err := os.ReadFile(filepath.Join(jobDir, "stderr.log"))
|
||||
if err != nil {
|
||||
t.Fatalf("read stderr.log: %v", err)
|
||||
}
|
||||
if !bytes.Equal(gotStdout, wantStdout) {
|
||||
t.Errorf("stdout capture length = %d, want %d", len(gotStdout), len(wantStdout))
|
||||
}
|
||||
if !bytes.Equal(gotStderr, wantStderr) {
|
||||
t.Errorf("stderr capture length = %d, want %d", len(gotStderr), len(wantStderr))
|
||||
}
|
||||
for _, name := range []string{"output.log", "stderr.log"} {
|
||||
info, err := os.Stat(filepath.Join(jobDir, name))
|
||||
if err != nil {
|
||||
t.Fatalf("stat %s: %v", name, err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != 0600 {
|
||||
t.Errorf("%s permissions = %#o, want 0600", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStdioUsesNonTTYStreams(t *testing.T) {
|
||||
jobDir := t.TempDir()
|
||||
cmd := exec.Command("sh", "-c", `if [ -t 0 ] || [ -t 1 ] || [ -t 2 ]; then exit 1; fi; printf 'stdout-only'; printf 'warning' >&2`)
|
||||
|
||||
if exitCode := runStdio(cmd, jobDir); exitCode != 0 {
|
||||
t.Fatalf("runStdio exit code = %d, want 0", exitCode)
|
||||
}
|
||||
stdout, err := os.ReadFile(filepath.Join(jobDir, "output.log"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stderr, err := os.ReadFile(filepath.Join(jobDir, "stderr.log"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(stdout) != "stdout-only" {
|
||||
t.Errorf("stdout = %q, want stdout-only", stdout)
|
||||
}
|
||||
if string(stderr) != "warning" {
|
||||
t.Errorf("stderr = %q, want warning", stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStdioWaitsForBothDescendantStreamsBeforePublishingExit(t *testing.T) {
|
||||
jobDir := t.TempDir()
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestStdioHelperProcess$")
|
||||
cmd.Env = mergeEnv(os.Environ(), map[string]string{
|
||||
"SHELLCTL_STDIO_HELPER": "spawn-descendants",
|
||||
"SHELLCTL_STDIO_JOB_DIR": jobDir,
|
||||
})
|
||||
done := make(chan struct{})
|
||||
var exitCode int
|
||||
go func() {
|
||||
exitCode = runCommandAndRecordExit(cmd, jobDir, jobmode.Stdio)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
stdoutRelease := filepath.Join(jobDir, "release-stdout")
|
||||
stderrRelease := filepath.Join(jobDir, "release-stderr")
|
||||
t.Cleanup(func() {
|
||||
_ = os.WriteFile(stdoutRelease, nil, 0600)
|
||||
_ = os.WriteFile(stderrRelease, nil, 0600)
|
||||
for _, name := range []string{"stdout-closed", "stderr-closed"} {
|
||||
if !waitForPath(filepath.Join(jobDir, name), 5*time.Second) {
|
||||
t.Errorf("cleanup timed out waiting for %s", name)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("cleanup timed out waiting for runner completion")
|
||||
}
|
||||
})
|
||||
|
||||
waitForTestFile(t, filepath.Join(jobDir, "direct-child-exited"))
|
||||
assertRunnerRemainsIncomplete(t, done)
|
||||
assertExitArtifactsAbsent(t, jobDir)
|
||||
|
||||
if err := os.WriteFile(stdoutRelease, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waitForTestFile(t, filepath.Join(jobDir, "stdout-closed"))
|
||||
assertRunnerRemainsIncomplete(t, done)
|
||||
assertExitArtifactsAbsent(t, jobDir)
|
||||
|
||||
if err := os.WriteFile(stderrRelease, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waitForTestFile(t, filepath.Join(jobDir, "stderr-closed"))
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
if exitCode != 0 {
|
||||
t.Fatalf("exit code = %d, want 0", exitCode)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("runner did not complete after both descendant streams reached EOF")
|
||||
}
|
||||
waitForTestFile(t, filepath.Join(jobDir, "runner-exit-code"))
|
||||
waitForTestFile(t, filepath.Join(jobDir, "runner-ended-at"))
|
||||
|
||||
stdout, err := os.ReadFile(filepath.Join(jobDir, "output.log"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stderr, err := os.ReadFile(filepath.Join(jobDir, "stderr.log"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(stdout) != "stdout-tail" {
|
||||
t.Errorf("stdout = %q, want stdout-tail", stdout)
|
||||
}
|
||||
if string(stderr) != "stderr-tail" {
|
||||
t.Errorf("stderr = %q, want stderr-tail", stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStdioPreservesNonZeroExitCode(t *testing.T) {
|
||||
jobDir := t.TempDir()
|
||||
cmd := exec.Command("sh", "-c", "exit 23")
|
||||
|
||||
if exitCode := runCommandAndRecordExit(cmd, jobDir, jobmode.Stdio); exitCode != 23 {
|
||||
t.Fatalf("exit code = %d, want 23", exitCode)
|
||||
}
|
||||
exitCodeArtifact, err := os.ReadFile(filepath.Join(jobDir, "runner-exit-code"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(exitCodeArtifact) != "23\n" {
|
||||
t.Errorf("runner-exit-code = %q, want 23", exitCodeArtifact)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStdioHelperProcess(t *testing.T) {
|
||||
switch os.Getenv("SHELLCTL_STDIO_HELPER") {
|
||||
case "":
|
||||
return
|
||||
case "large-output":
|
||||
stdout := bytes.Repeat([]byte("stdout-payload\n"), 16*1024)
|
||||
stderr := bytes.Repeat([]byte("stderr-payload\n"), 16*1024)
|
||||
_, _ = os.Stdout.Write(stdout)
|
||||
_, _ = os.Stderr.Write(stderr)
|
||||
os.Exit(0)
|
||||
case "spawn-descendants":
|
||||
spawnStdioDescendants()
|
||||
case "hold-stdout", "hold-stderr":
|
||||
holdStdioStream(os.Getenv("SHELLCTL_STDIO_HELPER"))
|
||||
default:
|
||||
os.Exit(125)
|
||||
}
|
||||
}
|
||||
|
||||
func spawnStdioDescendants() {
|
||||
jobDir := os.Getenv("SHELLCTL_STDIO_JOB_DIR")
|
||||
parentPID := strconv.Itoa(os.Getpid())
|
||||
stdoutHolder := exec.Command(os.Args[0], "-test.run=^TestStdioHelperProcess$")
|
||||
stdoutHolder.Env = mergeEnv(os.Environ(), map[string]string{
|
||||
"SHELLCTL_STDIO_HELPER": "hold-stdout",
|
||||
"SHELLCTL_STDIO_JOB_DIR": jobDir,
|
||||
"SHELLCTL_STDIO_PARENT_PID": parentPID,
|
||||
})
|
||||
stdoutHolder.Stdout = os.Stdout
|
||||
if err := stdoutHolder.Start(); err != nil {
|
||||
os.Exit(125)
|
||||
}
|
||||
|
||||
stderrHolder := exec.Command(os.Args[0], "-test.run=^TestStdioHelperProcess$")
|
||||
stderrHolder.Env = mergeEnv(os.Environ(), map[string]string{
|
||||
"SHELLCTL_STDIO_HELPER": "hold-stderr",
|
||||
"SHELLCTL_STDIO_JOB_DIR": jobDir,
|
||||
})
|
||||
stderrHolder.Stderr = os.Stderr
|
||||
if err := stderrHolder.Start(); err != nil {
|
||||
os.Exit(125)
|
||||
}
|
||||
|
||||
if !waitForPath(filepath.Join(jobDir, "stdout-ready"), 5*time.Second) ||
|
||||
!waitForPath(filepath.Join(jobDir, "stderr-ready"), 5*time.Second) {
|
||||
os.Exit(125)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func holdStdioStream(mode string) {
|
||||
jobDir := os.Getenv("SHELLCTL_STDIO_JOB_DIR")
|
||||
streamName := mode[len("hold-"):]
|
||||
if err := os.WriteFile(filepath.Join(jobDir, streamName+"-ready"), nil, 0600); err != nil {
|
||||
os.Exit(125)
|
||||
}
|
||||
if mode == "hold-stdout" {
|
||||
parentPID, err := strconv.Atoi(os.Getenv("SHELLCTL_STDIO_PARENT_PID"))
|
||||
if err != nil || !waitForProcessExit(parentPID, 5*time.Second) {
|
||||
os.Exit(125)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(jobDir, "direct-child-exited"), nil, 0600); err != nil {
|
||||
os.Exit(125)
|
||||
}
|
||||
}
|
||||
|
||||
if !waitForPath(filepath.Join(jobDir, "release-"+streamName), 5*time.Second) {
|
||||
os.Exit(125)
|
||||
}
|
||||
if mode == "hold-stdout" {
|
||||
_, _ = os.Stdout.WriteString("stdout-tail")
|
||||
_ = os.Stdout.Close()
|
||||
} else {
|
||||
_, _ = os.Stderr.WriteString("stderr-tail")
|
||||
_ = os.Stderr.Close()
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(jobDir, streamName+"-closed"), nil, 0600); err != nil {
|
||||
os.Exit(125)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func waitForProcessExit(pid int, timeout time.Duration) bool {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if err := syscall.Kill(pid, 0); err == syscall.ESRCH {
|
||||
return true
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func waitForPath(path string, timeout time.Duration) bool {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return true
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func waitForTestFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if !waitForPath(path, 5*time.Second) {
|
||||
t.Fatalf("timed out waiting for %s", filepath.Base(path))
|
||||
}
|
||||
}
|
||||
|
||||
func assertRunnerRemainsIncomplete(t *testing.T, done <-chan struct{}) {
|
||||
t.Helper()
|
||||
timer := time.NewTimer(100 * time.Millisecond)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("runner completed before both streams reached EOF")
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
|
||||
func assertExitArtifactsAbsent(t *testing.T, jobDir string) {
|
||||
t.Helper()
|
||||
for _, name := range []string{"runner-exit-code", "runner-ended-at"} {
|
||||
if _, err := os.Stat(filepath.Join(jobDir, name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s became visible before both streams reached EOF: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user