|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// Concurrent registration + lookup stress test for detail::ws_registry. |
| 22 | +// |
| 23 | +// ws_registry's class comment documents "its mutex is independent of |
| 24 | +// every other cluster's; no call site holds two of them at once" -- |
| 25 | +// but unlike route_table (route_table_concurrency.cpp) and hook_bus |
| 26 | +// (folded into threadsafety_stress.cpp), ws_registry had zero |
| 27 | +// concurrency verification anywhere in the suite. This binary mirrors |
| 28 | +// route_table_concurrency.cpp's pattern: N writer threads doing |
| 29 | +// try_register/unregister and M reader threads doing find/empty |
| 30 | +// against disjoint and overlapping keys. Without correct lock |
| 31 | +// discipline in ws_registry's shared_mutex (unique_lock for |
| 32 | +// try_register/unregister, shared_lock for find/empty), this test |
| 33 | +// would deadlock, crash, or (under TSan) report a data race. |
| 34 | +// |
| 35 | +// Unlike route_table_concurrency.cpp, this test needs no webserver / |
| 36 | +// MHD daemon at all -- ws_registry is a self-contained collaborator |
| 37 | +// (it only stores/erases/copies shared_ptr<websocket_handler>, never |
| 38 | +// dereferencing the pointee), so the stress driver talks to a bare |
| 39 | +// instance directly. |
| 40 | +// |
| 41 | +// **TSan gate:** rebuild with `CXXFLAGS="-fsanitize=thread -g -O1" |
| 42 | +// LDFLAGS="-fsanitize=thread"` and re-run this binary; the tsan CI lane |
| 43 | +// (`build-type: tsan` in .github/workflows/verify-build.yml) picks it |
| 44 | +// up automatically via `make check` (no separate wiring needed -- |
| 45 | +// route_table_concurrency required the RTC_ITERATIONS repeat gate |
| 46 | +// because it is TASK-092's headline stress target; this binary rides |
| 47 | +// the ordinary single-pass `make check` under that same tsan lane). |
| 48 | + |
| 49 | +#include <atomic> |
| 50 | +#include <chrono> |
| 51 | +#include <memory> |
| 52 | +#include <string> |
| 53 | +#include <thread> |
| 54 | +#include <vector> |
| 55 | + |
| 56 | +#include "./httpserver.hpp" |
| 57 | +#include "./httpserver/detail/ws_registry.hpp" |
| 58 | +#include "./littletest.hpp" |
| 59 | +#include "./test_utils.hpp" |
| 60 | + |
| 61 | +namespace ht = httpserver; |
| 62 | +namespace htd = httpserver::detail; |
| 63 | + |
| 64 | +namespace { |
| 65 | +class noop_ws_handler : public ht::websocket_handler { |
| 66 | + public: |
| 67 | + void on_message(ht::websocket_session&, std::string_view) override {} |
| 68 | +}; |
| 69 | +} // namespace |
| 70 | + |
| 71 | +LT_BEGIN_SUITE(ws_registry_concurrency_suite) |
| 72 | + void set_up() {} |
| 73 | + void tear_down() {} |
| 74 | +LT_END_SUITE(ws_registry_concurrency_suite) |
| 75 | + |
| 76 | +LT_BEGIN_AUTO_TEST(ws_registry_concurrency_suite, |
| 77 | + concurrent_register_and_find_no_data_race) |
| 78 | + htd::ws_registry reg; |
| 79 | + |
| 80 | + // Pre-register a stable set of keys so readers always have |
| 81 | + // something to find regardless of writer timing. |
| 82 | + for (int i = 0; i < 32; ++i) { |
| 83 | + reg.try_register("/stable/" + std::to_string(i), |
| 84 | + std::make_shared<noop_ws_handler>()); |
| 85 | + } |
| 86 | + |
| 87 | + std::atomic<bool> stop{false}; |
| 88 | + std::atomic<int> writer_ops{0}; |
| 89 | + std::atomic<int> reader_ops{0}; |
| 90 | + |
| 91 | + constexpr int kWriters = 4; |
| 92 | + constexpr int kReaders = 16; |
| 93 | + |
| 94 | + std::vector<std::thread> threads; |
| 95 | + threads.reserve(kWriters + kReaders); |
| 96 | + |
| 97 | + // Writers: register / unregister disjoint keys in their own numeric |
| 98 | + // range so try_register's duplicate-rejection path (false return) |
| 99 | + // is exercised only by the intentional re-register-after-unregister |
| 100 | + // sequence below, not by cross-writer collisions. |
| 101 | + for (int w = 0; w < kWriters; ++w) { |
| 102 | + threads.emplace_back([&, w] { |
| 103 | + int counter = 0; |
| 104 | + while (!stop.load(std::memory_order_relaxed)) { |
| 105 | + std::string key = "/dyn/" + std::to_string(w) + "/" |
| 106 | + + std::to_string(counter % 8); |
| 107 | + (void)reg.try_register(key, std::make_shared<noop_ws_handler>()); |
| 108 | + reg.unregister(key); |
| 109 | + ++counter; |
| 110 | + writer_ops.fetch_add(1, std::memory_order_relaxed); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + // Readers: hammer find()/empty() against the stable + dynamic keys. |
| 116 | + for (int r = 0; r < kReaders; ++r) { |
| 117 | + threads.emplace_back([&, r] { |
| 118 | + int counter = 0; |
| 119 | + while (!stop.load(std::memory_order_relaxed)) { |
| 120 | + std::string key = "/stable/" + std::to_string(counter % 32); |
| 121 | + (void)reg.find(key); |
| 122 | + std::string key2 = "/dyn/" + std::to_string(r % 4) + "/" |
| 123 | + + std::to_string(counter % 8); |
| 124 | + (void)reg.find(key2); |
| 125 | + (void)reg.empty(); |
| 126 | + ++counter; |
| 127 | + reader_ops.fetch_add(1, std::memory_order_relaxed); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + // Run until enough operations have been observed by both roles, or |
| 133 | + // until a wall-clock safety ceiling (30s) to survive valgrind/TSan |
| 134 | + // overhead. Mirrors route_table_concurrency.cpp's threshold-based |
| 135 | + // stop condition (avoids a fixed wall-clock sleep). |
| 136 | + constexpr int kOpThreshold = 10000; |
| 137 | + auto deadline = |
| 138 | + std::chrono::steady_clock::now() + std::chrono::seconds(30); |
| 139 | + while ((writer_ops.load() < kOpThreshold || reader_ops.load() < kOpThreshold) |
| 140 | + && std::chrono::steady_clock::now() < deadline) { |
| 141 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 142 | + } |
| 143 | + stop.store(true, std::memory_order_relaxed); |
| 144 | + for (auto& t : threads) t.join(); |
| 145 | + |
| 146 | + // We don't assert specific counts -- the gate is "completed without |
| 147 | + // deadlock or crash". TSan-detected races would break the build |
| 148 | + // when this TU is rebuilt under the manual TSan gate. Liveness (both |
| 149 | + // roles made progress) can be defeated by valgrind's single-core |
| 150 | + // scheduler, so only assert it off-valgrind (same posture as |
| 151 | + // route_table_concurrency.cpp). |
| 152 | + if (!under_valgrind()) { |
| 153 | + LT_CHECK(writer_ops.load() > 0); |
| 154 | + LT_CHECK(reader_ops.load() > 0); |
| 155 | + } |
| 156 | +LT_END_AUTO_TEST(concurrent_register_and_find_no_data_race) |
| 157 | + |
| 158 | +LT_BEGIN_AUTO_TEST_ENV() |
| 159 | + AUTORUN_TESTS() |
| 160 | +LT_END_AUTO_TEST_ENV() |
0 commit comments