mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-05 13:01:50 +08:00
fix(sandbox,traffic): keep sandbox command output and free scan dial slots
Docker exec no longer wraps commands in setsid, which exited immediately and cut stdout after about two seconds. Netstack releases the pending-dial slot once the handshake finishes, raises the in-flight limits, and treats local file-descriptor exhaustion as a silent drop. The gateway container gets a higher open-file limit.
This commit is contained in:
1 parent
ee5ac6e6dd
commit
8bac4960e7
6 files changed
+346
-24
No files matched your search
+50
-10
@@ -1192,10 +1192,7 @@ func (m *Manager) Exec(ctx context.Context, t Target, spec ExecSpec) ExecResult
|
||||
var pgidFile string
|
||||
if spec.KillOnAbort && len(cmd) > 0 {
|
||||
pgidFile = "/tmp/.artex-pgid-" + randomToken(8)
|
||||
// Wrap in a new session (setsid) recording its PGID so timeout/cancel
|
||||
// can kill the entire child process tree inside the container.
|
||||
wrapped := fmt.Sprintf(`echo $$ > %q; trap 'rm -f %q' EXIT; exec "$@"`, pgidFile, pgidFile)
|
||||
cmd = append([]string{"setsid", "sh", "-c", wrapped, "--"}, spec.Cmd...)
|
||||
cmd = wrapPGIDCommand(pgidFile, spec.Cmd)
|
||||
}
|
||||
|
||||
execID, err := m.docker.ExecCreate(runCtx, containerID, dockerExecCreateReq{
|
||||
@@ -1271,10 +1268,19 @@ func (m *Manager) Exec(ctx context.Context, t Target, spec ExecSpec) ExecResult
|
||||
if !timedOut && ctx.Err() == nil {
|
||||
inspectCtx, icancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer icancel()
|
||||
if info, ierr := m.docker.ExecInspect(inspectCtx, execID); ierr == nil {
|
||||
res.ExitCode = info.ExitCode
|
||||
if info.ExitCode != 0 && res.Err == nil {
|
||||
res.Err = fmt.Errorf("exit status %d", info.ExitCode)
|
||||
if info, ierr := m.waitExecInspect(inspectCtx, execID); ierr == nil {
|
||||
if info.Running {
|
||||
if pgidFile != "" {
|
||||
m.killProcessGroup(containerID, pgidFile)
|
||||
}
|
||||
if res.Err == nil {
|
||||
res.Err = errors.New("sandbox exec stream closed prematurely while command was still running")
|
||||
}
|
||||
} else {
|
||||
res.ExitCode = info.ExitCode
|
||||
if info.ExitCode != 0 && res.Err == nil {
|
||||
res.Err = fmt.Errorf("exit status %d", info.ExitCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1304,8 +1310,7 @@ func (m *Manager) ExecStream(ctx context.Context, t Target, spec ExecSpec) (*Str
|
||||
}
|
||||
|
||||
pgidFile := "/tmp/.artex-stream-" + randomToken(8)
|
||||
wrapped := fmt.Sprintf(`echo $$ > %q; trap 'rm -f %q' EXIT; exec "$@"`, pgidFile, pgidFile)
|
||||
cmd := append([]string{"setsid", "sh", "-c", wrapped, "--"}, spec.Cmd...)
|
||||
cmd := wrapPGIDCommand(pgidFile, spec.Cmd)
|
||||
|
||||
execID, err := m.docker.ExecCreate(ctx, containerID, dockerExecCreateReq{
|
||||
AttachStdin: true,
|
||||
@@ -1359,6 +1364,41 @@ func (m *Manager) ExecStream(ctx context.Context, t Target, spec ExecSpec) (*Str
|
||||
}, nil
|
||||
}
|
||||
|
||||
// wrapPGIDCommand records the exec process PID before replacing the shell with
|
||||
// the target command. Docker's runc already places every exec process into its
|
||||
// own session and process group (PID == PGID == SID), so we must NOT invoke
|
||||
// util-linux `setsid` (which without `--wait` forks when PID == PGID and exits
|
||||
// 0 immediately, causing containerd to cut off stdout/stderr at ~2s and masking
|
||||
// non-zero exit codes).
|
||||
func wrapPGIDCommand(pgidFile string, cmd []string) []string {
|
||||
wrapped := fmt.Sprintf(`echo $$ > %q; exec "$@"`, pgidFile)
|
||||
return append([]string{"sh", "-c", wrapped, "--"}, cmd...)
|
||||
}
|
||||
|
||||
// waitExecInspect polls Docker ExecInspect briefly after the stdio stream closes
|
||||
// so containerd's final Running->false transition settles before reading ExitCode.
|
||||
func (m *Manager) waitExecInspect(ctx context.Context, execID string) (dockerExecInspect, error) {
|
||||
var (
|
||||
info dockerExecInspect
|
||||
err error
|
||||
)
|
||||
backoffs := []time.Duration{0, 15 * time.Millisecond, 40 * time.Millisecond, 100 * time.Millisecond, 150 * time.Millisecond, 200 * time.Millisecond}
|
||||
for _, d := range backoffs {
|
||||
if d > 0 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return info, ctx.Err()
|
||||
case <-time.After(d):
|
||||
}
|
||||
}
|
||||
info, err = m.docker.ExecInspect(ctx, execID)
|
||||
if err != nil || !info.Running {
|
||||
return info, err
|
||||
}
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
|
||||
type hijackedWriteCloser struct {
|
||||
h *hijackedExec
|
||||
}
|
||||
|
||||
@@ -3,8 +3,12 @@ package sandbox
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -224,3 +228,27 @@ func TestEnsureWritableDirMigratesNestedFilesWithoutFollowingSymlinks(t *testing
|
||||
t.Fatalf("symlink target outside workspace must stay 0400, got %o", outInfo.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapPGIDCommandDoesNotDetachAndPropagatesExitCode(t *testing.T) {
|
||||
pgidFile := filepath.Join(t.TempDir(), "test.pgid")
|
||||
cmd := wrapPGIDCommand(pgidFile, []string{"sh", "-c", "echo hi; exit 42"})
|
||||
if len(cmd) == 0 || cmd[0] == "setsid" {
|
||||
t.Fatalf("wrapPGIDCommand must not wrap with detaching setsid, got %v", cmd)
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
return
|
||||
}
|
||||
c := exec.Command(cmd[0], cmd[1:]...)
|
||||
out, err := c.CombinedOutput()
|
||||
if strings.TrimSpace(string(out)) != "hi" {
|
||||
t.Fatalf("expected output 'hi', got %q (err=%v)", string(out), err)
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if !errors.As(err, &exitErr) || exitErr.ExitCode() != 42 {
|
||||
t.Fatalf("expected exit code 42, got err=%v", err)
|
||||
}
|
||||
pidBytes, rerr := os.ReadFile(pgidFile)
|
||||
if rerr != nil || strings.TrimSpace(string(pidBytes)) == "" {
|
||||
t.Fatalf("expected pgid file to contain PID, got %q err=%v", string(pidBytes), rerr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user