nautechsystems/nautilus_trader · error

Division result should be non-zero after validation

Error message

Division result should be non-zero after validation

What it means

Quota::from_gcra_parameters computes max_burst = tau/t and asserts the result fits u32 and is non-zero. The assertions above guarantee both, so this expect is a defensive invariant; it only fires if the preceding validation logic is wrong (division_result == 0 slipping through).

Source

Thrown at crates/network/src/ratelimiter/quota.rs:165

        let t_u64 = t.as_u64();
        let tau_u64 = tau.as_u64();

        // Validate division won't be zero or overflow
        assert!(t_u64 != 0, "Invalid GCRA parameter: t cannot be zero");

        let division_result = tau_u64 / t_u64;
        assert!(
            division_result != 0,
            "Invalid GCRA parameters: tau/t results in zero burst capacity"
        );
        assert!(
            u32::try_from(division_result).is_ok(),
            "Invalid GCRA parameters: tau/t exceeds u32::MAX"
        );

        // We've verified the result is non-zero and fits in u32
        let max_burst = NonZeroU32::new(division_result as u32)
            .expect("Division result should be non-zero after validation");
        let replenish_1_per = t.into();
        Self {
            max_burst,
            replenish_1_per,
        }
    }
}

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Validate that tau >= t (so the division is at least 1) before calling from_gcra_parameters.
  2. Check the config values for the GCRA parameters; tau/t=0 is an invalid quota definition.
  3. If reachable with valid inputs, report as a bug in the validation order.

Example fix

// before
let quota = Quota::from_gcra_parameters(tau, t); // panics if tau/t truncates to 0
// after
assert!(tau >= t, "GCRA tau must be >= t to yield a non-zero burst");
let quota = Quota::from_gcra_parameters(tau, t);
Defensive patterns

Strategy: validation

Validate before calling

fn valid_gcra(tau: u64, t: u64) -> bool { tau >= t && tau / t > 0 && tau / t <= u32::MAX as u64 }

Try / catch

let quota = std::panic::catch_unwind(|| Quota::from_gcra_parameters(tau, t));

Prevention

When it happens

Trigger: Calling Quota::from_gcra_parameters with tau/t values whose validated division_result is zero — normally unreachable because earlier asserts check non-zero and u32 fit; a logic bug or integer truncation (e.g. tau < t yielding 0 after division) could trigger it.

Common situations: Constructing a quota from raw GCRA parameters where tau (capacity interval) is smaller than t (cell interval), producing a truncated zero burst; misconfigured rate-limit configs from external systems.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/54b3336930a63da2. Report an issue: GitHub.