Files
Library/tests/standalone/Plugin/HotReloadUnderLoad.cpp
T
faumarayandClaude Fable 5 b343bce396 Add Plugin hot-plug: PluginManager, versioned state ABI, file watching
Backward-compatible C ABI v2 (Plugin.hpp; ABI_VERSION unchanged, IPlugin
vtable frozen): optional void_plugin_state_version / serialize_state /
free_string / deserialize_state(inst, json, from_version). State transfer
prefers serialize->deserialize (survives layout changes; migrations live
in the new .so via Json::MigrationChain), falling back to the existing
transfer_state, then no-op. PluginLoader::reload gains this chain; the
shared transfer_plugin_state helper drives it. Existing ABI-v1 plugins
(TestPlugin) load and reload unchanged.

PluginManager (Manager.hpp/.cpp): multi-plugin registry with RCU-style
pinned handles. acquire(name) is one atomic<shared_ptr> load returning a
PluginHandle that keeps its instance alive for the whole call. reload()
is transactional: build new instance, transfer state from old, then
atomic-exchange it in; on any failure the old instance stays. In-flight
handles keep the old instance and its mapped .so alive (dlclose_on_swap=
false) until they drop, so work never stops and callers never see a torn
or freed object. watch(name) auto-reloads via FileSystem::FileWatcher
(debounced); on_reloaded fires after each swap.

Reload-safety: dlopen of a fresh-inode hotswap copy is required to load
new bytes; deployments must install the new .so by atomic rename, never
in-place overwrite of the mapped file (corrupts the running mapping ->
SIGBUS). Documented; tests install via copy-to-.incoming + rename.

Tests: StatefulPluginV1/V2 (shared IStateful interface, atomic counter;
v2 adds a field and migrates v1 JSON). Manager.cpp covers load/acquire,
v1->v2 migration, pinned-handle-survives-reload, legacy transfer_state
fallback, and watch auto-reload. HotReloadUnderLoad.cpp: 6 reader threads
calling through acquire() while 24 v1<->v2 reloads run concurrently --
zero crashes, state survives every migration, ASan+UBSan clean, stable
across 10+ runs.

965 tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-13 18:08:06 +03:00

109 lines
3.1 KiB
C++

#include <Void/Plugin/Manager.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include "Stateful.hpp"
#include <atomic>
#include <chrono>
#include <filesystem>
#include <thread>
#include <vector>
using namespace Void::Plugin;
#if defined(VOID_STATEFUL_V1_PATH) && defined(VOID_STATEFUL_V2_PATH) && \
__has_include(<dlfcn.h>)
namespace
{
// Atomic-rename install (fresh inode) — overwriting a mapped .so in
// place corrupts the running mapping (SIGBUS).
void install(const std::filesystem::path& src,
const std::filesystem::path& dst)
{
auto staging = dst;
staging += ".incoming";
std::filesystem::copy_file(
src, staging, std::filesystem::copy_options::overwrite_existing);
std::filesystem::rename(staging, dst);
}
} // namespace
// @tag stress, concurrency
Void::Testing::Test hot_reload_under_concurrent_load()
{
Void::FileSystem::TempDir tmp{"void_hotload"};
const auto so = tmp.path() / "plugin.so";
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
PluginManager mgr;
Void::Testing::expect(mgr.load(so).has_value());
std::atomic<bool> stop{false};
std::atomic<long long> ops{0};
std::atomic<bool> sane{true};
std::atomic<int> crashes{0};
// Reader threads continuously acquire a pinned handle and use the
// plugin through it. acquire() returns a handle that keeps whichever
// instance was current alive for the whole call — so even as the main
// thread swaps instances underneath, no reader ever touches a freed
// object (ASan would catch it otherwise).
std::vector<std::thread> readers;
readers.reserve(6);
for (int r = 0; r < 6; ++r) {
readers.emplace_back([&] {
while (!stop.load(std::memory_order_relaxed)) {
auto h = mgr.acquire("StatefulPlugin");
if (!h) {
crashes.fetch_add(1);
continue;
}
auto* s = VoidTest::as_stateful(h.get());
const int before = s->counter();
s->bump();
const int after = s->counter();
// The pinned instance is consistent within the call.
if (after < before + 1) {
sane.store(false);
}
const int v = s->schema_version();
if (v != 1 && v != 2) {
sane.store(false);
}
ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Main thread flips the file v1<->v2 and reloads repeatedly.
for (int i = 0; i < 24; ++i) {
const char* src =
(i % 2 == 0) ? VOID_STATEFUL_V2_PATH : VOID_STATEFUL_V1_PATH;
install(src, so);
const auto r = mgr.reload("StatefulPlugin");
Void::Testing::expect(r.has_value());
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
stop.store(true);
for (auto& t : readers) {
t.join();
}
Void::Testing::expect(sane.load());
Void::Testing::expect_eq(crashes.load(), 0);
Void::Testing::expect(ops.load() > 0);
// State survived every migration: the final instance's counter is at
// least the number of reloads' worth of preserved bumps (monotonic,
// never reset to zero by a swap).
auto final = mgr.acquire("StatefulPlugin");
Void::Testing::expect(static_cast<bool>(final));
Void::Testing::expect(VoidTest::as_stateful(final.get())->counter() > 0);
}
#endif // stateful plugins available