Skip to content

Bounded counter retries can unnecessarily shrink reservations #237

Open

Description

@0z5a

Bug

atomic_bounded_counter::add_bounded and sub_bounded overwrite the caller's requested diff before compare_exchange_weak succeeds. If the exchange fails and another thread subsequently frees capacity, the retry uses the already-reduced amount, even though a larger amount would fit at the successful exchange. sub_bounded has the analogous problem when the counter increases during a retry.

add_bounded is used by host, device and disk reserve_upto paths, so concurrent release and reservation can result in an unnecessarily small reservation. It does not exceed the limit.

Reproduction

On main b09010a, a C++20 stress test repeatedly requests an addition of 10 with an upper bound of 10 while another thread alternates the atomic counter between 0 and 9. The operation has returned a nonzero addition with a resulting value below 10. For example, a retry that first observes 9 reduces the request to 1; if its exchange fails and the next observed value is 0, it can successfully add only 1 instead of 10.

The stress test observed the underfilled result with Clang 21 and libc++ on a CPU. Thread scheduling is nondeterministic. No GPU workload or performance impact was measured.

Expected behavior

Each retry should calculate the bounded amount from the original requested diff and the latest observed counter. Publish the actual amount only after the exchange succeeds, or zero when no capacity is available.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions