carlos / platform Public

media: let medialocal fakes ignore SIGTERM before they report ready

Hosted CI on 3577bbed failed TestRunnerStopEndsLikeTheTimer: the codec
was gone 1.1 ms after Stop. Reproduced locally (8 of 100 runs with
-count=50 -cpu=1,4); every early death exited 143, SIGTERM's default
action. The fake printed its ready line before signal.Notify, so a Stop
in that window killed it outright.

Each fake now installs its SIGTERM handler before it prints
"codec <pid>", and a grandchild confirms its own on a ready pipe
before the codec prints anything. watchOutput waits for the ready line
under a context and records when it came. The deadline tests check that
it came before their deadline, now 2 s, and say the machine is too
loaded if not. The same stress now passes 100 of 100.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5c6c435cd7bb3bf398a698b8531e75d638d8eee8 parent 3577bbe
1 file changed, +109 −38
  • internal/medialocal/runner_test.go +109 −38

CI passed — run details

diff --git a/internal/medialocal/runner_test.go b/internal/medialocal/runner_test.go
index 149ffd9fe..80d396f2e 100644
--- a/internal/medialocal/runner_test.go
+++ b/internal/medialocal/runner_test.go
@@ -45,11 +45,19 @@ func TestMain(m *testing.M) {
// fakeCodec runs one scenario and answers its exit code.
//
+// A fake that ignores SIGTERM prints its "codec <pid>" line, the line
+// the tests wait for before they signal it, only once its SIGTERM
+// handler is installed and any grandchild has said the same of its own
+// (startGrandchild). Printed earlier, a SIGTERM could arrive while the
+// signal's default action still applied and end it at once (hosted CI,
+// 3577bbed: Stop 30 ms after the start, codec exit 143).
+//
// - ignore-term: starts a grandchild in its own process group that
-// ignores SIGTERM too, prints "codec <pid>" and "grandchild <pid>"
-// on stdout, which both hold open, and blocks ignoring SIGTERM.
-// - grandchild: ignore-term's and move-group's child: blocks ignoring
-// SIGTERM.
+// ignores SIGTERM too, prints "grandchild <pid>" and then
+// "codec <pid>" on stdout, which both hold open, and blocks
+// ignoring SIGTERM.
+// - grandchild: ignore-term's and move-group's child: ignores
+// SIGTERM, says so on its ready pipe (fd 3), and blocks.
// - move-group: starts a grandchild leading a process group of its
// own, with nothing of the codec's stdout, moves itself into that
// group, prints both pids and blocks ignoring SIGTERM: the runner's
@@ -63,48 +71,47 @@ func TestMain(m *testing.M) {
func fakeCodec(scenario string) int {
switch scenario {
case "ignore-term":
- exe, err := os.Executable()
+ term := ignoreTerm()
+ gpid, err := startGrandchild(false)
if err != nil {
+ fmt.Fprintln(os.Stderr, "fake:", err)
return 1
}
- g := exec.Command(exe)
- g.Env = append(os.Environ(), fakeEnv+"=grandchild")
- g.Stdout = os.Stdout
- if err := g.Start(); err != nil {
- return 1
- }
- fmt.Printf("grandchild %d\n", g.Process.Pid)
+ fmt.Printf("grandchild %d\n", gpid)
fmt.Printf("codec %d\n", os.Getpid())
- return blockIgnoringTerm()
+ return blockOn(term)
case "grandchild":
- return blockIgnoringTerm()
+ term := ignoreTerm()
+ ready := os.NewFile(3, "ready")
+ if _, err := ready.Write([]byte{'r'}); err != nil {
+ return 1
+ }
+ ready.Close()
+ return blockOn(term)
case "move-group":
// A grandchild leads a group of its own and holds nothing of the
// codec's; the codec then joins that group, leaving the one the
// runner made for it, and blocks ignoring SIGTERM.
- exe, err := os.Executable()
+ term := ignoreTerm()
+ gpid, err := startGrandchild(true)
if err != nil {
+ fmt.Fprintln(os.Stderr, "fake:", err)
return 1
}
- g := exec.Command(exe)
- g.Env = append(os.Environ(), fakeEnv+"=grandchild")
- g.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- if err := g.Start(); err != nil {
- return 1
- }
- fmt.Printf("grandchild %d\n", g.Process.Pid)
- if err := unix.Setpgid(0, g.Process.Pid); err != nil {
+ fmt.Printf("grandchild %d\n", gpid)
+ if err := unix.Setpgid(0, gpid); err != nil {
fmt.Fprintln(os.Stderr, "fake: setpgid:", err)
return 4
}
- if pgid, _ := unix.Getpgid(0); pgid != g.Process.Pid {
+ if pgid, _ := unix.Getpgid(0); pgid != gpid {
return 5
}
fmt.Printf("codec %d\n", os.Getpid())
- return blockIgnoringTerm()
+ return blockOn(term)
case "alone":
+ term := ignoreTerm()
fmt.Printf("codec %d\n", os.Getpid())
- return blockIgnoringTerm()
+ return blockOn(term)
case "runner":
// The test binary as a runner: one execution of the alone codec,
// its output on this process's stdout, then it waits to be killed.
@@ -121,7 +128,7 @@ func fakeCodec(scenario string) int {
fmt.Fprintln(os.Stderr, "fake runner:", err)
return 1
}
- return blockIgnoringTerm()
+ return blockOn(ignoreTerm())
case "scratch":
name := os.Getenv(media.PayloadEnv)
// The handler's own rule: the scratch is CARLOS_MEDIA_SCRATCH,
@@ -153,15 +160,58 @@ func fakeCodec(scenario string) int {
return 98
}
-// blockIgnoringTerm waits on SIGTERM forever: only SIGKILL ends it.
-func blockIgnoringTerm() int {
+// ignoreTerm makes SIGTERM caught, from its return on: Notify installs
+// the handler before it returns, so a SIGTERM after this never takes the
+// default action. blockOn drains the channel.
+func ignoreTerm() chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
+ return c
+}
+
+// blockOn waits on c forever, dropping each SIGTERM: only SIGKILL ends
+// the process.
+func blockOn(c chan os.Signal) int {
for range c {
}
return 0
}
+// startGrandchild starts the grandchild scenario, in a process group of
+// its own when ownGroup (with nothing of the codec's stdout), else in
+// the codec's group holding its stdout, and returns once the grandchild
+// has said, on a ready pipe, that it ignores SIGTERM.
+func startGrandchild(ownGroup bool) (int, error) {
+ exe, err := os.Executable()
+ if err != nil {
+ return 0, err
+ }
+ rd, wr, err := os.Pipe()
+ if err != nil {
+ return 0, err
+ }
+ defer rd.Close()
+ g := exec.Command(exe)
+ g.Env = append(os.Environ(), fakeEnv+"=grandchild")
+ g.ExtraFiles = []*os.File{wr} // fd 3 in the grandchild
+ if ownGroup {
+ g.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
+ } else {
+ g.Stdout = os.Stdout
+ }
+ err = g.Start()
+ wr.Close()
+ if err != nil {
+ return 0, err
+ }
+ // A byte, or EOF if the grandchild died first: the deadline is the
+ // test's (watchOutput waits for the codec's line under a context).
+ if n, _ := rd.Read(make([]byte, 1)); n != 1 {
+ return 0, errors.New("the grandchild ended before it ignored SIGTERM")
+ }
+ return g.Process.Pid, nil
+}
+
// newTestRunner is a runner of the test binary as the fake codec running
// scenario, with a scratch root of its own, closed when the test ends.
func newTestRunner(t *testing.T, scenario string) *Runner {
@@ -181,13 +231,26 @@ func newTestRunner(t *testing.T, scenario string) *Runner {
}
// ignoring is a running fake codec: the pids it printed (grandchild 0
-// when it has none), and a channel closed once every process holding
-// its stdout has exited.
+// when it has none), when its ready line ("codec <pid>", printed once it
+// and its grandchild ignore SIGTERM) was read, and a channel closed once
+// every process holding its stdout has exited.
type ignoring struct {
codec, grandchild int
+ ready time.Time
gone chan struct{}
}
+// readyBefore fails the test when ig was not ready by deadline: a
+// SIGTERM sent before then may have met the signal's default action,
+// so what follows would not test the runner. Only a starved machine
+// gets here; it says so rather than failing as a runner bug.
+func readyBefore(t *testing.T, ig ignoring, deadline time.Time) {
+ t.Helper()
+ if ig.ready.After(deadline) {
+ t.Fatalf("the fake codec was ready %s after its deadline: the machine is too loaded for this test", ig.ready.Sub(deadline))
+ }
+}
+
// runIgnoring starts an execution of r's scenario with deadlineS and
// reads its pids (watchOutput).
func runIgnoring(t *testing.T, r *Runner, execution string, deadlineS int64) (string, ignoring) {
@@ -236,11 +299,13 @@ func watchOutput(t *testing.T, pr *os.File) ignoring {
pids <- got
io.Copy(io.Discard, pr)
}()
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
select {
case got := <-pids:
- ig.codec, ig.grandchild = got[0], got[1]
- case <-time.After(10 * time.Second):
- t.Fatal("the fake codec did not report its pid")
+ ig.codec, ig.grandchild, ig.ready = got[0], got[1], time.Now()
+ case <-ctx.Done():
+ t.Fatal("the fake codec was not ready within 10 s")
}
if ig.codec <= 0 {
t.Fatalf("the fake codec ended before it reported its pid (grandchild %d)", ig.grandchild)
@@ -310,12 +375,15 @@ func reaped(t *testing.T, pid int) {
// before the deadline plus the grace: SIGTERM to the group at the
// deadline, SIGKILL to the group once the grace has passed. The deadline
// is described reached (0) and the codec killed (137), and it is reaped.
+// The fake must be ready (ignoring SIGTERM) before the deadline, which is
+// 2 s so that a loaded machine has time to start it.
func TestRunnerDeadlineEndsAnIgnoringChild(t *testing.T) {
r := newTestRunner(t, "ignore-term")
r.Grace = time.Second
- const deadline = time.Second
+ const deadline = 2 * time.Second
start := time.Now()
arn, ig := runIgnoring(t, r, "exec-deadline", int64(deadline/time.Second))
+ readyBefore(t, ig, start.Add(deadline))
took := waitGone(t, ig, start, deadline+r.Grace+time.Second, "deadline")
if took < deadline+r.Grace {
t.Fatalf("gone %s after the start, before the deadline plus the grace", took)
@@ -332,7 +400,8 @@ func TestRunnerDeadlineEndsAnIgnoringChild(t *testing.T) {
}
// TestRunnerStopEndsLikeTheTimer: Stop is the timer's SIGTERM, grace and
-// SIGKILL to the group, described stopped by the console (143).
+// SIGKILL to the group, described stopped by the console (143). Stop is
+// called only once the fake is ready, that is ignoring SIGTERM.
func TestRunnerStopEndsLikeTheTimer(t *testing.T) {
r := newTestRunner(t, "ignore-term")
r.Grace = time.Second
@@ -363,13 +432,15 @@ func TestRunnerStopEndsLikeTheTimer(t *testing.T) {
// SIGTERM is still gone within the deadline plus the grace plus 1 s:
// the runner signals the codec itself as well as its group (Astra T13
// P1). Its grandchild, which left the group, is outside the runner's
-// scope (package doc) and is killed by the test.
+// scope (package doc) and is killed by the test. As in the test above,
+// the fake must be ready before the 2 s deadline.
func TestRunnerDeadlineEndsACodecThatMovedGroup(t *testing.T) {
r := newTestRunner(t, "move-group")
r.Grace = time.Second
- const deadline = time.Second
+ const deadline = 2 * time.Second
start := time.Now()
arn, ig := runIgnoring(t, r, "exec-moved", int64(deadline/time.Second))
+ readyBefore(t, ig, start.Add(deadline))
if ig.grandchild <= 0 {
t.Fatalf("the fake reported no grandchild: %+v", ig)
}