Skip to content

Get past a stale root-owned backup in the macOS self-update swap - #5827

Merged
Gabriel Dufresne (GabrielDuf) merged 2 commits into
mainfrom
fix/macos-updater-stale-old-backup
Nov 24, 2026
Merged

Gabriel Dufresne (GabrielDuf) merged 2 commits into
mainfrom
fix/macos-updater-stale-old-backup

Conversation

@GabrielDuf

@GabrielDuf Gabriel Dufresne (GabrielDuf) commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Bug report

Users on 2026.3.4, which includes #5578, still see the macOS self-update close UniGetUI and reopen it on the same version. One affected user's session.log shows 2026.3.5 downloaded, verified and extracted, then Replacing /Applications/UniGetUI.app with the freshly-extracted bundle and relaunching., then 2026.3.4 starting again three seconds later, with nothing logged in between. On disk, /Applications/UniGetUI.app was still 2026.3.4, and /Applications/UniGetUI.app.old, a 2026.3.1 copy, was owned by root:wheel.

Root cause

  1. Jamf installed 2026.3.1 from a .pkg, so /Applications/UniGetUI.app was owned by root.
  2. On Nov 12, that 2026.3.1 updater, which predates Stop the macOS updater swapping a translocated copy and failing silently #5578's ownership check, updated to 2026.3.4 as the logged-in admin user. Renaming the root-owned app to UniGetUI.app.old works, since an admin can write /Applications. Moving the new, user-owned app in works too. The final rm -rf "$target.old" could not delete root-owned contents, and its failure was ignored.
  3. From then on every swap fails. The app is user-owned, so Stop the macOS updater swapping a translocated copy and failing silently #5578's check sends it to the regular swap. rm -rf "$target.old" fails silently. mv "$target" "$target.old" then finds .old is still a folder, tries to move the app into it, and gets Permission denied. The script writes === Swap failed: and relaunches the old app.
  4. The user never learns why. The update loop starts in InitializeSharedServicesAsync, before the main window exists, and on a connected Mac its first ResetUpdateLog overwrites last-update-attempt.log. CheckForOrphanedUpdateAttempt runs later from MainWindowViewModel and finds a fresh log with no marker, so the "could not replace" banner from Stop the macOS updater swapping a translocated copy and failing silently #5578 never shows. The swap script also discarded mv's error.

Both swap steps were reproduced in a scratch folder, with a read-only folder standing in for the root-owned one: rm -rf exits 1 with .old still there, then mv: rename UniGetUI.app to UniGetUI.app.old/UniGetUI.app: Permission denied.

Fix

  • Stale backup: if the swap script cannot delete $target.old, it logs that and sets the old version aside under a unique $target.old.XXXXXX name. That name is deleted after a successful swap. The update then goes through with no prompt, and the stale root-owned folder is left as it was. Applied to the Linux swap script too, which had the same shape.
  • Why it failed: the macOS swap script now captures mv's error and writes an Update swap failed while …: <reason> line, exactly as the Linux script already did; ${2##*: } keeps only the reason, so no path reaches the log. Like Linux, if the rollback itself fails, it relaunches the old version from wherever it was left instead of the missing target.
  • Wiped log: new PreviousUpdateAttempt (UniGetUI.Core.Tools) reads the previous run's log once, the first time either ResetUpdateLog or CheckForOrphanedUpdateAttempt asks, and keeps a copy in previous-update-attempt.log. CheckForOrphanedUpdateAttempt works from that snapshot, so its banners show whichever runs first, and their View log opens the copy. The "finished" marker is only appended while the previous attempt is still the file on disk.

Regression tests

  • MacSwapScriptTests (new, macOS): the swap succeeds and opens the new version; it gets past an undeletable .old without a failure marker and leaves no unique backup behind; it logs Update swap failed while moving the installed version aside: Permission denied with no path. open is pointed at a stub in the test's copy of the script, so nothing is launched.
  • LinuxSwapScriptTests.SwapScript_SetsThePreviousVersionAsideWhenALeftoverBackupCannotBeDeleted (new).
  • PreviousUpdateAttemptTests: the snapshot survives the log being overwritten and is copied; no previous log means nothing is reported, even if one appears later.
  • Against main's scripts, the three new swap tests fail; with this change they pass. Filtered run of the updater test classes: 67 passed, 2 skipped (Linux-only), 0 failed.

Verification

  • dotnet build of UniGetUI.Avalonia (arm64) succeeds with no warnings in the changed files. main now restores Devolutions.Pinget.Cli.Rust 0.14.0 from a feed that needs credentials, so this compile used the local package cache with that pin rolled back to 0.13.0 locally; nothing of that is committed.
  • dotnet format whitespace and dotnet format style with --verify-no-changes on the changed files: exit 0.
  • Not run locally: the NativeAOT publish (left to CI; no interop or reflection changed), the Windows suite, and a real self-update.

Not covered

  • Users already stuck cannot update through their current updater, since it is the one that fails. Deleting /Applications/UniGetUI.app.old as root once unblocks them (for Jamf-managed Macs, a one-line policy).
  • The stale root-owned .old is left in place, since only root can delete it. The admin swap path (ElevatedSwapScript) still deletes it, as root, when it runs.

A Jamf .pkg install leaves UniGetUI.app owned by root. Updating it with an
updater older than 2026.3.3, run by an admin user, renamed it to
UniGetUI.app.old, moved the new user-owned version in, and could not delete
the root-owned .old. From then on every swap failed: rm -rf of the .old
failed silently, and mv then tried to move the app into that folder and got
Permission denied, so the app relaunched on the same version.

- Both the macOS and Linux swap scripts now set the old version aside under
  a unique name when the .old cannot be deleted, and the macOS one writes
  mv's error into the update log, as the Linux one already did. It also
  relaunches the old version from wherever it was left if the rollback fails.
- The update loop starts before the main window checks the previous
  attempt, and its first reset wiped that attempt's log, so the "could not
  replace" banner never showed. PreviousUpdateAttempt captures that log once,
  before anything overwrites it, and keeps a copy for View log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- macOS: when the rollback fails, the old version is left under a .old
  name, which LaunchServices refuses to open, so open -na relaunched
  nothing. The script now starts that backup's executable directly.
- Both scripts: if mktemp or rmdir fails while picking a backup name, the
  swap no longer moves the app to an empty or existing path. It keeps the
  installed version and logs why.
- PreviousUpdateAttempt now returns its snapshot even when the copy cannot
  be written, does not remember a failed read, and says whether the copy
  was written, so View log never opens an older copy.
- The stale-backup tests rely on a read-only folder, so they are skipped
  when running as root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The macOS update and rollback paths have only stubbed sandbox coverage, leaving packaged-app recovery around a root-owned .old bundle, resource loading, and LaunchServices relaunch unverified.

No open issues 🎉

What changed in this PR

This PR strengthens UniGetUI’s self-update recovery, extending #5578’s macOS fixes to handle undeletable stale backups and preserve failure details across startup.

Changes:

  • Use unique backup names when stale .old folders block macOS or Linux update swaps.
  • Improve macOS swap-error logging and relaunch the backup executable if rollback fails.
  • Preserve the previous update log for startup warnings and View log, with regression tests for snapshot and swap behavior.
File Description
src/​UniGetUI.Core.Tools/​PreviousUpdateAttempt.cs Captures and copies the previous update log before replacement.
src/​UniGetUI.Core.Tools/​MacUpdateTarget.cs Handles stale backups, records move errors, and improves rollback relaunch.
src/​UniGetUI.Core.Tools/​LinuxUpdateTarget.cs Adds unique-backup fallback for undeletable stale folders.
src/​UniGetUI.Core.Tools.Tests/​PreviousUpdateAttemptTests.cs Tests snapshot retention, absent logs, and copy-write failures.
src/​UniGetUI.Core.Tools.Tests/​PlatformFacts.cs Adds non-root guards for permission-dependent tests.
src/​UniGetUI.Core.Tools.Tests/​MacUpdateTargetTests.cs Tests successful swaps, stale backups, failures, and rollback relaunch.
src/​UniGetUI.Core.Tools.Tests/​LinuxUpdateTargetTests.cs Tests stale-backup recovery and restores permissions during cleanup.
src/​UniGetUI.Avalonia/​Infrastructure/​AvaloniaAutoUpdater.cs Uses the saved snapshot for startup warnings and previous-log viewing.

Review effort: Balanced

@randy-but-a-ro randy-but-a-ro Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Pull request was approved automatically: the AI review is complete and all its review threads are resolved. 🎉

Integration Details
{
	"deliveryId": "00e7f0c8-e828-11f1-913a-a54ca36dae5b",
	"headSha": "600f41bacecc9e8f5d822423907581e836c36183",
	"reviewer": "copilot-pull-request-reviewer[bot]"
}

@GabrielDuf
Gabriel Dufresne (GabrielDuf) merged commit a0af934 into main Nov 24, 2026
11 checks passed
@GabrielDuf
Gabriel Dufresne (GabrielDuf) deleted the fix/macos-updater-stale-old-backup branch November 24, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants