Skip to content
38 changes: 35 additions & 3 deletions migrations/1786643346.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,41 @@ 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 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

# 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) || fds=""
Comment thread
Copilot marked this conversation as resolved.
Resolved
[[ $fds == *"$1/"* ]] && return 0

# A process that answers for neither counts as open: better a repair
# deferred than one made under a browser that reverts it on exit.
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
Expand Down
79 changes: 75 additions & 4 deletions test/shell.d/copy-url-shortcut-migration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ run_migration() {

# A running Chromium-family browser marks its profile root with a SingletonLock
# symlink to <hostname>-<pid>, a target that never exists on disk. That lock —
# not the mere presence of a browser process — is what the migration waits on.
# not the mere presence of a browser process — is what the migration waits on,
# and it holds only while the pid it names is still a live browser. A copy of
# sleep under a browser's name is one, as far as /proc/<pid>/exe is concerned.
cp "$(command -v sleep)" "$stub_bin/chromium"
"$stub_bin/chromium" 600 &
browser_pid=$!

# Every stand-in has to go on the way out, including a failing exit: one left
# running holds this test's output pipe open long after the run.
holder_pid=""
cleanup() {
kill $browser_pid ${holder_pid:-} 2>/dev/null
rm -rf "$test_dir"
}
trap cleanup EXIT

LIVE_LOCK="$(uname -n)-$browser_pid"
export LIVE_LOCK

open_browser() {
mkdir -p "$profile_root"
ln -sfn "test-host-1234" "$profile_root/SingletonLock"
ln -sfn "$LIVE_LOCK" "$profile_root/SingletonLock"
}
close_browser() {
rm -f "$profile_root/SingletonLock"
Expand Down Expand Up @@ -80,14 +98,67 @@ pass "migration keeps the browser prompt visible"
# prompt, which the still-declining gum stub would otherwise fail.
close_browser
mkdir -p "$home/.config/google-chrome"
ln -sfn "test-host-1234" "$home/.config/google-chrome/SingletonLock"
ln -sfn "$LIVE_LOCK" "$home/.config/google-chrome/SingletonLock"
write_stale_preferences
run_migration || fail "migration repairs while a different profile root is open"
jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null ||
fail "migration repairs the shortcut while a different profile root is open"
pass "migration ignores a browser on a different profile root"
rm -f "$home/.config/google-chrome/SingletonLock" "$preferences.omarchy-copy-url-repair.bak"

# A crash or a reboot leaves the lock behind naming a pid that is gone. Waiting
# on that profile can never end — no keypress makes a dead browser exit — so a
# stale lock has to read as closed.
write_stale_preferences
(exit) &
stale_pid=$!
wait "$stale_pid"
ln -sfn "$(uname -n)-$stale_pid" "$profile_root/SingletonLock"
run_migration || fail "migration repairs through a stale singleton lock"
jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null ||
fail "migration repairs the shortcut through a stale singleton lock"
pass "migration repairs through a stale singleton lock"
close_browser
rm -f "$preferences.omarchy-copy-url-repair.bak"

# The kernel hands a dead browser's pid on to whatever starts next, and this
# test's own shell stands in for that unrelated process: the number matches,
# but nothing about it is a browser.
write_stale_preferences
ln -sfn "$(uname -n)-$$" "$profile_root/SingletonLock"
run_migration || fail "migration repairs through a lock whose pid was reused"
jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null ||
fail "migration repairs the shortcut through a lock whose pid was reused"
pass "migration repairs through a lock whose pid was reused"
close_browser
rm -f "$preferences.omarchy-copy-url-repair.bak"

# Browsers this migration has never heard of still hold their profile open, so
# a lock whose pid keeps a file open in there counts however it is named.
write_stale_preferences
bash -c "exec 9<\"$preferences\"; sleep 600" >/dev/null 2>&1 &
holder_pid=$!
sleep 0.2
ln -sfn "$(uname -n)-$holder_pid" "$profile_root/SingletonLock"
run_migration && fail "migration defers to a lock whose pid holds the profile open"
[[ $(jq -r '.extensions.commands["linux:Alt+Shift+L"].extension' "$preferences") == "$ghost_id" ]] ||
fail "migration leaves preferences alone while the lock's pid holds the profile open"
pass "migration defers to a lock whose pid holds the profile open"
kill "$holder_pid" 2>/dev/null
holder_pid=""
close_browser

# A pid that answers nothing about itself — pid 1 is alive and root-owned, so
# neither its files nor its binary are readable here — is no proof of a dead
# browser, and a deferred repair beats one made under a live one.
write_stale_preferences
ln -sfn "$(uname -n)-1" "$profile_root/SingletonLock"
run_migration && fail "migration defers to a lock whose pid it cannot read"
[[ $(jq -r '.extensions.commands["linux:Alt+Shift+L"].extension' "$preferences") == "$ghost_id" ]] ||
fail "migration leaves preferences alone while the lock's pid is unreadable"
pass "migration defers to a lock whose pid it cannot read"
close_browser

# Closing the affected profile and confirming the prompt lets the repair
# proceed.
write_stale_preferences
Expand Down Expand Up @@ -167,7 +238,7 @@ cat >"$stub_bin/python3" <<'STUB'
# the check calls report a surviving ghost through their exit status.
"${REAL_PYTHON}" "$@"
status=$?
[[ ${5:-} == "repair" ]] && ln -sfn "test-host-1234" "$HOME/.config/chromium/SingletonLock"
[[ ${5:-} == "repair" ]] && ln -sfn "${LIVE_LOCK:?}" "$HOME/.config/chromium/SingletonLock"
exit $status
STUB
chmod +x "$stub_bin/python3"
Expand Down