-
Notifications
You must be signed in to change notification settings - Fork 358
fix(backends): stage and verify backend install before removing the working binary #2315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ianbmacdonald
wants to merge
1
commit into
lemonade-sdk:main
Choose a base branch
from
ianbmacdonald:fix/install-atomicity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+409
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <system_error> | ||
|
|
||
| // Header-only, dependency-light staging/atomic-swap helpers for backend | ||
| // installation. Kept free of the heavier backend_utils.cpp dependencies so the | ||
| // crash-safety invariant can be unit-tested in isolation (test/cpp/test_install_atomicity.cpp). | ||
| namespace lemon::backends { | ||
|
|
||
| /** | ||
| * Recursively search `dir` for a regular file named `binary_name` (or | ||
| * `binary_name + ".exe"` on Windows). Returns the full path, or "" if not | ||
| * found or `dir` does not exist. | ||
| */ | ||
| inline std::string find_executable_in_dir(const std::string& dir, | ||
| const std::string& binary_name) { | ||
| namespace fs = std::filesystem; | ||
| if (!fs::exists(dir)) { | ||
| return ""; | ||
| } | ||
| #ifdef _WIN32 | ||
| const std::string binary_name_exe = binary_name + ".exe"; | ||
| #endif | ||
| for (const fs::directory_entry& dir_entry : fs::recursive_directory_iterator(dir)) { | ||
| if (dir_entry.is_regular_file()) { | ||
| const auto& fname = dir_entry.path().filename(); | ||
| if (fname == binary_name | ||
| #ifdef _WIN32 | ||
| || fname == binary_name_exe | ||
| #endif | ||
| ) { | ||
| return dir_entry.path().string(); | ||
| } | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| /** | ||
| * Atomically promote a fully-prepared staging directory to `install_dir`. | ||
| * | ||
| * Verifies that `staging_dir` contains `binary_name` before touching the | ||
| * currently-installed copy. The caller MUST create `staging_dir` as a | ||
| * sibling of `install_dir` so the renames stay on one filesystem. | ||
| * | ||
| * The promotion keeps a recoverable copy of the previous install at all | ||
| * times so a failed swap can never lose both installs: | ||
| * 1. move the existing install aside to `install_dir + ".old"`; | ||
| * 2. rename `staging_dir` into `install_dir`; | ||
| * 3. only then delete the `.old` backup. | ||
| * If step 2 fails the `.old` backup is rolled back into place, restoring the | ||
| * previously-working install. | ||
| * | ||
| * Outcomes: | ||
| * - returns the path to the installed executable on success; | ||
| * - returns "" when `staging_dir` does not contain `binary_name` (nothing | ||
| * was promoted; `staging_dir` is removed, `install_dir` is untouched); | ||
| * - throws std::runtime_error when the filesystem swap itself fails — the | ||
| * previous install is left in place (or rolled back), and `staging_dir` | ||
| * is left for the caller to clean up. This is distinct from the "" case | ||
| * so the caller can report an accurate error. | ||
| */ | ||
| inline std::string commit_staged_install(const std::string& staging_dir, | ||
| const std::string& install_dir, | ||
| const std::string& binary_name) { | ||
| namespace fs = std::filesystem; | ||
|
|
||
| // Verify the freshly-staged tree actually contains the backend | ||
| // executable before we touch the currently-installed copy. | ||
| std::string staged_exe = find_executable_in_dir(staging_dir, binary_name); | ||
| if (staged_exe.empty()) { | ||
| std::error_code ec; | ||
| fs::remove_all(staging_dir, ec); // drop the bad staging tree; keep install_dir | ||
| return ""; | ||
| } | ||
|
|
||
| const std::string backup_dir = install_dir + ".old"; | ||
| std::error_code ec; | ||
| fs::remove_all(backup_dir, ec); // clear any stale backup from a prior aborted swap | ||
|
|
||
| // Move the existing install aside (if any) so it survives a failed | ||
| // promotion. We never remove it outright — it is renamed to .old and | ||
| // only deleted once the new tree is verified in place. | ||
| const bool had_install = fs::exists(install_dir); | ||
| if (had_install) { | ||
| fs::rename(install_dir, backup_dir, ec); | ||
| if (ec) { | ||
| // Could not move the existing install aside; leave it untouched. | ||
| throw std::runtime_error( | ||
| "backend install swap failed: could not back up existing install at " | ||
| + install_dir + " (" + ec.message() + ")"); | ||
| } | ||
| } | ||
|
|
||
| // Promote the staged tree into place. | ||
| fs::rename(staging_dir, install_dir, ec); | ||
| if (ec) { | ||
| // Promotion failed. Roll the backup back so the previously-working | ||
| // install is restored rather than lost. | ||
| if (had_install) { | ||
| std::error_code rollback_ec; | ||
| fs::rename(backup_dir, install_dir, rollback_ec); | ||
| } | ||
| throw std::runtime_error( | ||
| "backend install swap failed: could not promote staged install to " | ||
| + install_dir + " (" + ec.message() + ")"); | ||
| } | ||
|
|
||
| // New install is in place; drop the backup. | ||
| fs::remove_all(backup_dir, ec); | ||
|
|
||
| return find_executable_in_dir(install_dir, binary_name); | ||
| } | ||
|
|
||
| } // namespace lemon::backends |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.