i3: test the opener's no-kill run with pipes, never a real sleep

Code review round 2 (C-5 follow-up). openerRun's core is now runWithoutKilling(ctx,
*exec.Cmd), callable directly by a test with a command of its own. The test's child
blocks reading its stdin (a pipe held open past the bound); after confirming
TimedOut, the test closes the pipe to unblock it, then waits on the child's own
end (a byte written to an extra file just before it exits, read with a plain
blocking read) instead of sleeping and polling for a marker file. A kill at the
bound now leaves that read empty, which the test checked by reintroducing the old
signal and watching it fail.

Builds: design 08 sign-in-page §3, §29
This commit is contained in:
jochen
2026-11-25 01:13:18 +01:00
parent a94f9f14aa
commit 9a3149df88
2 changed files with 53 additions and 22 deletions
+16 -6
View File
@@ -150,19 +150,29 @@ func xdgDataDirs() []string {
return dirs
}
// openerRun runs the opener (systemd-run --wait -- xdg-open ...): it stops waiting at its bound
// without killing what it started (design 08 sign-in-page §18, §29; DMC-8, lead answer C-5), unlike
// internal/desktop's Exec (run.go), which kills the command's process group at its deadline. A page
// still opening at the bound is left to open: the browser outlives this call either way, since it
// runs under the account's own service manager (systemd-run --user), not as this process's child.
// openerRun runs the opener (systemd-run --wait -- xdg-open ...) through runWithoutKilling: it
// stops waiting at its bound without killing what it started (design 08 sign-in-page §18, §29;
// DMC-8, lead answer C-5), unlike internal/desktop's Exec (run.go), which kills the command's
// process group at its deadline. A page still opening at the bound is left to open: the browser
// outlives this call either way, since it runs under the account's own service manager
// (systemd-run --user), not as this process's child.
func openerRun(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result {
res := desktop.Result{Command: append([]string{name}, args...)}
cmd := exec.Command(name, args...)
cmd.Env = env
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if stdin != nil {
cmd.Stdin = bytes.NewReader(stdin)
}
return runWithoutKilling(ctx, cmd)
}
// runWithoutKilling starts cmd and waits for it, but stops waiting at ctx's deadline without
// sending cmd any signal (design 08 sign-in-page §18, §29; DMC-8, lead answer C-5): left to go on,
// never torn down by the bound that only governs how long this call waits. A test calls this
// directly, with a command of its own whose Stdin and ExtraFiles it controls, to prove the started
// process is never killed, without a real sleep on either side.
func runWithoutKilling(ctx context.Context, cmd *exec.Cmd) desktop.Result {
res := desktop.Result{Command: append([]string{cmd.Path}, cmd.Args[1:]...)}
var out, errb bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &errb
if err := cmd.Start(); err != nil {
+37 -16
View File
@@ -3,8 +3,10 @@ package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
@@ -197,30 +199,49 @@ func TestOpenAnswersOpeningWhenStillRunningAtItsBound(t *testing.T) {
}
}
// C-5: cmd/i3-tools runs the opener with its own run (openerRun, not internal/desktop's Exec, which
// kills a command's process group at its deadline, run.go:82-86): it stops waiting at its bound
// without killing what it started, so an opener still running past the bound keeps running and
// finishes on its own.
// C-5: cmd/i3-tools runs the opener with its own run (runWithoutKilling, not internal/desktop's
// Exec, which kills a command's process group at its deadline, run.go:82-86): it stops waiting at
// its bound without killing what it started. The child blocks reading its stdin, a pipe this test
// holds open past the bound and only then closes; once unblocked, the child writes one byte on an
// extra file before it exits, its own end, which this test waits on instead of sleeping — a byte
// the child could reach only by surviving the bound, never by being killed at it.
func TestOpenerRunStopsWaitingWithoutKillingTheProcess(t *testing.T) {
marker := filepath.Join(t.TempDir(), "ran")
stdinR, stdinW, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer stdinW.Close()
doneR, doneW, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer doneR.Close()
cmd := exec.Command("sh", "-c", "cat >/dev/null; printf x >&3")
cmd.Stdin = stdinR
cmd.ExtraFiles = []*os.File{doneW}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} // as openerRun starts it
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
start := time.Now()
res := openerRun(ctx, os.Environ(), nil, "sh", "-c", "sleep 0.15; touch "+marker)
res := runWithoutKilling(ctx, cmd)
stdinR.Close()
doneW.Close() // this process's own copy: only the child's, closed when it exits, is left
if !res.TimedOut {
t.Fatalf("expected TimedOut, got %+v", res)
}
if since := time.Since(start); since > 200*time.Millisecond {
t.Fatalf("openerRun waited past its own bound: %s", since)
// Unblock the child: were it killed at the bound, this would reach nobody.
if err := stdinW.Close(); err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if _, err := os.Stat(marker); err == nil {
return // the process kept running after openerRun gave up waiting, and finished: not killed
}
time.Sleep(10 * time.Millisecond)
// Wait on the child's own end: blocks until the child writes its byte and exits, closing its
// copy of doneW: never a timer, never a kill.
b := make([]byte, 1)
n, _ := doneR.Read(b)
if n != 1 || b[0] != 'x' {
t.Fatalf("the opener process was killed instead of left to finish: read %d byte(s)", n)
}
t.Fatal("the opener process was killed: it never finished past the bound")
}
// DQ-7 (lead answer note (b)): every call writes one act to the log of acts done by hand, with its