From ed51bcc3b6a74b76647452f370ca87f69c86edc5 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 1 Oct 2025 13:25:28 +0200 Subject: [PATCH] Fix leftover active_runtime.json after crash If the server hard crashes (kill -9, system reboot etc), the active_runtime.json remains there and will be preserved thanks to the backup mechanism. If this happens, detect that it's already a symlink and prevent doing the backup instead. --- server/active_runtime.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/server/active_runtime.cpp b/server/active_runtime.cpp index 5fbf7e5c..7a1470cb 100644 --- a/server/active_runtime.cpp +++ b/server/active_runtime.cpp @@ -93,17 +93,32 @@ static void move_file(const std::filesystem::path & from, const std::filesystem: std::filesystem::rename(from, to); } +static const std::filesystem::path & backup_and_symlink( + const std::filesystem::path & location, + const std::filesystem::path & dst) +{ + std::filesystem::create_directories(location.parent_path()); + std::error_code ec; + if (std::filesystem::equivalent(location, dst, ec)) + return location; + // The file may be a dead symlink + if (ec) + std::filesystem::remove(location); + + move_file(location, backup_name(location)); + + std::filesystem::create_symlink(dst, location); + return location; +} + active_runtime::active_runtime() : pid(getpid()) { try { - std::filesystem::path active_runtime_json = xdg_config_home() / "openxr/1/active_runtime.json"; - std::filesystem::create_directories(active_runtime_json.parent_path()); - move_file(active_runtime_json, backup_name(active_runtime_json)); - - std::filesystem::create_symlink(manifest_path(), active_runtime_json); - this->active_runtime_json = active_runtime_json; + this->active_runtime_json = backup_and_symlink( + xdg_config_home() / "openxr/1/active_runtime.json", + manifest_path()); } catch (std::exception & e) {