feat(plugin): add CAbi.h v1, CModuleHost quiesce/reload/pins, and unlocked PluginManager staging

This commit is contained in:
faumaray committed 2026-11-26 16:16:29 +03:00
1 parent 1474a69ab1
commit a24e086e47
16 files changed
+3961 -164

No files matched your search

+174 -158
View File
@@ -187,10 +187,7 @@ namespace Void::Plugin
}
// Copy to a fresh-inode temp path so dlopen does not return the
// already-mapped image when the file is replaced in place. The name
// must be unique per copy: glibc dedupes dlopen by pathname, so
// reusing a temp name would hand back the previously mapped (old)
// image no matter what bytes the file now holds.
// already-mapped image when the file is replaced in place.
std::expected<std::filesystem::path, LoadError>
hotswap_copy(const std::filesystem::path& path)
{
@@ -213,9 +210,10 @@ namespace Void::Plugin
// ── PluginManager ─────────────────────────────────────────────────────
PluginManager::PluginManager(Options options)
: m_options(options), m_watcher(FileSystem::FileWatcherOptions{
.poll_interval = options.poll_interval,
.debounce_polls = options.debounce_polls})
: m_options(std::move(options)),
m_watcher(FileSystem::FileWatcherOptions{
.poll_interval = m_options.poll_interval,
.debounce_polls = m_options.debounce_polls})
{
}
@@ -227,6 +225,13 @@ namespace Void::Plugin
return it == m_plugins.end() ? nullptr : it->second.get();
}
std::shared_ptr<PluginManager::Entry>
PluginManager::find_shared(std::string_view name) const
{
const auto it = m_plugins.find(std::string{name});
return it == m_plugins.end() ? nullptr : it->second;
}
std::expected<std::string, LoadError>
PluginManager::load(const std::filesystem::path& path)
{
@@ -244,8 +249,6 @@ namespace Void::Plugin
}
auto inst = open_instance(load_path, m_options.dlclose_on_swap);
if (m_options.copy_on_load) {
// The mapping keeps the unlinked inode alive; the registry
// keeps addressing the original path for watch/reload.
std::filesystem::remove(load_path, ec);
}
if (!inst.has_value()) {
@@ -278,15 +281,24 @@ namespace Void::Plugin
return std::unexpected{LoadError::ActivationFailed};
}
std::lock_guard lock(m_mutex);
auto entry = std::make_unique<Entry>();
entry->path = path;
entry->current.store(std::move(*inst), std::memory_order_release);
Void::Log::info("Plugin.Manager", "loaded '{}' as '{}' (v{})",
path.string(), name,
entry->current.load(std::memory_order_relaxed)
->info.plugin_version);
m_plugins.insert_or_assign(name, std::move(entry));
std::shared_ptr<Entry> replaced_entry;
{
std::lock_guard lock(m_mutex);
auto entry = std::make_shared<Entry>();
entry->path = path;
entry->current.store(std::move(*inst), std::memory_order_release);
Void::Log::info("Plugin.Manager", "loaded '{}' as '{}' (v{})",
path.string(), name,
entry->current.load(std::memory_order_relaxed)
->info.plugin_version);
auto it = m_plugins.find(name);
if (it != m_plugins.end()) {
replaced_entry = std::move(it->second);
it->second = std::move(entry);
} else {
m_plugins.emplace(name, std::move(entry));
}
}
return name;
}
@@ -323,195 +335,199 @@ namespace Void::Plugin
std::expected<void, LoadError> PluginManager::unload(std::string_view name)
{
std::lock_guard lock(m_mutex);
auto* entry = find(name);
if (entry == nullptr) {
return std::unexpected{LoadError::UnknownPlugin};
std::shared_ptr<Entry> removed;
{
std::lock_guard lock(m_mutex);
auto it = m_plugins.find(std::string{name});
if (it == m_plugins.end()) {
return std::unexpected{LoadError::UnknownPlugin};
}
m_watcher.unwatch(it->second->path);
removed = std::move(it->second);
m_plugins.erase(it);
}
m_watcher.unwatch(entry->path);
// Erasing drops the registry's reference; outstanding handles keep
// the instance (and its mapped .so) alive until they drop.
m_plugins.erase(std::string{name});
Void::Log::info("Plugin.Manager", "unloaded '{}'", name);
return {};
}
PluginHandle PluginManager::acquire(std::string_view name) const
{
std::lock_guard lock(m_mutex);
auto* entry = find(name);
if (entry == nullptr) {
std::shared_ptr<Entry> entry;
{
std::lock_guard lock(m_mutex);
entry = find_shared(name);
}
if (!entry) {
return PluginHandle{};
}
// One atomic load pins the instance; it survives any later swap.
return PluginHandle{entry->current.load(std::memory_order_acquire)};
}
std::expected<void, LoadError> PluginManager::reload(std::string_view name)
{
std::vector<std::function<void(std::string_view)>> callbacks;
std::shared_ptr<Entry> entry;
{
std::lock_guard lock(m_mutex);
auto* entry = find(name);
if (entry == nullptr) {
return std::unexpected{LoadError::UnknownPlugin};
}
auto result = reload_locked(*entry, name, entry->path);
if (!result.has_value()) {
return result;
}
callbacks = reload_callbacks_locked();
entry = find_shared(name);
}
for (const auto& cb : callbacks) {
cb(name);
if (!entry) {
return std::unexpected{LoadError::UnknownPlugin};
}
return {};
return reload_entry_unlocked(entry, name, std::nullopt);
}
std::expected<void, LoadError>
PluginManager::replace(std::string_view name,
const std::filesystem::path& newPath)
{
std::vector<std::function<void(std::string_view)>> callbacks;
bool restartWatcher = false;
std::shared_ptr<Entry> entry;
{
std::lock_guard lock(m_mutex);
auto* entry = find(name);
if (entry == nullptr) {
return std::unexpected{LoadError::UnknownPlugin};
}
std::error_code error;
if (!std::filesystem::is_regular_file(newPath, error)) {
return std::unexpected{LoadError::FileNotFound};
}
auto result = reload_locked(*entry, name, newPath);
if (!result) {
return result;
}
restartWatcher = entry->watched;
if (restartWatcher) {
m_watcher.unwatch(entry->path);
}
entry->path = newPath;
callbacks = reload_callbacks_locked();
entry = find_shared(name);
}
if (!entry) {
return std::unexpected{LoadError::UnknownPlugin};
}
std::error_code error;
if (!std::filesystem::is_regular_file(newPath, error)) {
return std::unexpected{LoadError::FileNotFound};
}
return reload_entry_unlocked(entry, name, newPath);
}
std::expected<void, LoadError> PluginManager::reload_entry_unlocked(
const std::shared_ptr<Entry>& entry, std::string_view name,
const std::optional<std::filesystem::path>& newPath)
{
std::vector<std::function<void(std::string_view)>> callbacks;
bool restartWatcher = false;
std::shared_ptr<detail::Instance> retired_old;
{
// Serialize reloads per plugin entry WITHOUT holding m_mutex, so
// validators, config providers, and concurrent acquire() callers can
// reenter PluginManager without deadlocking.
std::lock_guard reload_lk{entry->reload_mutex};
std::filesystem::path sourcePath;
{
std::lock_guard lock(m_mutex);
if (find(name) != entry.get()) {
return std::unexpected{LoadError::UnknownPlugin};
}
sourcePath = newPath.has_value() ? *newPath : entry->path;
}
auto tmp = hotswap_copy(sourcePath);
if (!tmp.has_value()) {
return std::unexpected{tmp.error()};
}
auto next = open_instance(*tmp, m_options.dlclose_on_swap);
std::error_code ec;
std::filesystem::remove(*tmp, ec);
if (!next.has_value()) {
Void::Log::warn("Plugin.Manager",
"reload '{}' failed: {}; keeping old", name,
to_string(next.error()));
return std::unexpected{next.error()};
}
if ((*next)->plugin->name() != name) {
Void::Log::warn("Plugin.Manager",
"replace '{}' rejected: candidate reports '{}'",
name, (*next)->plugin->name());
return std::unexpected{LoadError::NameMismatch};
}
if (m_options.candidate_validator &&
!m_options.candidate_validator(PluginHandle{*next})) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: binary validation failed",
name);
return std::unexpected{LoadError::ActivationFailed};
}
if (m_options.config_provider && (*next)->configure != nullptr) {
const std::string config = m_options.config_provider(name);
(*next)->configure((*next)->plugin, config.c_str());
}
auto old = entry->current.load(std::memory_order_acquire);
if (old) {
if (!transfer_state(*old, **next)) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: state rejected", name);
return std::unexpected{LoadError::StateRejected};
}
}
try {
(*next)->plugin->on_load();
} catch (...) {
return std::unexpected{LoadError::ActivationFailed};
}
if ((*next)->ready != nullptr &&
(*next)->ready((*next)->plugin) != 0) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: activation failed", name);
return std::unexpected{LoadError::ActivationFailed};
}
if (m_options.instance_validator &&
!m_options.instance_validator(*(*next)->plugin, (*next)->info)) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: host validation failed",
name);
return std::unexpected{LoadError::ActivationFailed};
}
{
std::lock_guard lock(m_mutex);
if (find(name) != entry.get()) {
return std::unexpected{LoadError::UnknownPlugin};
}
retired_old = entry->current.exchange(
std::move(*next), std::memory_order_acq_rel);
if (newPath.has_value()) {
restartWatcher = entry->watched;
if (restartWatcher) {
m_watcher.unwatch(entry->path);
}
entry->path = *newPath;
}
callbacks = reload_callbacks_locked();
}
}
// Drop old instance and invoke observers strictly outside all locks.
retired_old.reset();
if (restartWatcher) {
watch(name);
}
for (const auto& callback : callbacks) {
callback(name);
}
return {};
}
std::expected<void, LoadError>
PluginManager::reload_locked(Entry& entry, std::string_view name,
const std::filesystem::path& sourcePath)
{
// Copy to a fresh inode so dlopen loads the new bytes instead of
// returning the still-open handle for this path. The caller must
// have installed the new file via atomic rename (a fresh inode of
// its own) — overwriting the mapped file in place corrupts the
// running instance's mapping (SIGBUS).
auto tmp = hotswap_copy(sourcePath);
if (!tmp.has_value()) {
return std::unexpected{tmp.error()};
}
auto next = open_instance(*tmp, m_options.dlclose_on_swap);
std::error_code ec;
std::filesystem::remove(*tmp, ec);
if (!next.has_value()) {
Void::Log::warn("Plugin.Manager",
"reload '{}' failed: {}; keeping old", name,
to_string(next.error()));
return std::unexpected{next.error()};
}
if ((*next)->plugin->name() != name) {
Void::Log::warn("Plugin.Manager",
"replace '{}' rejected: candidate reports '{}'",
name, (*next)->plugin->name());
return std::unexpected{LoadError::NameMismatch};
}
if (m_options.candidate_validator &&
!m_options.candidate_validator(PluginHandle{*next})) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: binary validation failed",
name);
return std::unexpected{LoadError::ActivationFailed};
}
if (m_options.config_provider && (*next)->configure != nullptr) {
const std::string config = m_options.config_provider(name);
(*next)->configure((*next)->plugin, config.c_str());
}
// Transfer state with both instances alive (transactional).
auto old = entry.current.load(std::memory_order_acquire);
if (old) {
if (!transfer_state(*old, **next)) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: state rejected", name);
return std::unexpected{LoadError::StateRejected};
}
}
try {
(*next)->plugin->on_load();
} catch (...) {
return std::unexpected{LoadError::ActivationFailed};
}
if ((*next)->ready != nullptr && (*next)->ready((*next)->plugin) != 0) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: activation failed", name);
return std::unexpected{LoadError::ActivationFailed};
}
if (m_options.instance_validator &&
!m_options.instance_validator(*(*next)->plugin, (*next)->info)) {
Void::Log::warn("Plugin.Manager",
"reload '{}' aborted: host validation failed",
name);
return std::unexpected{LoadError::ActivationFailed};
}
// Atomic swap. The old instance keeps living while any acquired
// handle still pins it; it is destroyed when the last one drops.
entry.current.store(std::move(*next), std::memory_order_release);
Void::Log::info("Plugin.Manager", "reloaded '{}'", name);
for (const auto& cb : callbacks) {
cb(name);
}
return {};
}
void PluginManager::watch(std::string_view name)
{
std::shared_ptr<Entry> entry;
std::filesystem::path path;
{
std::lock_guard lock(m_mutex);
auto* entry = find(name);
if (entry == nullptr) {
entry = find_shared(name);
if (!entry) {
return;
}
path = entry->path;
entry->watched = true;
}
const std::string name_str{name};
m_watcher.watch(path, [this, name_str](
m_watcher.watch(path, [this, entry, name_str](
const FileSystem::FileWatcher::Event& e) {
if (!e.exists) {
return;
}
std::vector<std::function<void(std::string_view)>> callbacks;
{
std::lock_guard lock(m_mutex);
auto* entry = find(name_str);
if (entry == nullptr) {
return;
}
if (!reload_locked(*entry, name_str, entry->path).has_value()) {
return;
}
callbacks = reload_callbacks_locked();
}
for (const auto& cb : callbacks) {
cb(name_str);
}
(void)reload_entry_unlocked(entry, name_str, std::nullopt);
});
}