From 302c833480897476128d635fb39385f5919521d8 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 06:23:44 -0300 Subject: [PATCH] hash: async_heavy_test's responsive check orders the timer before the completion through a latch, never through the scheduler check_responsive started an Argon2id of 64 MiB on the pool and a 10 ms timer on the caller's io_context, and asserted that the timer's handler ran before the completion's. Nothing ordered the two: when the caller's thread was descheduled, on a loaded runner, for longer than the derivation took, the completion was queued before the loop ran, and Windows's scheduler (IOCP) ran it before the expired timer (CI run 44690351573, MSVC 14.3, async_heavy_no_exceptions: timer_fired_first failed). A delay of 500 ms between the call and the loop's run reproduces it on the msvc-lane every time. The check now runs the same libsodium Argon2id of 64 MiB on a backend of the test, `latched`, which then waits, off the caller's thread, until the test opens its latch. The loop runs once the work has started on the pool, and the timer's handler, which finds the work not completed, opens the latch: the completion cannot come first, whatever the scheduler does, and the result is still the synchronous function's. The same 500 ms delay now passes, natively and on MSVC under Wine. On the caller's thread the latch is not waited for, so that a work run there cannot hang the test. --- test/async_heavy_test.cpp | 72 +++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/test/async_heavy_test.cpp b/test/async_heavy_test.cpp index 3a16b66..505aa25 100644 --- a/test/async_heavy_test.cpp +++ b/test/async_heavy_test.cpp @@ -12,14 +12,16 @@ // engines run libsodium's and Hash2's and record the thread they run on, shows it. Each result // equals the synchronous function's; a guard's refusal completes through the executor, never inside // the call; a handler with no executor of its own completes on the pool; and a 10 ms timer on the -// caller's io_context fires while an Argon2id of 64 MiB runs. Cancellation (spec 11.5): before the -// work starts it completes at once, the work never run; after it starts, the work runs to its end -// and only then the completion is operation_aborted, the output zeroed, while a `total` one is not -// honoured; a second test backend, `gated`, holds its work until the test lets it go, and shows -// that the work reads the caller's spans themselves, never a copy, so that they are the caller's -// until the completion. The handler's associated allocator gets back every block before the handler -// runs; and co_await in Asio (use_awaitable) completes on the caller's loop, an error read through -// as_tuple without exceptions (ruling R71). +// caller's io_context fires while an Argon2id of 64 MiB is in flight on the pool, which a test +// backend, `latched`, keeps from completing until the timer's handler lets it, so that the order is +// never the scheduler's. Cancellation (spec 11.5): before the work starts it completes at once, the +// work never run; after it starts, the work runs to its end and only then the completion is +// operation_aborted, the output zeroed, while a `total` one is not honoured; another test backend, +// `gated`, holds its work until the test lets it go, and shows that the work reads the caller's +// spans themselves, never a copy, so that they are the caller's until the completion. The handler's +// associated allocator gets back every block before the handler runs; and co_await in Asio +// (use_awaitable) completes on the caller's loop, an error read through as_tuple without exceptions +// (ruling R71). #include #include @@ -98,6 +100,13 @@ std::atomic gated_password{nullptr}; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): written by the pool's thread std::atomic gated_output{nullptr}; +// The latch of the `latched` backend: whether its work has started, and whether the test lets it +// complete. Global, as the counters above. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): written by the pool's thread +std::atomic latch_entered{false}; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): set by the test +std::atomic latch_open{false}; + void record_thread() { if (std::this_thread::get_id() == caller_thread) { ++runs_on_caller; @@ -123,6 +132,10 @@ struct recording; // a work that has started and has not ended, for as long as the test needs. struct gated; +// A backend of this test whose Argon2id is libsodium's, then, off the caller's thread, waits for +// the test's latch before it returns: a heavy work that cannot complete before the test lets it. +struct latched; + } // namespace webcpp::test namespace webcpp::hash::detail { @@ -166,6 +179,32 @@ struct argon2id_engine { } }; +/** + Argon2id on `latched`: libsodium's, then, on any thread but the caller's, a wait until the test + opens the latch. On the caller's thread it does not wait, so that a work run there cannot hang the + test. +*/ +template <> +struct argon2id_engine { + static constexpr argon2id_limits limits = argon2id_engine::limits; + + [[nodiscard]] static boost::system::result derive(std::span output, + std::span password, + std::span salt, + std::uint64_t passes, + std::size_t memory) noexcept { + latch_entered = true; + auto result = + argon2id_engine::derive(output, password, salt, passes, memory); + if (std::this_thread::get_id() != caller_thread) { + while (!latch_open) { + std::this_thread::yield(); + } + } + return result; + } +}; + /** scrypt on `recording`: libsodium's, on the thread recorded. */ template <> struct scrypt_engine { @@ -222,6 +261,7 @@ struct argon2_engine { namespace { using webcpp::test::gated; +using webcpp::test::latched; using webcpp::test::recording; // What a callback saw: whether it ran, whether the initiating call had returned when it did, on @@ -870,8 +910,12 @@ void check_never_completed(asio::io_context& context) { #endif } -// The caller's event loop stays responsive: a 10 ms timer on its io_context fires while an Argon2id -// of 64 MiB (libsodium's interactive costs) runs on the pool. +// The caller's event loop stays responsive while a heavy work runs on the pool: an Argon2id of +// 64 MiB (libsodium's interactive costs) on `latched`, which cannot complete before the test lets +// it, so that the order is the test's, never the scheduler's. Once the work has started on the +// pool, a 10 ms timer on the caller's io_context fires while the work has not completed, and its +// handler lets the work complete; the completion then arrives on the caller's loop, the result the +// synchronous function's. void check_responsive(asio::io_context& context, asio::thread_pool& pool) { auto const password = bytes_of("correct horse battery staple"); std::array const salt{}; @@ -879,17 +923,23 @@ void check_responsive(asio::io_context& context, asio::thread_pool& pool) { bool derived = false; bool timer_fired = false; bool timer_fired_first = false; - hash::async_argon2id( + latch_entered = false; + latch_open = false; + hash::async_argon2id( pool.get_executor(), output, password, salt, hash::argon2id_interactive, asio::bind_executor(context.get_executor(), [&](boost::system::error_code code) { BOOST_TEST(!code); derived = true; })); + while (!latch_entered) { + std::this_thread::yield(); + } asio::steady_timer timer(context, std::chrono::milliseconds(10)); timer.async_wait([&](boost::system::error_code code) { BOOST_TEST(!code); timer_fired = true; timer_fired_first = !derived; + latch_open = true; }); context.restart(); context.run(); // lint-run: the test's event loop