fix: complete P2 host workspace contract
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user