fix: complete P2 host workspace contract
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
//go:build !windows
|
||||
|
||||
package compose
|
||||
import ("os/exec"; "syscall")
|
||||
func configureOwnedProcess(c *exec.Cmd) { c.SysProcAttr=&syscall.SysProcAttr{Setpgid:true} }
|
||||
func terminateOwnedProcess(c *exec.Cmd) { if c.Process!=nil { _=syscall.Kill(-c.Process.Pid, syscall.SIGKILL); _=c.Process.Kill() } }
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func configureOwnedProcess(c *exec.Cmd) { c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} }
|
||||
func registerOwnedProcess(c *exec.Cmd) error { return nil }
|
||||
func releaseOwnedProcess(c *exec.Cmd) {}
|
||||
func terminateOwnedProcess(c *exec.Cmd) {
|
||||
if c.Process != nil {
|
||||
_ = syscall.Kill(-c.Process.Pid, syscall.SIGKILL)
|
||||
_ = c.Process.Kill()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,51 @@
|
||||
//go:build windows
|
||||
|
||||
package compose
|
||||
import ("os/exec"; "syscall")
|
||||
func configureOwnedProcess(c *exec.Cmd) { c.SysProcAttr=&syscall.SysProcAttr{} }
|
||||
func terminateOwnedProcess(c *exec.Cmd) { if c.Process!=nil { _=c.Process.Kill() } }
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var ownedJobs = struct {
|
||||
sync.Mutex
|
||||
m map[*exec.Cmd]windows.Handle
|
||||
}{m: make(map[*exec.Cmd]windows.Handle)}
|
||||
|
||||
func configureOwnedProcess(c *exec.Cmd) {
|
||||
c.SysProcAttr = &windows.SysProcAttr{CreationFlags: windows.CREATE_NEW_PROCESS_GROUP}
|
||||
}
|
||||
func registerOwnedProcess(c *exec.Cmd) error {
|
||||
h, err := windows.CreateJobObject(nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = windows.AssignProcessToJobObject(h, windows.Handle(c.Process.Pid)); err != nil {
|
||||
windows.CloseHandle(h)
|
||||
return err
|
||||
}
|
||||
ownedJobs.Lock()
|
||||
ownedJobs.m[c] = h
|
||||
ownedJobs.Unlock()
|
||||
return nil
|
||||
}
|
||||
func releaseOwnedProcess(c *exec.Cmd) {
|
||||
ownedJobs.Lock()
|
||||
h := ownedJobs.m[c]
|
||||
delete(ownedJobs.m, c)
|
||||
ownedJobs.Unlock()
|
||||
if h != 0 {
|
||||
windows.CloseHandle(h)
|
||||
}
|
||||
}
|
||||
func terminateOwnedProcess(c *exec.Cmd) {
|
||||
ownedJobs.Lock()
|
||||
h := ownedJobs.m[c]
|
||||
ownedJobs.Unlock()
|
||||
if h != 0 {
|
||||
_ = windows.TerminateJobObject(h, 1)
|
||||
} else if c.Process != nil {
|
||||
_ = c.Process.Kill()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,111 +7,154 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Result is the captured output and process exit code for one Docker invocation.
|
||||
var ErrOutputLimit = errors.New("compose output limit exceeded")
|
||||
|
||||
// CaptureLimits bounds each stream independently. Overflow is never retained.
|
||||
type CaptureLimits struct { StdoutBytes int64; StderrBytes int64 }
|
||||
|
||||
type CaptureLimits struct {
|
||||
StdoutBytes int64
|
||||
StderrBytes int64
|
||||
}
|
||||
type Result struct {
|
||||
Stdout string
|
||||
Stderr string
|
||||
ExitCode int
|
||||
}
|
||||
type Runner struct{ binary string }
|
||||
|
||||
// Runner executes the Docker CLI. It never invokes a shell.
|
||||
type Runner struct {
|
||||
binary string
|
||||
}
|
||||
|
||||
// NewRunner returns a runner for binary. An empty binary selects docker from PATH.
|
||||
func NewRunner(binary string) Runner {
|
||||
if binary == "" {
|
||||
binary = "docker"
|
||||
}
|
||||
return Runner{binary: binary}
|
||||
}
|
||||
|
||||
// Run invokes Docker with the supplied argument array and optional standard input.
|
||||
func (r Runner) Run(ctx context.Context, args []string, stdin io.Reader) (Result, error) {
|
||||
command := exec.CommandContext(ctx, r.binary, args...)
|
||||
command.Stdin = stdin
|
||||
var stdout, stderr bytes.Buffer
|
||||
command.Stdout = &stdout
|
||||
command.Stderr = &stderr
|
||||
err := command.Run()
|
||||
result := Result{Stdout: stdout.String(), Stderr: stderr.String()}
|
||||
c := exec.CommandContext(ctx, r.binary, args...)
|
||||
c.Stdin = stdin
|
||||
var out, er bytes.Buffer
|
||||
c.Stdout = &out
|
||||
c.Stderr = &er
|
||||
err := c.Run()
|
||||
res := Result{Stdout: out.String(), Stderr: er.String()}
|
||||
if err == nil {
|
||||
return result, nil
|
||||
return res, nil
|
||||
}
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
result.ExitCode = exitError.ExitCode()
|
||||
return result, err
|
||||
var x *exec.ExitError
|
||||
if errors.As(err, &x) {
|
||||
res.ExitCode = x.ExitCode()
|
||||
return res, err
|
||||
}
|
||||
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) {
|
||||
result.ExitCode = 127
|
||||
return result, fmt.Errorf("%w: %w", exec.ErrNotFound, err)
|
||||
res.ExitCode = 127
|
||||
return res, fmt.Errorf("%w: %w", exec.ErrNotFound, err)
|
||||
}
|
||||
return result, err
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
||||
// RunBounded executes one owned process while retaining at most the configured bytes per stream.
|
||||
// The complete process group is terminated as soon as either stream exceeds its limit.
|
||||
// RunBounded streams each pipe into a preallocated fixed-capacity collector. It owns the
|
||||
// process group and tears it down on the first overflow or cancellation.
|
||||
func (r Runner) RunBounded(ctx context.Context, args []string, stdin io.Reader, limits CaptureLimits) (Result, error) {
|
||||
if limits.StdoutBytes < 0 || limits.StderrBytes < 0 { return Result{}, ErrOutputLimit }
|
||||
command := exec.Command(r.binary, args...)
|
||||
command.Stdin = stdin
|
||||
stdoutPipe, err := command.StdoutPipe(); if err != nil { return Result{}, err }
|
||||
stderrPipe, err := command.StderrPipe(); if err != nil { return Result{}, err }
|
||||
configureOwnedProcess(command)
|
||||
if err := command.Start(); err != nil { return Result{ExitCode: 127}, err }
|
||||
type stream struct { b []byte; overflow bool }
|
||||
var out, er stream
|
||||
if limits.StdoutBytes < 0 || limits.StderrBytes < 0 {
|
||||
return Result{}, ErrOutputLimit
|
||||
}
|
||||
c := exec.Command(r.binary, args...)
|
||||
c.Stdin = stdin
|
||||
op, err := c.StdoutPipe()
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
ep, err := c.StderrPipe()
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
configureOwnedProcess(c)
|
||||
if err = c.Start(); err != nil {
|
||||
return Result{ExitCode: 127}, err
|
||||
}
|
||||
if err = registerOwnedProcess(c); err != nil {
|
||||
terminateOwnedProcess(c)
|
||||
_ = c.Wait()
|
||||
return Result{}, err
|
||||
}
|
||||
defer releaseOwnedProcess(c)
|
||||
type collector struct {
|
||||
buf []byte
|
||||
n int64
|
||||
overflow bool
|
||||
}
|
||||
out := collector{buf: make([]byte, limits.StdoutBytes)}
|
||||
er := collector{buf: make([]byte, limits.StderrBytes)}
|
||||
overflow := make(chan struct{}, 1)
|
||||
var wg sync.WaitGroup; wg.Add(2)
|
||||
read := func(rd io.Reader, max int64, target *stream) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
read := func(rd io.Reader, max int64, col *collector) {
|
||||
defer wg.Done()
|
||||
buf := make([]byte, 32*1024)
|
||||
chunk := make([]byte, 32*1024)
|
||||
for {
|
||||
n, readErr := rd.Read(buf)
|
||||
n, e := rd.Read(chunk)
|
||||
if n > 0 {
|
||||
remain := max - int64(len(target.b))
|
||||
if remain > 0 { take := int64(n); if take > remain { take = remain }; target.b = append(target.b, buf[:take]...) }
|
||||
remain := max - col.n
|
||||
if remain > 0 {
|
||||
take := int64(n)
|
||||
if take > remain {
|
||||
take = remain
|
||||
}
|
||||
copy(col.buf[col.n:col.n+take], chunk[:take])
|
||||
col.n += take
|
||||
}
|
||||
if int64(n) > remain {
|
||||
target.overflow = true
|
||||
select { case overflow <- struct{}{}: default: }
|
||||
col.overflow = true
|
||||
select {
|
||||
case overflow <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
if readErr != nil { return }
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
go read(stdoutPipe, limits.StdoutBytes, &out); go read(stderrPipe, limits.StderrBytes, &er)
|
||||
finished := make(chan error, 1); go func() { finished <- command.Wait() }()
|
||||
go read(op, limits.StdoutBytes, &out)
|
||||
go read(ep, limits.StderrBytes, &er)
|
||||
waited := make(chan error, 1)
|
||||
go func() { waited <- c.Wait() }()
|
||||
var waitErr error
|
||||
var cancelled error
|
||||
select {
|
||||
case waitErr = <-finished:
|
||||
// The parent may exit while a descendant inherited stdout/stderr; tear down the owned group.
|
||||
terminateOwnedProcess(command)
|
||||
case waitErr = <-waited:
|
||||
terminateOwnedProcess(c)
|
||||
case <-overflow:
|
||||
terminateOwnedProcess(command); waitErr = <-finished
|
||||
terminateOwnedProcess(c)
|
||||
waitErr = <-waited
|
||||
case <-ctx.Done():
|
||||
terminateOwnedProcess(command); waitErr = <-finished
|
||||
cancelled = ctx.Err()
|
||||
terminateOwnedProcess(c)
|
||||
waitErr = <-waited
|
||||
}
|
||||
wg.Wait()
|
||||
result := Result{Stdout: string(out.b), Stderr: string(er.b)}
|
||||
if out.overflow || er.overflow { return result, ErrOutputLimit }
|
||||
if waitErr == nil { return result, nil }
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(waitErr, &exitErr) { result.ExitCode = exitErr.ExitCode(); return result, waitErr }
|
||||
if errors.Is(waitErr, exec.ErrNotFound) || errors.Is(waitErr, os.ErrNotExist) { result.ExitCode = 127; return result, fmt.Errorf("%w: %w", exec.ErrNotFound, waitErr) }
|
||||
if ctx.Err() != nil { return result, ctx.Err() }
|
||||
return result, waitErr
|
||||
res := Result{Stdout: string(out.buf[:out.n]), Stderr: string(er.buf[:er.n])}
|
||||
if out.overflow || er.overflow {
|
||||
return res, ErrOutputLimit
|
||||
}
|
||||
if cancelled != nil {
|
||||
return res, cancelled
|
||||
}
|
||||
if waitErr == nil {
|
||||
return res, nil
|
||||
}
|
||||
var ex *exec.ExitError
|
||||
if errors.As(waitErr, &ex) {
|
||||
res.ExitCode = ex.ExitCode()
|
||||
return res, waitErr
|
||||
}
|
||||
if errors.Is(waitErr, exec.ErrNotFound) || errors.Is(waitErr, os.ErrNotExist) {
|
||||
res.ExitCode = 127
|
||||
return res, fmt.Errorf("%w: %w", exec.ErrNotFound, waitErr)
|
||||
}
|
||||
return res, waitErr
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRunnerPassesEachArgumentWithoutShellSplitting(t *testing.T) {
|
||||
@@ -65,18 +67,78 @@ func writeExecutable(t *testing.T, contents string) string {
|
||||
return path
|
||||
}
|
||||
|
||||
|
||||
func TestRunnerBoundedCapturesExactlyAtLimit(t *testing.T) {
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nprintf 1234567890\nprintf error12345 >&2\n"))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if err != nil { t.Fatal(err) }
|
||||
if result.Stdout != "1234567890" || result.Stderr != "error12345" { t.Fatalf("bounded result = %#v", result) }
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Stdout != "1234567890" || result.Stderr != "error12345" {
|
||||
t.Fatalf("bounded result = %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedRejectsOneByteOverEachStream(t *testing.T) {
|
||||
for _, script := range []string{"#!/bin/sh\nprintf 12345678901\n", "#!/bin/sh\nprintf 12345678901 >&2\n"} {
|
||||
runner := NewRunner(writeExecutable(t, script)); result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if !errors.Is(err, ErrOutputLimit) { t.Fatalf("err = %v result=%#v", err, result) }
|
||||
if len(result.Stdout) > 10 || len(result.Stderr) > 10 { t.Fatalf("retained overflow: %#v", result) }
|
||||
runner := NewRunner(writeExecutable(t, script))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v result=%#v", err, result)
|
||||
}
|
||||
if len(result.Stdout) > 10 || len(result.Stderr) > 10 {
|
||||
t.Fatalf("retained overflow: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedTerminatesInfiniteStreams(t *testing.T) {
|
||||
for _, script := range []string{"#!/bin/sh\nwhile :; do printf x; done", "#!/bin/sh\nwhile :; do printf x >&2; done"} {
|
||||
runner := NewRunner(writeExecutable(t, script))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 1024, StderrBytes: 1024})
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v result = %#v", err, result)
|
||||
}
|
||||
if len(result.Stdout) > 1024 || len(result.Stderr) > 1024 {
|
||||
t.Fatalf("retained overflow: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedCancellationWinsOverChildKill(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nsleep 30\n"))
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runner.RunBounded(ctx, nil, nil, CaptureLimits{StdoutBytes: 32, StderrBytes: 32})
|
||||
done <- err
|
||||
}()
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("cancellation did not terminate process")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedTerminatesGrandchildHoldingPipe(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("shell process-group fixture is Unix-specific")
|
||||
}
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\n(sleep 30; printf survivor) &\nwhile :; do printf x; done\n"))
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 64, StderrBytes: 64})
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("grandchild kept owned pipe alive")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user