diff --git a/CHANGELOG.md b/CHANGELOG.md index fc3578c..a70017d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/cpp/src/operation_budget.hpp b/src/cpp/src/operation_budget.hpp index c7d10a8..277aff2 100644 --- a/src/cpp/src/operation_budget.hpp +++ b/src/cpp/src/operation_budget.hpp @@ -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( + clock::now() - started_); + return elapsed >= timeout_ ? SMS_STATUS_STORE_BUSY : SMS_STATUS_SUCCESS; } diff --git a/tests/cpp/atomic_budget_tests.cpp b/tests/cpp/atomic_budget_tests.cpp index aebc45f..d2fb287 100644 --- a/tests/cpp/atomic_budget_tests.cpp +++ b/tests/cpp/atomic_budget_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include static_assert(sizeof(void*) == 8); @@ -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::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);