Plugin: copy_on_load option; unique hotswap temp names

- copy_on_load: dlopen a fresh-inode temp copy on first load (reloads
  already did), so deployments that rewrite the plugin file in place —
  e.g. a build writing its output — cannot corrupt the mapped image.
  The registry keeps addressing the original path for watch/reload.
- hotswap_copy now generates a unique temp name per copy. glibc dedupes
  dlopen by pathname, so the previous fixed ".void_hotswap" name made
  every second reload through the same path silently return the
  already-mapped OLD image instead of the new bytes.
- hot_reload_under_concurrent_load now asserts the true downgrade
  semantics (v2 -> v1 is rejected transactionally, v2 stays); it only
  ever passed the old all-succeed expectation via the dedup bug, since
  no flip after the first actually took effect.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-07-11 23:46:21 +03:00
1 parent ee4486e1d2
commit a06f639393
4 files changed
+80 -12

No files matched your search

+25 -4
View File
@@ -8,7 +8,9 @@
#endif
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <string>
#include <system_error>
#include <utility>
@@ -170,12 +172,18 @@ 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.
// 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.
std::expected<std::filesystem::path, LoadError>
hotswap_copy(const std::filesystem::path& path)
{
const auto tmp = path.parent_path() /
(path.filename().string() + ".void_hotswap");
static std::atomic<std::uint64_t> counter{0};
const auto tmp =
path.parent_path() /
(path.filename().string() + ".void_hotswap." +
std::to_string(counter.fetch_add(1, std::memory_order_relaxed)));
std::error_code ec;
std::filesystem::copy_file(
path, tmp, std::filesystem::copy_options::overwrite_existing,
@@ -211,7 +219,20 @@ namespace Void::Plugin
if (!std::filesystem::exists(path, ec)) {
return std::unexpected{LoadError::FileNotFound};
}
auto inst = open_instance(path, m_options.dlclose_on_swap);
std::filesystem::path load_path = path;
if (m_options.copy_on_load) {
auto tmp = hotswap_copy(path);
if (!tmp.has_value()) {
return std::unexpected{tmp.error()};
}
load_path = *tmp;
}
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()) {
Void::Log::warn("Plugin.Manager", "load '{}' failed: {}",
path.string(), to_string(inst.error()));