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>
This commit is contained in:
1 parent
697e07843c
commit
b343bce396
13 files changed
+1179
-38
No files matched your search
@@ -16,6 +16,8 @@ enum class LoadError {
|
||||
AbiMismatch,
|
||||
CreateFailed,
|
||||
NotSupported, // platform does not support dlopen
|
||||
StateRejected, // new instance refused the serialized state
|
||||
UnknownPlugin, // PluginManager: no plugin registered under that name
|
||||
};
|
||||
|
||||
[[nodiscard]] std::string_view to_string(LoadError e) noexcept;
|
||||
@@ -50,18 +52,31 @@ public:
|
||||
|
||||
IPlugin* operator->() noexcept { return plugin_; }
|
||||
|
||||
// Hot-reload: copies current .so to a temp file (fresh inode), loads it,
|
||||
// transfers state, then destroys old instance and swaps handles.
|
||||
// Hot-reload: copies current .so to a temp file (fresh inode), loads
|
||||
// it, transfers state, then destroys old instance and swaps handles.
|
||||
// State transfer prefers the v2 serialize/deserialize protocol (old
|
||||
// serializes, new deserializes with the old schema version); falls
|
||||
// back to void_plugin_transfer_state; else no transfer.
|
||||
[[nodiscard]] std::expected<void, LoadError> reload();
|
||||
|
||||
private:
|
||||
PluginLoader() = default;
|
||||
|
||||
void* handle_ {};
|
||||
IPlugin* plugin_ {};
|
||||
destroy_fn destroy_ {};
|
||||
transfer_fn transfer_{};
|
||||
PluginInfo info_ {};
|
||||
// Moves plugin state old -> new during a reload (v2 serialize/
|
||||
// deserialize, else transfer_state). Returns false only if the new
|
||||
// instance rejects the state.
|
||||
friend bool transfer_plugin_state(const PluginLoader& from,
|
||||
PluginLoader& to);
|
||||
|
||||
void* handle_ {};
|
||||
IPlugin* plugin_ {};
|
||||
destroy_fn destroy_ {};
|
||||
transfer_fn transfer_ {};
|
||||
state_version_fn state_version_{};
|
||||
serialize_fn serialize_ {};
|
||||
free_string_fn free_string_ {};
|
||||
deserialize_fn deserialize_ {};
|
||||
PluginInfo info_ {};
|
||||
std::filesystem::path path_;
|
||||
|
||||
[[nodiscard]] std::expected<void, LoadError>
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
#include <Void/System/FileWatcher.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <expected>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Plugin
|
||||
{
|
||||
enum class LoadError; // from Loader.hpp
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// One loaded .so + its instance, with the C ABI entry points
|
||||
// resolved. Destruction runs on_unload -> destroy -> (optional)
|
||||
// dlclose. Shared via shared_ptr so in-flight callers keep it
|
||||
// alive across a swap (RCU-style).
|
||||
struct Instance;
|
||||
} // namespace detail
|
||||
|
||||
// ── PluginHandle ──────────────────────────────────────────────────────
|
||||
// A pinned reference to a plugin instance. While any handle is alive,
|
||||
// the underlying .so and instance are kept loaded even if a reload
|
||||
// swaps in a newer instance — so calls in flight never see a torn or
|
||||
// freed object. The old instance is destroyed once the last handle to
|
||||
// it drops. Work never stops for a reload.
|
||||
class PluginHandle
|
||||
{
|
||||
public:
|
||||
|
||||
PluginHandle() = default;
|
||||
|
||||
[[nodiscard]] IPlugin* operator->() const noexcept;
|
||||
[[nodiscard]] IPlugin& operator*() const noexcept;
|
||||
[[nodiscard]] IPlugin* get() const noexcept;
|
||||
[[nodiscard]] explicit operator bool() const noexcept;
|
||||
|
||||
[[nodiscard]] const PluginInfo& info() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend class PluginManager;
|
||||
explicit PluginHandle(std::shared_ptr<detail::Instance> pin)
|
||||
: m_pin(std::move(pin))
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<detail::Instance> m_pin;
|
||||
};
|
||||
|
||||
// ── PluginManager ─────────────────────────────────────────────────────
|
||||
// Loads plugins by name, hands out pinned handles, and hot-reloads
|
||||
// (manually or by watching the file). Reload is transactional: the new
|
||||
// instance is created and given the old instance's serialized state
|
||||
// before any swap; on any failure the old instance stays installed.
|
||||
struct PluginManagerOptions {
|
||||
std::chrono::milliseconds poll_interval{500};
|
||||
int debounce_polls = 2;
|
||||
// Keep the old .so mapped after a swap (default) — closing it
|
||||
// while a coroutine frame or std::function from that .so is still
|
||||
// executing would crash. Pins already prevent the instance from
|
||||
// being destroyed; this guards the code pages.
|
||||
bool dlclose_on_swap = false;
|
||||
};
|
||||
|
||||
class PluginManager
|
||||
{
|
||||
public:
|
||||
|
||||
using Options = PluginManagerOptions;
|
||||
|
||||
explicit PluginManager(Options options = {});
|
||||
~PluginManager();
|
||||
|
||||
PluginManager(const PluginManager&) = delete;
|
||||
PluginManager& operator=(const PluginManager&) = delete;
|
||||
PluginManager(PluginManager&&) = delete;
|
||||
PluginManager& operator=(PluginManager&&) = delete;
|
||||
|
||||
/// Load a plugin from `path`. Returns the plugin's name (used to
|
||||
/// address it later). The name comes from PluginInfo::name.
|
||||
std::expected<std::string, LoadError>
|
||||
load(const std::filesystem::path& path);
|
||||
|
||||
/// Pin the current instance for `name`. The returned handle
|
||||
/// keeps it alive for as long as it lives. Empty handle if no
|
||||
/// such plugin. One atomic load on the hot path.
|
||||
[[nodiscard]] PluginHandle acquire(std::string_view name) const;
|
||||
|
||||
/// Reload `name` from its file: create new, transfer state from
|
||||
/// old, atomically swap. Transactional — old instance is kept
|
||||
/// on any failure. In-flight handles to the old instance keep
|
||||
/// it alive until they drop.
|
||||
std::expected<void, LoadError> reload(std::string_view name);
|
||||
|
||||
/// Begin watching the plugin's file; reload automatically when
|
||||
/// it changes (debounced). Reload failures are logged and the
|
||||
/// old instance is kept.
|
||||
void watch(std::string_view name);
|
||||
void unwatch(std::string_view name);
|
||||
|
||||
/// Invoked after every successful reload (manual or watched),
|
||||
/// on the caller's / watcher's thread.
|
||||
void on_reloaded(std::function<void(std::string_view name)> cb);
|
||||
|
||||
[[nodiscard]] std::vector<std::string> names() const;
|
||||
|
||||
private:
|
||||
|
||||
struct Entry {
|
||||
std::filesystem::path path;
|
||||
std::atomic<std::shared_ptr<detail::Instance>> current;
|
||||
};
|
||||
|
||||
Entry* find(std::string_view name) const;
|
||||
std::expected<void, LoadError> reload_locked(Entry& entry,
|
||||
std::string_view name);
|
||||
|
||||
Options m_options;
|
||||
mutable std::mutex m_mutex; // guards the registry map + reloads
|
||||
std::unordered_map<std::string, std::unique_ptr<Entry>> m_plugins;
|
||||
std::function<void(std::string_view)> m_on_reloaded;
|
||||
FileSystem::FileWatcher m_watcher;
|
||||
};
|
||||
|
||||
} // namespace Void::Plugin
|
||||
@@ -48,15 +48,38 @@ public:
|
||||
//
|
||||
// Optional (enables hot-reload state transfer):
|
||||
// void void_plugin_transfer_state(void* old, void* new)
|
||||
//
|
||||
// Optional v2 state protocol (preferred over transfer_state when both
|
||||
// sides provide it; survives layout changes between plugin versions):
|
||||
//
|
||||
// uint32_t void_plugin_state_version()
|
||||
// — schema version of the state this build serializes
|
||||
// const char* void_plugin_serialize_state(void* instance)
|
||||
// — heap-allocated JSON; release with void_plugin_free_string
|
||||
// void void_plugin_free_string(const char*)
|
||||
// — frees serialize output (allocator stays inside the .so)
|
||||
// int void_plugin_deserialize_state(void* instance,
|
||||
// const char* json, uint32_t from_version)
|
||||
// — 0 on success; migrations from historical versions live in the
|
||||
// NEW .so (it knows the old formats; Json::MigrationChain helps)
|
||||
|
||||
using create_fn = void* (*)(void);
|
||||
using destroy_fn = void (*)(void*);
|
||||
using info_fn = PluginInfo (*)(void);
|
||||
using transfer_fn = void (*)(void* old_instance, void* new_instance);
|
||||
using create_fn = void* (*)(void);
|
||||
using destroy_fn = void (*)(void*);
|
||||
using info_fn = PluginInfo (*)(void);
|
||||
using transfer_fn = void (*)(void* old_instance, void* new_instance);
|
||||
using state_version_fn = std::uint32_t (*)(void);
|
||||
using serialize_fn = const char* (*)(void* instance);
|
||||
using free_string_fn = void (*)(const char*);
|
||||
using deserialize_fn = int (*)(void* instance, const char* json,
|
||||
std::uint32_t from_version);
|
||||
|
||||
inline constexpr const char* kCreateSymbol = "void_plugin_create";
|
||||
inline constexpr const char* kDestroySymbol = "void_plugin_destroy";
|
||||
inline constexpr const char* kInfoSymbol = "void_plugin_info";
|
||||
inline constexpr const char* kTransferSymbol = "void_plugin_transfer_state";
|
||||
inline constexpr const char* kCreateSymbol = "void_plugin_create";
|
||||
inline constexpr const char* kDestroySymbol = "void_plugin_destroy";
|
||||
inline constexpr const char* kInfoSymbol = "void_plugin_info";
|
||||
inline constexpr const char* kTransferSymbol = "void_plugin_transfer_state";
|
||||
inline constexpr const char* kStateVersionSymbol = "void_plugin_state_version";
|
||||
inline constexpr const char* kSerializeSymbol = "void_plugin_serialize_state";
|
||||
inline constexpr const char* kFreeStringSymbol = "void_plugin_free_string";
|
||||
inline constexpr const char* kDeserializeSymbol = "void_plugin_deserialize_state";
|
||||
|
||||
} // namespace Void::Plugin
|
||||
Reference in new issue
Block a user