mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 20:34:53 +00:00
the timeout by exit code 124. artex-exec-wrap returns 124 when it enforces timeout_ms, but the command can also exit 124 itself: coreutils `timeout 1 sleep 5` does. The runner then removed the worker and stopped the task's background jobs after an ordinary inner timeout. Count an exit 124 as the runner's timeout only when the exec ran for at least timeout_ms, which is the only way the wrapper can return it.
120 lines
4.1 KiB
Go
120 lines
4.1 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A real Engine is needed to prove the kernel kills a detached child, not just
|
|
// that the runner requests removal. The child writes a canary and performs no
|
|
// network activity. Build the worker/proxy images before opting in.
|
|
func TestDockerTimeoutRemovesDetachedChild(t *testing.T) {
|
|
if os.Getenv("ARTEX_SANDBOX_DOCKER_TEST") != "1" {
|
|
t.Skip("requires live Docker and built sandbox images")
|
|
}
|
|
ws, au := t.TempDir(), t.TempDir()
|
|
id := fmt.Sprintf("timeout-%d", time.Now().UnixNano())
|
|
egressNet := "artex-test-egress-" + id
|
|
for _, dir := range []string{ws, au, filepath.Join(ws, id), filepath.Join(au, id)} {
|
|
if err := os.MkdirAll(dir, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
b := NewDockerBackend(DockerConfig{WorkspaceRoot: ws, HostWorkspaceRoot: ws, AuditRoot: au, HostAuditRoot: au,
|
|
TaskNetPrefix: "artex-test-task-", EgressNet: egressNet})
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
t.Cleanup(func() {
|
|
_ = b.Delete(context.Background(), id)
|
|
_ = b.removeNetwork(context.Background(), egressNet)
|
|
})
|
|
if err := b.Ping(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc, err := b.ensureTask(ctx, id, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
worker := tc.workerID
|
|
script := `import subprocess,sys,time
|
|
child="import time; f=open('/workspace/timeout-canary','ab',buffering=0); [(f.write(b'x'),time.sleep(.05)) for _ in range(200)]"
|
|
subprocess.Popen([sys.executable,'-c',child],start_new_session=True,stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
|
time.sleep(10)`
|
|
res, err := b.Exec(ctx, id, ExecRequest{Argv: []string{"python3", "-c", script}, Workdir: "/workspace", TimeoutMS: 1000})
|
|
if err != nil || !res.TimedOut {
|
|
t.Fatalf("response=%+v error=%v", res, err)
|
|
}
|
|
r, err := b.dockerDo(ctx, http.MethodGet, "/containers/"+worker+"/json", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer r.Body.Close()
|
|
if r.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("timed-out worker still exists: HTTP %d", r.StatusCode)
|
|
}
|
|
before, err := os.ReadFile(filepath.Join(ws, id, "timeout-canary"))
|
|
if err != nil || len(before) == 0 {
|
|
t.Fatalf("child never ran: %v", err)
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
after, err := os.ReadFile(filepath.Join(ws, id, "timeout-canary"))
|
|
if err != nil || len(after) != len(before) {
|
|
t.Fatalf("child continued after timeout: %v", err)
|
|
}
|
|
}
|
|
|
|
// A command that exits 124 by itself must not be treated as the runner's
|
|
// timeout: the worker, and with it the task's background jobs, stays.
|
|
func TestDockerCommandExit124KeepsWorker(t *testing.T) {
|
|
if os.Getenv("ARTEX_SANDBOX_DOCKER_TEST") != "1" {
|
|
t.Skip("requires live Docker and built sandbox images")
|
|
}
|
|
ws, au := t.TempDir(), t.TempDir()
|
|
id := fmt.Sprintf("exit124-%d", time.Now().UnixNano())
|
|
egressNet := "artex-test-egress-" + id
|
|
for _, dir := range []string{ws, au, filepath.Join(ws, id), filepath.Join(au, id)} {
|
|
if err := os.MkdirAll(dir, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
b := NewDockerBackend(DockerConfig{WorkspaceRoot: ws, HostWorkspaceRoot: ws, AuditRoot: au, HostAuditRoot: au,
|
|
TaskNetPrefix: "artex-test-task-", EgressNet: egressNet})
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
t.Cleanup(func() {
|
|
_ = b.Delete(context.Background(), id)
|
|
_ = b.removeNetwork(context.Background(), egressNet)
|
|
})
|
|
if err := b.Ping(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc, err := b.ensureTask(ctx, id, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
worker := tc.workerID
|
|
res, err := b.Exec(ctx, id, ExecRequest{Argv: []string{"timeout", "1", "sleep", "5"}, Workdir: "/workspace", TimeoutMS: 30000})
|
|
if err != nil || res.ExitCode != 124 || res.TimedOut {
|
|
t.Fatalf("response=%+v error=%v", res, err)
|
|
}
|
|
r, err := b.dockerDo(ctx, http.MethodGet, "/containers/"+worker+"/json", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer r.Body.Close()
|
|
if r.StatusCode != http.StatusOK {
|
|
t.Fatalf("worker removed after the command's own exit 124: HTTP %d", r.StatusCode)
|
|
}
|
|
}
|