diff --git a/include/exec/detail/bwos_lifo_queue.hpp b/include/exec/detail/bwos_lifo_queue.hpp index 02a87fbc2..b104d7b98 100644 --- a/include/exec/detail/bwos_lifo_queue.hpp +++ b/include/exec/detail/bwos_lifo_queue.hpp @@ -129,6 +129,9 @@ namespace experimental::execution::bwos auto push_back(Tp value) noexcept -> bool; + // Pushes items from [first, last) until the queue is full, and returns + // where it stopped. Thieves can steal the pushed items before it returns + // (see GRANTING BLOCKS). template auto push_back(Iterator first, Sentinel last) noexcept -> Iterator; diff --git a/include/exec/static_thread_pool.hpp b/include/exec/static_thread_pool.hpp index 12a146d55..07e62bef6 100644 --- a/include/exec/static_thread_pool.hpp +++ b/include/exec/static_thread_pool.hpp @@ -892,9 +892,14 @@ namespace experimental::execution bwos::lifo_queue<_task_base*, numa_allocator<_task_base*>>& local_queue) { auto const last = local_queue.push_back(pending_queue.begin(), pending_queue.end()); - __intrusive_queue<&_task_base::next_> tmp{}; - tmp.splice(tmp.begin(), pending_queue, pending_queue.begin(), last); - tmp.clear(); + // Thieves can steal the tasks that push_back() moved to local_queue, and + // run and destroy them, before it returns. So drop them from + // pending_queue without touching them: splicing them out would write to + // the last one. + _task_base* const head = last == pending_queue.end() ? nullptr : *last; + _task_base* const tail = head == nullptr ? nullptr : pending_queue.back(); + pending_queue.clear(); + pending_queue = __intrusive_queue<&_task_base::next_>(head, tail); } inline auto _static_thread_pool::_thread_state::try_remote(_remote_poll_mode mode) diff --git a/test/exec/test_static_thread_pool.cpp b/test/exec/test_static_thread_pool.cpp index 87bfffd79..10e335958 100644 --- a/test/exec/test_static_thread_pool.cpp +++ b/test/exec/test_static_thread_pool.cpp @@ -486,6 +486,34 @@ TEST_CASE("a task on static_thread_pool can wait for a task that it schedules on } } +// A worker moves the tasks in its remote queues to its local queue, where +// thieves can steal, run, and destroy them right away when the blocks hold one +// task each. The worker used to write to the last task it moved after that, +// which TSan reported as a data race, and which lost tasks when the write hit +// a new task that reused the memory. +TEST_CASE("static_thread_pool does not touch a task after a thief can steal it", + "[types][static_thread_pool]") +{ + constexpr int num_tasks = 20'000; + std::atomic completed{0}; // outlives the pool + exec::static_thread_pool pool{8, exec::bwos_params{.blockSize = 1}}; + auto sched = pool.get_scheduler(); + for (int i = 0; i < num_tasks; ++i) + { + exec::start_detached( + ex::schedule(sched) + | ex::then([&completed] { completed.fetch_add(1, std::memory_order_relaxed); })); + } + + auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); + while (completed.load(std::memory_order_relaxed) < num_tasks + && std::chrono::steady_clock::now() < deadline) + { + std::this_thread::yield(); + } + CHECK(completed.load(std::memory_order_relaxed) == num_tasks); +} + TEST_CASE("tasks that a task on static_thread_pool schedules run in parallel with it", "[types][static_thread_pool]") {