Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: box helper init when it runs as pid 1

ogsamrat817e3f133997
diff --git a/apps/desktop/box/go/cmd/boxd/main.go b/apps/desktop/box/go/cmd/boxd/main.go
index aad3e8df..9f1067b3 100644
--- a/apps/desktop/box/go/cmd/boxd/main.go
+++ b/apps/desktop/box/go/cmd/boxd/main.go
@@ -125,6 +125,13 @@ func main() {
fmt.Println(version)
return
}
+ // PID 1 with no init in front (`unshare --pid --fork`, a runtime without
+ // --init): be a tiny init that reaps orphans and run the daemon as its
+ // child, so a reaper never takes a command's exit status from the
+ // daemon's own Wait.
+ if os.Getpid() == 1 && os.Getenv("BOXD_INIT_CHILD") == "" {
+ os.Exit(runInit())
+ }
cfg := loadConfig()
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.SetPrefix("boxd ")
@@ -142,10 +149,6 @@ func main() {
if err != nil {
log.Fatalf("listen: %v", err)
}
- // Reap orphans when we are PID 1 (no --init).
- if os.Getpid() == 1 {
- go reap()
- }
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
@@ -270,12 +273,42 @@ func ensureLine(file, needle, line string) {
_, _ = f.WriteString(line)
}
-func reap() {
+// runInit is PID 1: start boxd as a child, forward signals to it, reap
+// every orphan, and exit with the daemon's status.
+func runInit() int {
+ self, err := os.Executable()
+ if err != nil {
+ self = "/proc/self/exe"
+ }
+ cmd := exec.Command(self, os.Args[1:]...)
+ cmd.Env = append(os.Environ(), "BOXD_INIT_CHILD=1")
+ cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintf(os.Stderr, "boxd init: %v\n", err)
+ return 1
+ }
+ child := cmd.Process.Pid
+ sigs := make(chan os.Signal, 4)
+ signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT)
+ go func() {
+ for sig := range sigs {
+ _ = cmd.Process.Signal(sig)
+ }
+ }()
for {
var status syscall.WaitStatus
pid, err := syscall.Wait4(-1, &status, 0, nil)
- if err != nil || pid <= 0 {
- time.Sleep(200 * time.Millisecond)
+ if err != nil {
+ if errors.Is(err, syscall.EINTR) {
+ continue
+ }
+ return 1
+ }
+ if pid == child {
+ if status.Signaled() {
+ return 128 + int(status.Signal())
+ }
+ return status.ExitStatus()
}
}
}
diff --git a/apps/desktop/box/go/cmd/boxd/pid1_test.go b/apps/desktop/box/go/cmd/boxd/pid1_test.go
new file mode 100644
index 00000000..061b7cca
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/pid1_test.go
@@ -0,0 +1,91 @@
+//go:build linux
+
+package main
+
+import (
+ "fmt"
+ "net"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+
+ "omnirush.ai/box/proto"
+)
+
+// TestPID1ExitCodes runs the real boxd binary as PID 1 of its own PID
+// namespace (what `unshare --pid --fork` in the WSL distro, wslc and Apple
+// containers without --init give it) and checks 200 quick commands, several
+// with children that outlive them, against their exit codes. A reaper that
+// took a command's status from exec.Cmd.Wait turned some of them into 126.
+// Needs unprivileged user namespaces; skipped where they are blocked.
+func TestPID1ExitCodes(t *testing.T) {
+ unshare, err := exec.LookPath("unshare")
+ if err != nil {
+ t.Skip("no unshare")
+ }
+ if out, err := exec.Command(unshare, "--user", "--map-root-user", "--pid", "--fork", "true").CombinedOutput(); err != nil {
+ t.Skipf("no user namespaces here: %s", strings.TrimSpace(string(out)))
+ }
+ dir := t.TempDir()
+ bin := filepath.Join(dir, "boxd")
+ build := exec.Command("go", "build", "-o", bin, ".")
+ build.Env = append(os.Environ(), "CGO_ENABLED=0")
+ if out, err := build.CombinedOutput(); err != nil {
+ t.Fatalf("build: %v %s", err, out)
+ }
+ portFile := filepath.Join(dir, "port")
+ // A fixed free port too, so the same test runs against a boxd without BOXD_PORT_FILE.
+ probe, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ fixed := probe.Addr().(*net.TCPAddr).Port
+ probe.Close()
+ cmd := exec.Command(unshare, "--user", "--map-root-user", "--mount", "--pid", "--fork", "--mount-proc", "--kill-child", bin)
+ cmd.Env = []string{"PATH=/usr/bin:/bin", "BOXD_TOKEN=tok", "BOXD_LISTEN=127.0.0.1:" + strconv.Itoa(fixed), "BOXD_PORT_FILE=" + portFile, "BOXD_HOME=" + dir, "BOXD_ROOT=" + dir, "BOXD_SHELL=/bin/sh"}
+ cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
+ if err := cmd.Start(); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = cmd.Process.Kill(); _, _ = cmd.Process.Wait() })
+ addr := ""
+ for i := 0; i < 200 && addr == ""; i++ {
+ if data, err := os.ReadFile(portFile); err == nil && strings.TrimSpace(string(data)) != "" {
+ addr = "127.0.0.1:" + strings.TrimSpace(string(data))
+ } else if c, err := net.DialTimeout("tcp", "127.0.0.1:"+strconv.Itoa(fixed), 50*time.Millisecond); err == nil {
+ c.Close()
+ addr = "127.0.0.1:" + strconv.Itoa(fixed)
+ } else {
+ time.Sleep(25 * time.Millisecond)
+ }
+ }
+ if addr == "" {
+ t.Fatal("boxd did not start")
+ }
+ // boxd must be PID 1 there (with the init in front, its daemon is PID 2).
+ r := call(t, addr, proto.Request{Op: "exec", Argv: []string{"/bin/sh", "-c", "cat /proc/1/comm; echo $PPID"}}, nil)
+ if !strings.HasPrefix(r.out, "boxd") {
+ t.Fatalf("PID 1 is %q", r.out)
+ }
+ wrong := 0
+ for i := 0; i < 200; i++ {
+ want := i % 7
+ script := fmt.Sprintf("exit %d", want)
+ if i%3 == 0 {
+ // An orphan that outlives the command: PID 1 must reap it, not the command.
+ script = fmt.Sprintf("(sleep 0.05 &) ; echo x; exit %d", want)
+ }
+ res := call(t, addr, proto.Request{Op: "exec", Argv: []string{"/bin/sh", "-c", script}}, nil)
+ if res.exit.Code != want {
+ wrong++
+ t.Logf("call %d: exit %d, want %d (%s)", i, res.exit.Code, want, strconv.Quote(res.err))
+ }
+ }
+ if wrong != 0 {
+ t.Fatalf("%d of 200 commands got a wrong exit code", wrong)
+ }
+}