Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ chronological order.
unlocked anchors while other owners remain live. Ambiguous ownership
evidence is retained. This is a compatible bug fix; public APIs and resource
protocol 2 are unchanged.
- Fixed native finite operation timeouts above the clock's nanosecond range
expiring immediately. This is a patch-level correction with no API or mapped
protocol change.

## 3.0.0 - 2026-07-16

Expand Down
6 changes: 5 additions & 1 deletion src/cpp/src/operation_budget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class OperationBudget {
}
if (!valid()) return SMS_STATUS_UNKNOWN_FAILURE;
if (is_infinite() || is_no_wait()) return SMS_STATUS_SUCCESS;
return clock::now() - started_ >= timeout_
// Compare in the public timeout unit. Converting an accepted int64
// millisecond timeout to the clock's finer ticks can overflow.
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
clock::now() - started_);
return elapsed >= timeout_
? SMS_STATUS_STORE_BUSY
: SMS_STATUS_SUCCESS;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/cpp/atomic_budget_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <thread>

static_assert(sizeof(void*) == 8);
Expand Down Expand Up @@ -85,6 +86,27 @@ int main() {
SMS_CHECK(finite.try_continue_after_contention(0, terminal));
SMS_CHECK(terminal == SMS_STATUS_SUCCESS);

// These finite millisecond values exceed the signed nanosecond range.
// Checking a fresh budget must not multiply the timeout into overflow.
for (const auto timeout : {
std::chrono::milliseconds{9'223'372'036'854LL},
std::chrono::milliseconds{9'223'372'036'855LL},
std::chrono::milliseconds{std::numeric_limits<std::int64_t>::max()}}) {
auto large = OperationBudget::start_at(timeout, now);
SMS_CHECK(large.valid());
SMS_CHECK(!large.is_infinite());
SMS_CHECK(large.check() == SMS_STATUS_SUCCESS);
SMS_CHECK(large.check_periodic(64) == SMS_STATUS_SUCCESS);
terminal = SMS_STATUS_UNKNOWN_FAILURE;
SMS_CHECK(large.try_continue_after_contention(0, terminal));
SMS_CHECK(terminal == SMS_STATUS_SUCCESS);

CancellationFlag large_cancellation;
auto cancelable_large = OperationBudget::start_at(timeout, now, &large_cancellation);
large_cancellation.cancel();
SMS_CHECK(cancelable_large.check() == SMS_STATUS_OPERATION_CANCELED);
}

auto expired = OperationBudget::start_at(1ms, now - 10ms);
SMS_CHECK(expired.check() == SMS_STATUS_STORE_BUSY);
SMS_CHECK(expired.check_periodic(64) == SMS_STATUS_STORE_BUSY);
Expand Down
Loading