Skip to content

test(isolation): 20 reflexive tests fail in the full suite but pass alone — Hypatia.Supervisor is torn down mid-run #643

Closed

Description

@hyperpolymath

Context

E2E — Elixir Scanner Pipeline is red on main. With #642 making the output visible, the
cause is now measurable rather than mysterious.

Reproduced locally (Elixir 1.18.3 / OTP 27, mix deps.get clean):

Run Result
mix test (full suite) 1398 tests, 20 failures
mix test test/reflexive_test.exs (alone) 16 tests, 0 failures

Every one of the 20 failures is in test/reflexive_test.exs with the same shape:

** (exit) exited in: GenServer.call(Hypatia.Supervisor, {:terminate_child, Hypatia.SelfDiagnostics}, :infinity)
    ** (EXIT) no process: the process is not alive or there's no process currently
              associated with the given name, possibly because its application isn't started
  test/reflexive_test.exs:29: Hypatia.ReflexiveTest.take_supervised/1

The file passing in isolation is the key datum: this is a test-isolation defect, not a
product bug.
Hypatia.Supervisor is alive at the start of an isolated run and dead by the
time reflexive tests run in the full suite.

Likely mechanism (to be confirmed, not assumed)

take_supervised/1 (test/reflexive_test.exs:29) removes a child from the live application
supervisor so start_supervised!/1 can own a clean instance:

Supervisor.terminate_child(Hypatia.Supervisor, module)
Supervisor.delete_child(Hypatia.Supervisor, module)
start_supervised!(module)
on_exit(fn -> ... Supervisor.start_child(Hypatia.Supervisor, module) end)

Two candidate causes, both consistent with the evidence:

  1. Restore never happens. If a test crashes before on_exit runs, or start_child
    fails (the spec was deleted), the child stays absent and later take_supervised calls
    compound the damage.
  2. max_restarts exceeded. lib/application.ex:90 uses strategy: :one_for_one with
    the default max_restarts: 3, max_seconds: 5. Repeated terminate/restart churn across
    many tests — plus any child that crash-loops — trips the limit and the whole supervisor
    shuts down
    , taking Hypatia.Supervisor with it. This matches "no process ... its
    application isn't started" exactly.

Hypatia.ReflexiveTest is async: false, so ordering is deterministic per seed — the failure
should be reproducible with a fixed --seed.

Changes

Diagnosis first — do not guess at a fix:

  1. Confirm which of the two mechanisms applies: run the full suite with a fixed --seed,
    and assert Process.whereis(Hypatia.Supervisor) in setup to find the first test at
    which it is already nil. Bisect by test file.
  2. Then fix at the right level:
    • If restore-failure: make take_supervised/1 restore idempotently and register the
      on_exit before the mutation, so a crash mid-setup still restores.
    • If max_restarts: stop mutating the live application supervisor from tests at all.
      Preferred shape — start these GenServers under the test's own supervisor via
      start_supervised!/1 with an explicit name:, and leave Hypatia.Supervisor
      untouched. A test should not need to dismantle the application to observe it.
  3. Consider raising max_restarts in lib/application.ex:90 only if it is genuinely
    warranted in production — not as a way to paper over test churn.

Acceptance criteria

  • mix test is 0 failures on a clean checkout, at three different seeds.
  • mix test test/reflexive_test.exs still passes in isolation (16 tests).
  • No test terminates or deletes a child of Hypatia.Supervisor, or if it must, the
    restore is proven by asserting the child is alive again after the suite.
  • E2E — Elixir Scanner Pipeline green on main.

Test plan

Model tier

Sonnet — bounded, single-repo, no new design; the mechanism is narrowed to two
candidates and the acceptance criteria are mechanical. Escalate to Opus only if the
cause turns out to be a genuine supervision-tree design problem rather than test hygiene.

Related

Activity

  1. added
    cx/L2
    status:ready
    vfy/test
    tri/control
    on Jul 21, 2026
  2. hyperpolymath commented on Jul 21, 2026

    @hyperpolymath
    OwnerAuthor

    Correction to the original diagnosis, and a partial fix

    Two things in the opening post were wrong, and the corrected picture is better.

    Wrong: "Hypatia.Supervisor is torn down mid-run"

    I instrumented test_helper.exs with a monitor on Hypatia.Supervisor and ran the full
    suite. No :DOWN ever fired — the supervisor survives the whole run. The
    max_restarts hypothesis is dead.

    Wrong: "20 failures, all in reflexive_test"

    That was one seed. The failing set changes with the seed:

    Seed Failures Reflexive involved?
    1 9 no
    7 57 yes
    42 15 yes
    0 10 no — instead dashboard, root_hygiene, workflow_audit, metrics, recipe_health

    So this is not one bug. It is order-dependent state leakage with several independent
    sources, of which reflexive was merely the loudest.

    Confirmed: the actual reflexive mechanism

    take_supervised/1 registered its restoring on_exit after mutating the live
    supervisor. If anything in between raised, the child was deleted and never restored — for
    the rest of the suite — so one root failure cascaded into all later tests needing that
    module.

    Proved by injecting a raise after the delete and asking the supervisor what children it has
    (load-independent, unlike suite counts):

    [[PROOF]] OLD  -> child present after raise: false     <- lost forever
    [[PROOF]] NEW  -> restore registered, runs on_exit
    

    Fixed in #644: register the restore first, make it idempotent, and inspect
    Supervisor.start_child/2's return instead of discarding it.

    Also found: the suite is load-sensitive

    reflexive_test.exs (and others) synchronise with :timer.sleep(30) / :timer.sleep(50).
    Under load those flake, which made a whole-suite A/B of the fix inconclusive — counts
    moved in both directions across seeds for reasons unrelated to the change. Worth replacing
    with deterministic waits (poll-until-condition, or assert_receive on a telemetry event);
    until then, suite-count comparisons on a busy machine are not evidence.

    Revised remaining work

    • reflexive cascade — fix(test): register the supervisor restore BEFORE mutating (#643) #644
    • the other order-dependent failures (dashboard, root_hygiene, workflow_audit,
      metrics, recipe_health) — find each leak; these are independent of the above
    • replace :timer.sleep/1 synchronisation with deterministic waits
    • only then is "mix test green at three seeds" a meaningful acceptance gate

    Re-tiering: the cascade was cx/L2 and is done. The remainder is cx/L3 — several
    independent leaks across unrelated test files, needing real comprehension of each.

  3. added
    cx/L3
    and removed
    cx/L2
    on Jul 21, 2026
  4. hyperpolymath commented on Jul 21, 2026

    @hyperpolymath
    OwnerAuthor

    The failure count is currently unmeasurable — CI does not pin the seed

    Two consecutive CI runs on this branch, with zero Elixir source changed between them
    (git diff --stat 311b504..1dd31af = .github/workflows/tests.yml only, 0 files matching
    \.exs?$|^lib/|^test/):

    run ExUnit seed result
    311b504 20912 1398 tests, 73 failures
    1dd31af 406044 1398 tests, 79 failures

    Same code, different seed, ±6 failures. The dominant cluster is identical in both
    (contract_test 42, blackboard_test 40, reflexive_test 33, kin_test 30), so this is
    this issue's order-dependence, not a regression.

    The consequence is what matters: while the seed floats, nobody can distinguish a real
    regression from reshuffled noise
    . A fix that genuinely removes 6 failures and a seed that
    happens to be kinder look identical. I nearly mis-reported the 73→79 delta as a regression
    myself, and only caught it by checking that no Elixir file had changed.

    Cheap fix, worth doing before any work on this issue

    Pin the seed in CI so the count is comparable run to run:

    # test/test_helper.exs
    ExUnit.start(seed: String.to_integer(System.get_env("EXUNIT_SEED", "0")))

    or simply mix test --seed 0 in the workflow. Then:

    • a changed failure count means a real change
    • EXUNIT_SEED=<n> mix test still reproduces any specific ordering on demand
    • a separate scheduled job can rotate seeds deliberately to find new order-dependence,
      which is the only place randomness actually earns its keep

    Without this, the acceptance criterion "failures drop from N" is not checkable.

    Related

  5. hyperpolymath commented on Aug 7, 2026

    @hyperpolymath
    OwnerAuthor

    Seed pinned, and the residual cluster is now closed — but the diagnosis in comment 2 needed one correction.

    test/test_helper.exs now sets seed: 0 (the prerequisite from comment 2: failure-count deltas are evidence again). Pass --seed N explicitly to probe ordering.

    What pinning revealed is that not all of the residual instability was ordering at all. Three distinct mechanisms, all now fixed:

    1. Genuine order-dependence — shared supervised state. Hypatia.Watcher's queue-depth map keyed Prometheus labels on inspect(child_id); Bandit's id is {module, make_ref()}, so a fresh #Reference<...> landed in the label (unbounded cardinality) and its braces broke the exposition test's label parsing — but only once the 30s queue poll had fired, hence in-suite only. Ids now collapse to the module. Separately, Diagnostics.Monitor's 30s tick is now config-gated off in the test env: its recovery path GenServer.stops supervised children mid-suite, which is invisible order-dependence by construction.

    2. Not ordering — cross-RUN state. RecipeHealthTest derived "unique" recipe ids from System.unique_integer/1, which is unique only within one BEAM instance: it restarts from a low base each mix test and allocates stride-32 blocks whose offset moves with whatever ran before it. The outcomes log is append-only and was never truncated, so a later run redrew ids that an earlier run had already written records under, and verification_rate/2 aggregated both. Measured 2026-08-07: 194 stale test-recipe-health-* ids accumulated since 07-30; one failure asserted 6 successes and observed 16 — exactly 10 stale (from a 4-day-old quarantine test) plus 6 new. This is why pinning the seed did not stabilise that file. Fixed by giving ids a monotonic-clock component and wiping the throwaway store in test_helper.exs.

    3. Not ordering — a timer race. RateLimiter "enqueue increments queue size" enqueued for a bot that was not rate limited, then slept 50ms and asserted queue_size >= 1. The 5s :drain_queue tick pops and dispatches any entry whose bot passes the check, so a tick inside that window emptied the queue. Where the tick falls shifts with total suite duration — seed-dependent in appearance, a pure race in fact. Fixed by saturating the burst limit first so drain returns the entry (also the realistic scenario), with delta rather than absolute counter assertions.

    Also on this issue's remit: test/contract_test.exs's unhardened take_supervised/1 is gone — #688 removed it outright, which is the better fix (those tests isolate by unique key, not by process). The test/reflexive_test.exs hardening from #644 remains the reference pattern where a test genuinely must take a child from the supervisor; test/watcher_test.exs now uses it, having previously raced the supervised Watcher's :permanent restart.

    Current state: 1433 tests, 0 failures, 242 excluded — verified at seeds 0, 1, 42 and 777. The :timer.sleep(30)/(50) synchronisation this issue also flags remains in place where it is not load-bearing; replacing those with deterministic waits is the honest remaining scope if you want to keep this open for it.

  6. added
    bug
    on Aug 24, 2026
  7. hyperpolymath commented on Aug 24, 2026

    @hyperpolymath
    OwnerAuthor

    Re-verified after the CI lock wall was removed by #715. The supervisor defect described here was fixed by #644 / e5d6f26: mix test test/reflexive_test.exs --seed 0 is 16 tests, 0 failures. A full mix test --seed 0 --max-failures 2 no longer produces any Hypatia.Supervisor or reflexive failures; its first two failures are unrelated merge-orchestration policy assertions in StrategistTest and SensorTest. The live E2E job on #715 stopped earlier at the Elixir lint gate and did not execute the suite, so that lint failure should be tracked separately rather than keeping this stale supervisor issue open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcx/L3status:readytri/controlvfy/test

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions