Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions apps/desktop/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3010,23 +3010,33 @@ async function applyUpdatesPosixInApp(opts: any) {
emitUpdateProgress({ stage: 'restart', message: 'Installing the updated app and restarting…', percent: 95 })

// Detached swapper: wait for THIS process to exit (so the bundle is free),
// ditto the rebuilt app over the running one, clear quarantine, relaunch.
// then atomically replace the running .app, clear quarantine, and reopen it.
// If the swap fails, fall back to launching the freshly rebuilt bundle
// directly so we do not leave the user with a dead quit.
const swapScript = `#!/bin/bash

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

set -e makes the existing mv "$DST" "$DST.hermes-update-old" || rm -rf "$DST" sequence fatal if both commands fail, before either explicit open "$SRC" fallback. Handle that displacement failure explicitly so the recovery invariant holds.

set -u
set -euo pipefail
APP_PID=${process.pid}
SRC=${shellQuote(rebuiltApp)}
DST=${shellQuote(targetApp)}
for _ in $(seq 1 240); do
kill -0 "$APP_PID" 2>/dev/null || break
sleep 0.5
done

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This trap removes only .hermes-update-old; a failed ditto or destination replacement can leave .hermes-update-new. Clean both staging paths so the stated no-orphaned-staging invariant is true.

cleanup() { rm -rf "$DST.hermes-update-old" 2>/dev/null || true; }
trap cleanup EXIT
if [ "$SRC" != "$DST" ]; then
if /usr/bin/ditto "$SRC" "$DST.hermes-update-new"; then
rm -rf "$DST.hermes-update-old" 2>/dev/null || true
mv "$DST" "$DST.hermes-update-old" 2>/dev/null || rm -rf "$DST"
mv "$DST.hermes-update-new" "$DST"
rm -rf "$DST.hermes-update-old" 2>/dev/null || true
if ! /usr/bin/ditto "$SRC" "$DST.hermes-update-new"; then
echo "[updates] ditto failed" >&2
/usr/bin/open "$SRC"
exit 0
fi
mv "$DST" "$DST.hermes-update-old" 2>/dev/null || rm -rf "$DST"
if ! mv "$DST.hermes-update-new" "$DST"; then
echo "[updates] destination replace failed" >&2
/usr/bin/open "$SRC"
exit 0
fi
rm -rf "$DST.hermes-update-old" 2>/dev/null || true
fi
/usr/bin/xattr -dr com.apple.quarantine "$DST" 2>/dev/null || true
/usr/bin/open "$DST"
Expand Down
Loading