|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2024 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 15 | + * Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <catch2/catch_message.hpp> |
| 24 | +#include <catch2/catch_test_macros.hpp> |
| 25 | +#include <memory> |
| 26 | +#include <mutex> |
| 27 | +#include <string> |
| 28 | +#include <utility> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include "nuclear" |
| 32 | +#include "test_util/diff_string.hpp" |
| 33 | + |
| 34 | +// Deterministic regression test for a premature global-idle epoch, WITHOUT relying on any sleeps or |
| 35 | +// timing windows. |
| 36 | +// |
| 37 | +// Topology: |
| 38 | +// * outer - runs on a single-worker WorkerPool while holding the Sync group token, and |
| 39 | +// dispatches the re-entrant inner task onto that same pool while STILL holding the |
| 40 | +// token (so it cannot run and instead parks as an external waiter). |
| 41 | +// * inner - the re-entrant task: same Sync group, same WorkerPool, LOW priority. |
| 42 | +// * on<Idle<>> - the global idle reaction, also pinned to the single-worker WorkerPool, at |
| 43 | +// REALTIME priority. |
| 44 | +// |
| 45 | +// The bug: when the WorkerPool worker is woken by the parked inner task's pending_idle latch, the |
| 46 | +// buggy scheduler fires a global idle epoch BEFORE dequeuing the (now drained and runnable) inner |
| 47 | +// task, dropping the pool's active count and releasing its active_pools slot while real work is |
| 48 | +// queued. |
| 49 | +// |
| 50 | +// Why this is deterministic (no sleeps): |
| 51 | +// * Submission of the inner task is synchronous inside the outer reaction, so while the outer |
| 52 | +// reaction still holds the Sync token the inner task is parked (arming pending_idle) before the |
| 53 | +// outer reaction returns. Returning releases the token and drains the inner task into the |
| 54 | +// WorkerPool queue as a runnable task. |
| 55 | +// * The inner task and the idle reaction share the SAME single-worker WorkerPool, so they can never |
| 56 | +// run concurrently - the one worker serializes them. |
| 57 | +// * The idle reaction is REALTIME and the inner task is LOW. In the buggy case the premature idle |
| 58 | +// submits the idle reaction into the same queue that already holds the drained inner task; the |
| 59 | +// single worker then dequeues the higher-priority idle reaction FIRST, so it observes the still |
| 60 | +// pending inner task (awaiting_inner == true) and records the violation. In the fixed case the |
| 61 | +// worker dequeues the inner task first (no idle epoch), so the idle reaction never runs while the |
| 62 | +// inner task is pending. |
| 63 | +// |
| 64 | +// Result: without the fix the event sequence deterministically contains an extra |
| 65 | +// "idle-while-inner-pending" entry; with the fix it is exactly {"outer", "inner"}. |
| 66 | + |
| 67 | +namespace { |
| 68 | + |
| 69 | +struct WorkerPool { |
| 70 | + static constexpr int concurrency = 1; |
| 71 | +}; |
| 72 | +struct SyncGroup {}; |
| 73 | + |
| 74 | +struct Outer {}; |
| 75 | +struct Inner {}; |
| 76 | + |
| 77 | +// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) |
| 78 | +std::mutex mtx; |
| 79 | +std::vector<std::string> events; |
| 80 | +bool awaiting_inner = false; |
| 81 | +bool inner_done = false; |
| 82 | +bool shutting_down = false; |
| 83 | +// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) |
| 84 | + |
| 85 | +class TestReactor : public NUClear::Reactor { |
| 86 | +public: |
| 87 | + explicit TestReactor(std::unique_ptr<NUClear::Environment> environment) : Reactor(std::move(environment)) { |
| 88 | + |
| 89 | + // Global idle, pinned to the (single-worker) WorkerPool at REALTIME so that if the buggy |
| 90 | + // scheduler submits it while the drained inner task is still queued on the same pool, it is |
| 91 | + // dequeued before the lower-priority inner task - making the observation deterministic. |
| 92 | + on<Idle<>, Pool<WorkerPool>, Priority::REALTIME>().then([this] { |
| 93 | + bool do_shutdown = false; |
| 94 | + { |
| 95 | + const std::lock_guard<std::mutex> lock(mtx); |
| 96 | + if (awaiting_inner) { |
| 97 | + // Real work (the parked-then-drained inner task) is still pending on this pool, yet |
| 98 | + // a global idle epoch has fired: this is the premature-idle bug. |
| 99 | + events.push_back("idle-while-inner-pending"); |
| 100 | + } |
| 101 | + else if (inner_done && !shutting_down) { |
| 102 | + shutting_down = true; |
| 103 | + do_shutdown = true; |
| 104 | + } |
| 105 | + } |
| 106 | + if (do_shutdown) { |
| 107 | + powerplant.shutdown(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // Kick off exactly one cycle. |
| 112 | + on<Startup>().then([this] { emit(std::make_unique<Outer>()); }); |
| 113 | + |
| 114 | + // The outer reaction: holds the Sync token on the WorkerPool and dispatches the re-entrant |
| 115 | + // inner task onto that same pool while still holding that token. |
| 116 | + on<Trigger<Outer>, Sync<SyncGroup>, Pool<WorkerPool>, Priority::REALTIME>().then([this] { |
| 117 | + { |
| 118 | + const std::lock_guard<std::mutex> lock(mtx); |
| 119 | + awaiting_inner = true; |
| 120 | + events.push_back("outer"); |
| 121 | + } |
| 122 | + // Submitted synchronously: because we still hold the Sync<SyncGroup> token, the inner task |
| 123 | + // fails the group lock and PARKS as an external waiter (arming the WorkerPool's |
| 124 | + // pending_idle latch) before this reaction returns. Returning then releases the token, |
| 125 | + // draining the parked inner task into the WorkerPool queue as a runnable task. |
| 126 | + emit(std::make_unique<Inner>()); |
| 127 | + }); |
| 128 | + |
| 129 | + // The re-entrant inner task: same Sync group, same WorkerPool, lower priority than idle. |
| 130 | + on<Trigger<Inner>, Sync<SyncGroup>, Pool<WorkerPool>, Priority::LOW>().then([this] { |
| 131 | + const std::lock_guard<std::mutex> lock(mtx); |
| 132 | + events.push_back("inner"); |
| 133 | + awaiting_inner = false; |
| 134 | + inner_done = true; |
| 135 | + }); |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +} // namespace |
| 140 | + |
| 141 | +TEST_CASE("Test global idle does not fire while a parked Sync waiter is pending", "[api][dsl][Idle][Pool][Sync]") { |
| 142 | + |
| 143 | + { |
| 144 | + const std::lock_guard<std::mutex> lock(mtx); |
| 145 | + events.clear(); |
| 146 | + awaiting_inner = false; |
| 147 | + inner_done = false; |
| 148 | + shutting_down = false; |
| 149 | + } |
| 150 | + |
| 151 | + NUClear::Configuration config; |
| 152 | + config.default_pool_concurrency = 1; |
| 153 | + NUClear::PowerPlant powerplant(config); |
| 154 | + powerplant.install<TestReactor>(); |
| 155 | + powerplant.start(); |
| 156 | + |
| 157 | + const std::vector<std::string> expected = { |
| 158 | + "outer", |
| 159 | + "inner", |
| 160 | + }; |
| 161 | + |
| 162 | + INFO(test_util::diff_string(expected, events)); |
| 163 | + |
| 164 | + REQUIRE(events == expected); |
| 165 | +} |
0 commit comments