Skip to content

cache: budget counters wrap below zero #72

Closed

Description

@zfarrell

Found by a memory-accounting audit of 8b3c813. The tests are on branch audit/memory-tests and fail on main today; check out that branch to run them. File and line links point at that commit.

What goes wrong.

Evidence.

  • release_disk(1) on an empty budget left disk usage at 18,446,744,073,709,551,615, and the next try_reserve_disk(1) panicked with "attempt to add with overflow" (budget.rs:83).
  • The memory path behaves the same (budget.rs:40).

Tests.

Test File Command Flags
audit_release_disk_beyond_usage_does_not_wrap src/core/src/cache/budget.rs:240 cargo +1.96.0 test -p liquid-cache --lib audit_release_disk_beyond_usage_does_not_wrap -- --nocapture
audit_memory_shrink_beyond_usage_does_not_wrap src/core/src/cache/budget.rs:264 cargo +1.96.0 test -p liquid-cache --lib audit_memory_shrink_beyond_usage_does_not_wrap -- --nocapture Inline

Inline test (append inside mod tests in budget.rs; it uses the existing test_budget):

    fn describe_next_reservation(reserve: impl FnOnce() -> Result<(), ()>) -> String {
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(reserve)) {
            Ok(result) => format!("returned {result:?}"),
            Err(panic) => format!(
                "panicked: {}",
                panic
                    .downcast_ref::<&str>()
                    .map(|s| s.to_string())
                    .or_else(|| panic.downcast_ref::<String>().cloned())
                    .unwrap_or_else(|| "<non-string panic>".to_string())
            ),
        }
    }

    #[test]
    fn audit_memory_shrink_beyond_usage_does_not_wrap() {
        let max_memory = 1000;
        let budget = test_budget(max_memory, usize::MAX);

        budget
            .try_update_memory_usage(1, 0)
            .expect("a release cannot fail");
        let usage = budget.memory_usage_bytes();
        let next = describe_next_reservation(|| budget.try_reserve_memory(1));
        assert!(
            usage <= max_memory,
            "try_update_memory_usage(1, 0) on an empty budget left memory usage at \
             {usage}, over the limit of {max_memory}; the next try_reserve_memory(1) {next}"
        );
        assert_eq!(usage, 0);
    }

Heads-up: the existing test a_spill_installs_its_record_when_the_memory_budget_reads_full (core.rs:2215) relies on this wrap. It calls try_update_memory_usage(1, 0) to push usage over the limit, so its setup needs changing once this is fixed. Related: #30.

Flags: P probabilistic (a pass after a fix only means the race wasn't hit), F interleaving forced by a test hook, L Linux only, D needs Docker, M measurement only, C control (passes by design).

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

    bugSomething isn't workingmemoryMemory accounting or memory usage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions