Skip to content

FaultController: the PRNG is shared across rules, so Probability determinism depends on request ordering #510

Closed

Description

@scttfrdmn

Summary

FaultController holds one *rand.Rand shared by every rule in a FaultConfig,
and InjectFault rolls it once per matching rule per request. So a Probability
outcome depends not only on the seed and on how many requests that rule has seen, but
on how many requests every earlier rule matched. A fixed seed reproduces a run only
while the whole sequence of requests is unchanged.

Found while implementing #480, which documented the coupling rather than changing it —
per-rule PRNGs would change the outcome of every existing seeded run, which is not a
change to make inside a fix for something else.

The current state

emulator/fault.go:

type FaultController struct {
	mu     sync.Mutex
	config FaultConfig
	rng    *rand.Rand   // one PRNG for the whole config
}

and in InjectFault, inside the rule loop:

p := rule.Probability
if p <= 0 {
	p = 1.0
}
if f.rng.Float64() >= p {   // advances the sequence for every rule
	continue
}

The roll happens after the rule matches, so a rule that matches nothing does not
advance the PRNG — which is the subtle part. Whether rule 2's roll comes out the same
depends on how many requests rule 1 matched, not on how many requests arrived.

Why it matters

Determinism is substrate's core property, and this is the one place where it holds only
conditionally. Two concrete ways a fixture breaks without anybody touching the fault
config:

  1. A rule ordered before yours starts matching. Arm {service: s3, probability: 0.5}
    and {service: dynamodb, probability: 0.5}. Add an S3 request anywhere upstream — a
    HeadBucket probe in a helper, say — and every subsequent DynamoDB roll shifts. The
    DynamoDB assertions change with a seed that never moved.

  2. A retry. A probability: 0.5 rule that fires makes the SDK retry, and the retry
    is another matching request, so it rolls again. The number of rolls a run performs is
    a function of the outcomes of earlier rolls, which is exactly the feedback loop that
    makes a seeded sequence hard to reason about.

Neither is a wrong answer today — the run is still reproducible given identical inputs,
which is what "deterministic" claims. But a consumer reasonably reads "fixed seed" as
"this rule's outcomes are pinned", and they are not.

What a fix looks like

Give each rule its own PRNG, seeded deterministically from the controller seed and the
rule's index (or a hash of its matchers, which survives reordering):

// per-rule, built in withOwnedRules or on UpdateConfig
rngs[i] = rand.New(rand.NewSource(seed + int64(i)))

Then rule 2's sequence is independent of everything rule 1 did, and a fixture's
assertions about one rule survive an unrelated request being added elsewhere.

Seeding from the rule's index means reordering rules still shifts outcomes; seeding from
a hash of the rule's matcher fields does not, at the cost of two rules with identical
matchers sharing a stream. The index form is simpler and probably right — worth deciding
in this issue rather than in review.

Compatibility

This changes the outcome of existing seeded runs, which is why #480 did not do it.
Any test that depends on a specific Probability outcome under a fixed seed will need
its expectations regenerated. That is a real cost and the reason to make it a deliberate,
separately-releasable change.

Rules using Times rather than Probability are unaffected: the bound needs no roll at
all, which is why #480's docs steer fixtures toward it.

Acceptance criteria

  • Each rule draws from its own PRNG, seeded deterministically from the controller
    seed.
  • The index-versus-matcher-hash seeding question is decided and the reasoning is in
    the code comment.
  • A test asserts rule 2's outcomes are unchanged when rule 1 matches a different
    number of requests — the property this issue exists to establish, and the one that
    fails today.
  • A test asserts a fixed seed still reproduces a run exactly, so the change buys
    independence without losing reproducibility.
  • docs/testing-guide.md's Probability note and FaultRule.Probability's doc
    comment are updated: the ordering caveat goes away, and the pointer to this issue
    with it.
  • CHANGELOG.md records it under ### Changed with the compatibility note above,
    because a consumer's seeded expectations move.

References

Activity

  1. added
    type: bug
    area: server
    priority: low
    on Aug 2, 2026
  2. added this to the v0.93.0 milestone on Aug 6, 2026
  3. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Picking this up for v0.93.0, alongside #540 — that issue's remedy runs through the fault tier, and a determinism defect here would undermine any new reproducibility claim there.

    The stated compatibility cost is measurably zero in-repo

    The issue's reason for parking this was that "any test that depends on a specific Probability outcome under a fixed seed will need its expectations regenerated". Measured: every Probability that actually reaches InjectFault is 1.0 or 0.0 — fault_test.go:30,53,81,96,100,125,153,627, fault_control_test.go:67,301, and test/e2e/journey_throttling_test.go:61. Both values are degenerate: 1.0 fires unconditionally and 0.0 is normalised to 1.0 by the p <= 0 guard (fault.go:184), so neither depends on where the PRNG sequence happens to be. The one intermediate value in the repo, 0.5 at config_test.go:220,236, asserts that the field round-trips through ToFaultConfig and never runs a request.

    So nothing in-repo needs regenerating. The compatibility note still belongs in the CHANGELOG, because a consumer's fixture may well use an intermediate probability — but the in-repo cost that justified deferring this is not there.

    The index-versus-matcher-hash question, decided: index

    Per the acceptance criterion asking for this to be settled here rather than in review — seed from the rule's index, and the code comment will carry the reasoning:

    • Two rules with identical matchers are legitimate and useful. "Fail with SlowDown, then fail with InternalError, then succeed" is written as two rules with the same service/operation and different error codes. A matcher hash would make them share one PRNG stream, which reintroduces exactly the cross-rule coupling this issue exists to remove — and it would do it silently, in the case where a reader is least likely to suspect it.
    • A matcher hash makes outcomes depend on matcher text. Editing a path_suffix from .parquet to .pq, or adding a header_prefix to narrow a rule, would reshuffle that rule's results even though the seed never moved. That is a worse surprise than the one it buys, because reordering rules is a visible edit to the fixture whereas retyping a matcher reads as a refinement.

    Reordering rules will therefore still shift outcomes, which is the acknowledged cost and the right one to pay.

    One refinement on the sketch in the issue body: rather than rand.NewSource(seed + int64(i)), the seed and the index get mixed through FNV. Adjacent seeds produce correlated streams in Go's default source, so seed + 0 and seed + 1 would not be as independent as the fix is meant to make them.

    The rngs slice is rebuilt in UpdateConfig as well as NewFaultController, so re-arming a configuration resets its streams exactly as it already resets each rule's Fired count — otherwise a fixture that re-arms between phases would get its full Times budget back but a half-spent PRNG.

  4. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Correction to my previous comment: the claim that "adjacent seeds produce correlated streams in Go's default source" is wrong, and I should have measured it before writing it.

    Measured over 5000 draws per stream, worst-case |Pearson r| between the streams of adjacent indices, across 60 base seeds:

    worst |corr| seed+i : 0.03008
    worst |corr| fnv    : 0.03593
    

    And for the more specific worry — whether the first draw tracks the rule index, which is what a fixture using rule 0 and rule 1 would actually notice — mean |r| over 200 base seeds with 32 rules: seed+i 0.078, FNV 0.121. Neither scheme shows a correlation the other lacks; Go's source already scrambles nearby seeds.

    The decision does not change — the reason does. The implementation still mixes seed and index through FNV, for two reasons that survive measurement:

    1. That scrambling is an unspecified property of math/rand, not a documented one. Mixing first means per-rule independence does not rest on it, so a rule's independence from its neighbours cannot be quietly changed by a Go release.
    2. seed+i makes a controller's streams collide with an adjacent controller's: seed+1 at index 0 and seed at index 1 are the same source. Two controllers seeded 1 and 2 would share streams pairwise.

    The index-over-matcher-hash decision from that comment stands unchanged and for the reasons given there; only the FNV rationale is corrected.

    Recorded because the code comment asserted the correlation too, and a comment justifying a design choice with a measurement that does not hold is worse than one that says "for robustness". Both now say what was actually measured. The corresponding mutant — seed+i in place of the FNV mix — is therefore classed EQUIVALENT rather than SURVIVED, with this measurement as its proof.

  5. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Shipped in v0.93.0 (#584). All acceptance criteria are met; ticked above.

    The index-versus-matcher-hash question resolved to index, mixed through FNV, for the reasons recorded in ruleRNGs — with one correction to my earlier comment on this issue, already posted separately: the claim that seed + int64(i) gives adjacent rules correlated streams does not survive measurement (worst |r| 0.030 for seed+i versus 0.036 for FNV over 5000 draws × 60 seeds). The mixing stays for the reasons that do hold — Go's source scrambling is an unspecified implementation detail rather than a documented property, and seed+i makes two controllers whose seeds differ by one share streams at an offset — and both the code comment and TestFaultController_AdjacentRulesAreNotCorrelated now say only what was measured.

    Tests: emulator/fault_prng_test.go (5), plus TestFaultCfg_SeedRoundTrips and TestNewFaultController_AlwaysBuiltAndSeeded from the wiring PR (#585), which is where fault.seed came from — a config-loaded probability was seeded off the wall clock before it, so reproducibility was only reachable in-process.

    Mutation-tested: rngs[i] → rngs[0] in InjectFault (CAUGHT, restores this bug) and the rngs rebuild dropped from UpdateConfig (CAUGHT).

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

    area: serverpriority: lowtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions