Skip to content
78 changes: 72 additions & 6 deletions migrations/1786643346.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,73 @@ unverified_repairs_exist() {
# Whether a profile is open is mechanical: a running Chromium-family browser
# holds a SingletonLock (and socket) inside its user-data-dir.
profile_open() {
# -L catches a SingletonLock left as a dangling symlink; -e covers a plain
# file, and -S the socket — any of them means a browser is attached.
[[ -L $1/SingletonLock || -e $1/SingletonLock || -S $1/SingletonSocket ]]
local lock=$1/SingletonLock target host pid state fds exe

# The lock always dangles: it points at <hostname>-<pid>, never at a file. So
# only that pid says whether a browser is still attached, and a crash or a
# reboot routinely leaves the link behind with a pid that is long gone.
if [[ -L $lock ]]; then
target=$(readlink "$lock")
host=${target%-*}
pid=${target##*-}

# A lock another machine wrote (or one that doesn't parse) says nothing
# from here, so leave it counting as open rather than repair under a
# browser we cannot see.
[[ $host == "$(uname -n)" && $pid =~ ^[0-9]+$ ]] || return 0

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.

P2 New branches lack tests

Every simulated lock names test-host-1234, so the existing tests return at this hostname check without reaching the new PID checks. Successful confirmations also close the browser immediately, leaving the three-prompt limit untested.

Add cases for a dead PID, a live owner, a reused non-browser PID, and the prompt limit resetting when the waiting profile changes. Otherwise, both fixes can regress while the tests still pass.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


# Whatever wrote a lock naming a pid that is gone is gone with it.
[[ -d /proc/$pid ]] || return 1

# A zombie is just as gone; it only still has an entry because nobody has
# reaped it. Left to the checks below it would read as a process too
# secretive to see into, since a zombie has no files and no binary.
state=$(</proc/$pid/stat) || state=""
[[ ${state##*") "} == Z* ]] && return 1

# The kernel hands that number on to whatever starts next, so the lock
# holds only while the pid still keeps a file open inside this profile —
# a browser holds dozens, from leveldb locks to the login database.
fds=$(readlink -- /proc/$pid/fd/* 2>/dev/null || true)
[[ $fds == *"$1/"* ]] && return 0

# A browser whose files this cannot see — one reaching the profile through
# a symlink, or holding it open only in a child — still counts as open by
# its name alone. Waiting on the wrong browser costs a deferred repair the
# next run makes; repairing under a live one loses the fix for good.
exe=$(readlink "/proc/$pid/exe" 2>/dev/null) || return 0
case ${exe##*/} in
*chrom* | brave* | msedge* | microsoft-edge* | vivaldi* | opera* | helium*) return 0 ;;
esac

return 1
fi

[[ -e $lock || -S $1/SingletonSocket ]]
}

# Gate on a pending — or to-be-verified — profile actually being open, not on
# browsers in general. Waiting on every browser process deadlocks the update on
# machines whose main browser is effectively never closed: the pending ghosts
# commonly sit in a stale profile nobody has open, yet the update blocks on the
# always-open one.
# Leaves the profile it stopped on in open_profile, so a wait that goes
# nowhere can say which one it is waiting for.
affected_profile_open() {
local preferences backup profile_root
local preferences backup profile_root profile

open_profile=""

for preferences in "${pending[@]}"; do
profile_open "$(dirname "$(dirname "$preferences")")" && return 0
profile=$(dirname "$(dirname "$preferences")")
profile_open "$profile" && open_profile=$profile && return 0
done

for profile_root in "${profile_roots[@]}"; do
for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do
[[ -f $backup ]] || continue
profile_open "$(dirname "$(dirname "$backup")")" && return 0
profile=$(dirname "$(dirname "$backup")")
profile_open "$profile" && open_profile=$profile && return 0
done
done

Expand All @@ -166,7 +212,27 @@ fi
# ask in, gum fails; then — as on decline — fail so the migration stays
# pending, and the login notifier keeps prompting until a rerun goes through
# with the affected profiles closed.
#
# Three rounds is someone closing windows. Past that the profile reads as open
# whatever the user does, and this loop runs inside omarchy-update: waiting on
# it forever hangs the update itself, where deferring only postpones a repair
# the next run can still make. Closing one profile and moving on to the next is
# progress, so the count follows the profile being waited on rather than the
# prompt.
attempts=0
waiting_on=""
while affected_profile_open; do
if [[ $open_profile != "$waiting_on" ]]; then
waiting_on=$open_profile
attempts=0
fi

if (( ++attempts > 3 )); then
echo "$open_profile still looks like it has a browser attached." >&2
echo "If none is running, delete the leftover $open_profile/Singleton* links, then run: omarchy-migrate" >&2
exit 1
fi

if ! gum confirm "Close the browser windows to repair the Copy URL shortcut, then continue"; then
echo "A running browser would undo the Copy URL shortcut repair." >&2
echo "Close the browser windows, then run: omarchy-migrate" >&2
Expand Down
Loading