Repository navigation
Description
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:
-
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
HeadBucketprobe in a helper, say — and every subsequent DynamoDB roll shifts. The
DynamoDB assertions change with a seed that never moved. -
A retry. A
probability: 0.5rule 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'sProbabilitynote andFaultRule.Probability's doc
comment are updated: the ordering caveat goes away, and the pointer to this issue
with it. -
CHANGELOG.mdrecords it under### Changedwith the compatibility note above,
because a consumer's seeded expectations move.
References
emulator/fault.go—FaultController.rng,InjectFault,FaultRule.Probability- Fault injection: S3 rules can't name an operation, can't be bounded to N fires, and drop ErrorCode on the wire #480 — the fault-injection fixes that documented this rather than changing it
docs/testing-guide.md§ Fault Injection
Activity
scttfrdmn commentedon Aug 6, 2026
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
Probabilityoutcome under a fixed seed will need its expectations regenerated". Measured: everyProbabilitythat actually reachesInjectFaultis1.0or0.0—fault_test.go:30,53,81,96,100,125,153,627,fault_control_test.go:67,301, andtest/e2e/journey_throttling_test.go:61. Both values are degenerate:1.0fires unconditionally and0.0is normalised to1.0by thep <= 0guard (fault.go:184), so neither depends on where the PRNG sequence happens to be. The one intermediate value in the repo,0.5atconfig_test.go:220,236, asserts that the field round-trips throughToFaultConfigand 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:
SlowDown, then fail withInternalError, then succeed" is written as two rules with the sameservice/operationand 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.path_suffixfrom.parquetto.pq, or adding aheader_prefixto 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, soseed + 0andseed + 1would not be as independent as the fix is meant to make them.The
rngsslice is rebuilt inUpdateConfigas well asNewFaultController, so re-arming a configuration resets its streams exactly as it already resets each rule'sFiredcount — otherwise a fixture that re-arms between phases would get its fullTimesbudget back but a half-spent PRNG.scttfrdmn commentedon Aug 6, 2026
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:
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+i0.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:
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.seed+imakes a controller's streams collide with an adjacent controller's:seed+1at index 0 andseedat 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+iin place of the FNV mix — is therefore classed EQUIVALENT rather than SURVIVED, with this measurement as its proof.fix(fault): each rule draws from its own PRNG (#510) (#584)
scttfrdmn commentedon Aug 6, 2026
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 thatseed + int64(i)gives adjacent rules correlated streams does not survive measurement (worst |r| 0.030 forseed+iversus 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, andseed+imakes two controllers whose seeds differ by one share streams at an offset — and both the code comment andTestFaultController_AdjacentRulesAreNotCorrelatednow say only what was measured.Tests:
emulator/fault_prng_test.go(5), plusTestFaultCfg_SeedRoundTripsandTestNewFaultController_AlwaysBuiltAndSeededfrom the wiring PR (#585), which is wherefault.seedcame from — a config-loadedprobabilitywas seeded off the wall clock before it, so reproducibility was only reachable in-process.Mutation-tested:
rngs[i]→rngs[0]inInjectFault(CAUGHT, restores this bug) and therngsrebuild dropped fromUpdateConfig(CAUGHT).