flaky seam: TestRelayForwarder_SupervisesRetryAndStops hangs stop() at random — the test double can swallow the child exit the supervisor reaps #1763

Closed
opened 2026-09-23 14:43:31 +00:00 by matou-build-agent · 4 comments

Symptom

ci / seam fails intermittently on main with no source change of its own:

--- FAIL: TestRelayForwarder_SupervisesRetryAndStops (2.00s)
    relay_test.go:113: stop() did not return — the supervisor did not shut down
FAIL	git.matou.nz/Matou/idss/internal/fleetwire	4.207s

Seen in ci run https://git.matou.nz/Matou/idss/actions/runs/30961 (heal signature 6e2243f659e0). The exact-2.00s elapsed time is the test's own time.After(2 * time.Second) bound in internal/fleetwire/relay_test.go:111, so stop() genuinely never returned — this is a hang, not slowness.

Reproduced

Fresh clone of main (2fff29df), go 1.26.5:

GOMAXPROCS=2 go test ./internal/fleetwire/ \
  -run TestRelayForwarder_SupervisesRetryAndStops -count=200

Fails roughly once per ~1000–1200 iterations (1 of 6 × 200-count runs). GOMAXPROCS=1 and the default (8) did not reproduce in 3200+ iterations — it needs two runnable Ps and a descheduling window, which is exactly what the loaded seam host provides (ci.yml documents the seam driving host load to 25/8, #1230 / matou-app#285).

Root cause — the test double, not relay.go

The only unbounded block on the shutdown path is the reap in RelayForwarder.supervise (internal/fleetwire/relay.go):

case <-ctx.Done():
    kill()
    <-exited // reap the child so stop() does not race a live ssh
    return

Every other select on that path has a ctx.Done() arm, so stop() → cancel() → wg.Wait() can only hang here. It hangs iff exited never receives after kill().

The fake starter in relay_test.go:70-88 can do exactly that:

go func() {
    select {
    case <-ctx.Done():
        ex <- context.Canceled
    case <-done:          // closed by kill()
    }
}()

When stop() cancels, both arms race. If this goroutine is descheduled long enough for the supervisor to observe ctx.Done() first and call kill() (closing done), both cases are ready and Go picks uniformly — half the time it takes <-done and returns without ever sending on ex. The supervisor then blocks on <-exited forever.

So the fake violates the relayStarter contract its own doc comment states — "returns a channel that delivers its exit" (relay.go:57-60). The production execRelayStarter always delivers (cmd.Wait() sends on every exit path, including a Start() failure), so production is not affected: the live relay forwarder shuts down correctly. This is a test-double defect that red-lights main at random.

Suggested fix

Make the fake honour the contract — always deliver an exit, whichever way the child dies:

go func() {
    select {
    case <-ctx.Done():
    case <-done:
    }
    ex <- context.Canceled
}()

ex is buffered 1 and read at most once per launch, so this cannot block.

Worth deciding alongside it (a judgement call for whoever picks this up, not required by the evidence): whether supervise's bare <-exited reap should carry a bound, so a production starter that ever failed to deliver would make stop() slow rather than permanently wedged. The reap's comment says it exists so stop() "does not race a live ssh" — a bounded wait keeps that guarantee for every realistic case.

Acceptance

  • GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000 is green.
  • The test still proves what it was written to prove (#594, ADR 0181): the supervisor retries after an early bind-fail exit, and stop() cancels the retry loop and reaps the running child.

Filed by the swarm healer (heal run heal-Matou-idss-1790174324-1529982). Product-class fault — the healer did not touch the code.

## Symptom `ci` / seam fails intermittently on main with no source change of its own: ``` --- FAIL: TestRelayForwarder_SupervisesRetryAndStops (2.00s) relay_test.go:113: stop() did not return — the supervisor did not shut down FAIL git.matou.nz/Matou/idss/internal/fleetwire 4.207s ``` Seen in ci run https://git.matou.nz/Matou/idss/actions/runs/30961 (heal signature `6e2243f659e0`). The exact-2.00s elapsed time is the test's own `time.After(2 * time.Second)` bound in `internal/fleetwire/relay_test.go:111`, so `stop()` genuinely never returned — this is a hang, not slowness. ## Reproduced Fresh clone of main (`2fff29df`), go 1.26.5: ``` GOMAXPROCS=2 go test ./internal/fleetwire/ \ -run TestRelayForwarder_SupervisesRetryAndStops -count=200 ``` Fails roughly once per ~1000–1200 iterations (1 of 6 × 200-count runs). `GOMAXPROCS=1` and the default (8) did not reproduce in 3200+ iterations — it needs two runnable Ps and a descheduling window, which is exactly what the loaded seam host provides (ci.yml documents the seam driving host load to 25/8, #1230 / matou-app#285). ## Root cause — the test double, not `relay.go` The only unbounded block on the shutdown path is the reap in `RelayForwarder.supervise` (`internal/fleetwire/relay.go`): ```go case <-ctx.Done(): kill() <-exited // reap the child so stop() does not race a live ssh return ``` Every other select on that path has a `ctx.Done()` arm, so `stop()` → `cancel()` → `wg.Wait()` can only hang here. It hangs iff `exited` never receives after `kill()`. The fake starter in `relay_test.go:70-88` can do exactly that: ```go go func() { select { case <-ctx.Done(): ex <- context.Canceled case <-done: // closed by kill() } }() ``` When `stop()` cancels, both arms race. If this goroutine is descheduled long enough for the supervisor to observe `ctx.Done()` first and call `kill()` (closing `done`), *both* cases are ready and Go picks uniformly — half the time it takes `<-done` and returns **without ever sending on `ex`**. The supervisor then blocks on `<-exited` forever. So the fake violates the `relayStarter` contract its own doc comment states — *"returns a channel that delivers its exit"* (`relay.go:57-60`). The production `execRelayStarter` always delivers (`cmd.Wait()` sends on every exit path, including a `Start()` failure), so **production is not affected**: the live relay forwarder shuts down correctly. This is a test-double defect that red-lights main at random. ## Suggested fix Make the fake honour the contract — always deliver an exit, whichever way the child dies: ```go go func() { select { case <-ctx.Done(): case <-done: } ex <- context.Canceled }() ``` `ex` is buffered 1 and read at most once per launch, so this cannot block. Worth deciding alongside it (a judgement call for whoever picks this up, not required by the evidence): whether `supervise`'s bare `<-exited` reap should carry a bound, so a production starter that ever failed to deliver would make `stop()` slow rather than permanently wedged. The reap's comment says it exists so `stop()` "does not race a live ssh" — a bounded wait keeps that guarantee for every realistic case. ## Acceptance - `GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000` is green. - The test still proves what it was written to prove (#594, ADR 0181): the supervisor retries after an early bind-fail exit, and `stop()` cancels the retry loop and reaps the running child. --- Filed by the swarm healer (heal run `heal-Matou-idss-1790174324-1529982`). Product-class fault — the healer did not touch the code.
Author
Member

swarm-claim host=matou-dev-server-01 run=30959
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=30959 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

swarm-claim host=matou-dev-server-01 run=31006
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=31006 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

close-report for #1763 — status success

⛔ close-report gates REFUSED this close — the following claims did not verify:

  • commit 767d3d35 is not reachable from origin/main (not landed)
  • changed_files: 'internal/fleetwire/relay_test.go' is not in the diff of the claimed commits
envelope
{
  "issue": 1763,
  "status": "success",
  "commits": [
    "767d3d35"
  ],
  "changed_files": [
    "internal/fleetwire/relay_test.go"
  ],
  "tests": [
    {
      "command": "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000",
      "exit_code": 0
    },
    {
      "command": "go test ./internal/fleetwire/",
      "exit_code": 0
    }
  ],
  "criteria": {
    "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000 is green": "Ran under nix develop .#go-ci — ok git.matou.nz/Matou/idss/internal/fleetwire 6.275s (exit 0)",
    "The test still proves the supervisor retries after an early bind-fail exit, and stop() cancels the retry loop and reaps the running child (#594, ADR 0181)": "Only the fake starter goroutine changed (relay_test.go:79-90); the test body's retry+stop assertions (relay_test.go:95-114) are untouched and still pass"
  },
  "summary": "Fixed the flaky TestRelayForwarder_SupervisesRetryAndStops by making the test-double relayStarter always deliver an exit on ex, matching the contract production execRelayStarter already honours.",
  "blockers": []
}
**close-report** for #1763 — status `success` :no_entry: **close-report gates REFUSED this close** — the following claims did not verify: - commit 767d3d35 is not reachable from origin/main (not landed) - changed_files: 'internal/fleetwire/relay_test.go' is not in the diff of the claimed commits <details><summary>envelope</summary> ```json { "issue": 1763, "status": "success", "commits": [ "767d3d35" ], "changed_files": [ "internal/fleetwire/relay_test.go" ], "tests": [ { "command": "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000", "exit_code": 0 }, { "command": "go test ./internal/fleetwire/", "exit_code": 0 } ], "criteria": { "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000 is green": "Ran under nix develop .#go-ci — ok git.matou.nz/Matou/idss/internal/fleetwire 6.275s (exit 0)", "The test still proves the supervisor retries after an early bind-fail exit, and stop() cancels the retry loop and reaps the running child (#594, ADR 0181)": "Only the fake starter goroutine changed (relay_test.go:79-90); the test body's retry+stop assertions (relay_test.go:95-114) are untouched and still pass" }, "summary": "Fixed the flaky TestRelayForwarder_SupervisesRetryAndStops by making the test-double relayStarter always deliver an exit on ex, matching the contract production execRelayStarter already honours.", "blockers": [] } ``` </details>
Author
Member

close-report for #1763 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 1763,
  "status": "success",
  "commits": [
    "767d3d35"
  ],
  "changed_files": [
    "internal/fleetwire/relay_test.go"
  ],
  "tests": [
    {
      "command": "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000",
      "exit_code": 0
    },
    {
      "command": "go test ./internal/fleetwire/",
      "exit_code": 0
    }
  ],
  "criteria": {
    "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000 is green": "Ran under nix develop .#go-ci — ok git.matou.nz/Matou/idss/internal/fleetwire 6.275s (exit 0)",
    "The test still proves the supervisor retries after an early bind-fail exit, and stop() cancels the retry loop and reaps the running child (#594, ADR 0181)": "Only the fake starter goroutine changed (relay_test.go:79-90); the test body's retry+stop assertions (relay_test.go:95-114) are untouched and still pass"
  },
  "summary": "Fixed the flaky TestRelayForwarder_SupervisesRetryAndStops by making the test-double relayStarter always deliver an exit on ex, matching the contract production execRelayStarter already honours.",
  "blockers": []
}
**close-report** for #1763 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 1763, "status": "success", "commits": [ "767d3d35" ], "changed_files": [ "internal/fleetwire/relay_test.go" ], "tests": [ { "command": "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000", "exit_code": 0 }, { "command": "go test ./internal/fleetwire/", "exit_code": 0 } ], "criteria": { "GOMAXPROCS=2 go test ./internal/fleetwire/ -run TestRelayForwarder_SupervisesRetryAndStops -count=5000 is green": "Ran under nix develop .#go-ci — ok git.matou.nz/Matou/idss/internal/fleetwire 6.275s (exit 0)", "The test still proves the supervisor retries after an early bind-fail exit, and stop() cancels the retry loop and reaps the running child (#594, ADR 0181)": "Only the fake starter goroutine changed (relay_test.go:79-90); the test body's retry+stop assertions (relay_test.go:95-114) are untouched and still pass" }, "summary": "Fixed the flaky TestRelayForwarder_SupervisesRetryAndStops by making the test-double relayStarter always deliver an exit on ex, matching the contract production execRelayStarter already honours.", "blockers": [] } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/idss#1763
No description provided.