Repository navigation
fix(plugins): suspend local watch during plugin update (#12859) #13006
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ export GIT_SSH_COMMAND="${GIT_SSH_COMMAND:-ssh -oBatchMode=yes}" | |
| PLUGINS_DIR="$HOME/.config/omarchy/plugins" | ||
| ASSUME_YES=0 | ||
| UPDATED_ANY=0 | ||
| WATCH_SUSPENDED=0 | ||
| SHELL_AVAILABLE=0 | ||
|
|
||
| fail() { | ||
| echo "omarchy-plugin-update: $*" >&2 | ||
|
|
@@ -36,7 +38,7 @@ valid_plugin_id() { | |
| [[ $1 =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ && $1 != *..* ]] | ||
| } | ||
|
|
||
| update_one() { | ||
| prepare_one() { | ||
| local id="$1" | ||
| local dir="$PLUGINS_DIR/$id" | ||
|
|
||
|
|
@@ -47,7 +49,7 @@ update_one() { | |
|
|
||
| if [[ $(git -C "$dir" rev-parse HEAD) == $(git -C "$dir" rev-parse FETCH_HEAD) ]]; then | ||
| echo "$id is up to date." | ||
| return 0 | ||
| return 2 | ||
| fi | ||
|
|
||
| if (( ! ASSUME_YES )); then | ||
|
|
@@ -60,10 +62,17 @@ update_one() { | |
| echo | ||
| confirm "Update $id?" || { | ||
| echo "Skipped $id." | ||
| return 0 | ||
| return 2 | ||
| } | ||
| fi | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| apply_one() { | ||
| local id="$1" | ||
| local dir="$PLUGINS_DIR/$id" | ||
|
|
||
| if ! git -C "$dir" merge --ff-only FETCH_HEAD >/dev/null 2>&1; then | ||
| echo "omarchy-plugin-update: cannot fast-forward '$id'; you have local changes in $dir" >&2 | ||
| return 1 | ||
|
|
@@ -79,6 +88,42 @@ update_one() { | |
| UPDATED_ANY=1 | ||
| } | ||
|
|
||
| resume_local_plugin_watch() { | ||
| (( WATCH_SUSPENDED )) || return 0 | ||
| WATCH_SUSPENDED=0 | ||
|
|
||
| if (( SHELL_AVAILABLE )); then | ||
| if ! omarchy-shell shell setLocalPluginWatch true >/dev/null; then | ||
| echo "omarchy-plugin-update: warning: could not resume local plugin watch; restart omarchy-shell" >&2 | ||
| return 1 | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| cleanup() { | ||
| local status=$? | ||
| trap - EXIT | ||
| resume_local_plugin_watch || true | ||
| exit "$status" | ||
| } | ||
|
|
||
| suspend_local_plugin_watch() { | ||
| SHELL_AVAILABLE=0 | ||
| if omarchy-shell shell ping >/dev/null 2>&1; then | ||
| SHELL_AVAILABLE=1 | ||
| fi | ||
|
|
||
| # No running shell means there is no live watcher to race. If a shell is | ||
| # reachable, however, suspension is part of the mutation invariant: do not | ||
| # write the plugin tree if that request fails (including an older shell that | ||
| # does not yet expose setLocalPluginWatch). | ||
| if (( SHELL_AVAILABLE )); then | ||
| omarchy-shell shell setLocalPluginWatch false >/dev/null || | ||
| fail "running shell could not suspend the local plugin watcher; restart the shell and retry" | ||
| WATCH_SUSPENDED=1 | ||
| fi | ||
| } | ||
|
|
||
| id="" | ||
|
|
||
| while (( $# > 0 )); do | ||
|
|
@@ -122,12 +167,41 @@ else | |
| fi | ||
| fi | ||
|
|
||
| # Serialize complete update sessions so two callers cannot independently | ||
| # suspend/resume the one global watcher or merge the same checkout concurrently. | ||
| runtime_dir="${XDG_RUNTIME_DIR:-/tmp}" | ||
| lock_file="${OMARCHY_PLUGIN_UPDATE_LOCK:-$runtime_dir/omarchy-plugin-update-$UID.lock}" | ||
| exec 9>"$lock_file" || fail "cannot open update lock: $lock_file" | ||
|
Comment on lines
+172
to
+174
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If Use a private user-owned directory for the fallback. How this was verified: The fallback opens an attacker-precreated shared-directory file for writing and aborts when that file denies write access. |
||
| flock 9 || fail "cannot acquire plugin update lock" | ||
|
|
||
| planned=() | ||
| rc=0 | ||
| for id in "${targets[@]}"; do | ||
| update_one "$id" || rc=1 | ||
| if prepare_one "$id"; then | ||
| planned+=("$id") | ||
| else | ||
| status=$? | ||
| (( status == 2 )) || rc=1 | ||
| fi | ||
| done | ||
|
|
||
| if (( UPDATED_ANY )); then | ||
| omarchy-shell shell rescanPlugins >/dev/null | ||
| # Nothing will mutate the tree, so leave the watcher untouched. | ||
| if (( ${#planned[@]} == 0 )); then | ||
| exit "$rc" | ||
| fi | ||
| exit $rc | ||
|
|
||
| suspend_local_plugin_watch | ||
| trap cleanup EXIT | ||
|
Comment on lines
+193
to
+194
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Cleanup is installed only after the suspension request succeeds. The shell can apply Install cleanup before requesting suspension and record that restoration may be needed before sending the request. |
||
|
|
||
| for id in "${planned[@]}"; do | ||
| apply_one "$id" || rc=1 | ||
| done | ||
|
|
||
| if (( UPDATED_ANY )) && (( SHELL_AVAILABLE )); then | ||
| omarchy-shell shell rescanPlugins >/dev/null || | ||
| echo "omarchy-plugin-update: warning: shell rescan failed; updated plugins will load on the next shell rescan" >&2 | ||
|
Comment on lines
+200
to
+202
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suspension stops Request a rescan after every suspended mutation phase, including failed updates, so existing edits are not lost. |
||
| fi | ||
|
|
||
| resume_local_plugin_watch || true | ||
| trap - EXIT | ||
| exit "$rc" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,10 @@ ShellRoot { | |
|
|
||
| Timer { | ||
| id: localPluginReloadTimer | ||
| interval: 150 | ||
| // Recursive plugin edits can emit large write bursts. Update suspends the | ||
| // watcher around its own merge; this trailing window protects ordinary | ||
| // local edits from reloading while a write burst is still settling. | ||
| interval: 500 | ||
| onTriggered: shell.reloadPlugins() | ||
| } | ||
|
|
||
|
|
@@ -1593,6 +1596,13 @@ ShellRoot { | |
| shell.reloadPlugins() | ||
| } | ||
|
|
||
| function setLocalPluginWatch(enabled: string): string { | ||
| var on = enabled === "true" || enabled === "1" || enabled === "on" | ||
| shell.pluginRegistry.setLocalPluginWatch(on) | ||
| if (!on) localPluginReloadTimer.stop() | ||
| return on ? "watching" : "suspended" | ||
|
Comment on lines
+1599
to
+1603
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The suspension call must wait for existing plugin reads and loads to finish, or refuse mutation until they finish. Knowledge Base Used: Shell UI and desktop interactions |
||
| } | ||
|
|
||
| function reloadConfig(): string { | ||
| userConfigFile.reload() | ||
| return "ok" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" | ||
| require_command git | ||
| require_command flock | ||
|
|
||
| test_dir=$(mktemp -d) | ||
| trap 'rm -rf "$test_dir"' EXIT | ||
| mkdir -p "$test_dir/bin" "$test_dir/home/.config/omarchy/plugins" "$test_dir/remote" | ||
| export CALL_LOG="$test_dir/calls" | ||
| export VALIDATE_STARTED="$test_dir/validate-started" | ||
| export VALIDATE_RELEASE="$test_dir/validate-release" | ||
| export OMARCHY_PLUGIN_UPDATE_LOCK="$test_dir/update.lock" | ||
| : >"$CALL_LOG" | ||
|
|
||
| cat >"$test_dir/bin/omarchy-shell" <<'SH' | ||
| #!/bin/bash | ||
| printf '%s\n' "$*" >>"$CALL_LOG" | ||
|
|
||
| if [[ $* == "shell ping" ]]; then | ||
| exit ${SHELL_AVAILABLE:-0} | ||
| fi | ||
|
|
||
| if [[ $* == "shell setLocalPluginWatch false" && ${FAIL_SUSPEND:-0} == 1 ]]; then | ||
| exit 1 | ||
| fi | ||
|
|
||
| exit 0 | ||
| SH | ||
|
|
||
| cat >"$test_dir/bin/omarchy-plugin-validate" <<'SH' | ||
| #!/bin/bash | ||
| if [[ ${BLOCK_VALIDATE:-0} == 1 && ! -e $VALIDATE_RELEASE ]]; then | ||
| touch "$VALIDATE_STARTED" | ||
| for _ in {1..500}; do | ||
| [[ -e $VALIDATE_RELEASE ]] && break | ||
| sleep 0.01 | ||
| done | ||
| fi | ||
| exit 0 | ||
| SH | ||
|
|
||
| cat >"$test_dir/bin/omarchy-cmd-present" <<'SH' | ||
| #!/bin/bash | ||
| exit 1 | ||
| SH | ||
| chmod +x "$test_dir/bin/"* | ||
|
|
||
| remote="$test_dir/remote/acme.watch.git" | ||
| git init -q --bare "$remote" | ||
|
|
||
| work="$test_dir/work" | ||
| mkdir -p "$work" | ||
| cat >"$work/manifest.json" <<'JSON' | ||
| { | ||
| "schemaVersion": 1, | ||
| "id": "acme.watch", | ||
| "name": "Watch", | ||
| "version": "1.0.0", | ||
| "kinds": ["bar-widget"], | ||
| "entryPoints": { "barWidget": "Widget.qml" }, | ||
| "barWidget": { | ||
| "displayName": "Watch", | ||
| "category": "Test", | ||
| "allowMultiple": false | ||
| } | ||
| } | ||
| JSON | ||
| printf 'import QtQuick\nItem {}\n' >"$work/Widget.qml" | ||
| git -C "$work" init -q | ||
| git -C "$work" add . | ||
| git -C "$work" -c user.name=Test -c user.email=test@example.com commit -qm "v1" | ||
| git -C "$work" branch -M main | ||
| git -C "$work" remote add origin "$remote" | ||
| git -C "$work" push -q origin main | ||
| git -C "$remote" symbolic-ref HEAD refs/heads/main | ||
|
|
||
| installed="$test_dir/home/.config/omarchy/plugins/acme.watch" | ||
| git clone -q -b main "$remote" "$installed" | ||
|
|
||
| advance_remote() { | ||
| local n="$1" | ||
| printf 'import QtQuick\nItem { readonly property int n: %s }\n' "$n" >"$work/Widget.qml" | ||
| git -C "$work" add Widget.qml | ||
| git -C "$work" -c user.name=Test -c user.email=test@example.com commit -qm "v$n" | ||
| git -C "$work" push -q origin main | ||
| } | ||
|
|
||
| run_update() { | ||
| HOME="$test_dir/home" OMARCHY_PATH="$ROOT" PATH="$test_dir/bin:$ROOT/bin:$PATH" \ | ||
| XDG_RUNTIME_DIR="$test_dir" OMARCHY_PLUGIN_UPDATE_LOCK="$OMARCHY_PLUGIN_UPDATE_LOCK" \ | ||
| "$ROOT/bin/omarchy-plugin-update" acme.watch --yes | ||
| } | ||
|
|
||
| advance_remote 2 | ||
| run_update >"$test_dir/out" 2>&1 | ||
|
|
||
| mapfile -t calls <"$CALL_LOG" | ||
| [[ ${calls[0]} == "shell ping" ]] || | ||
| fail "update probes whether a live watcher exists before suspension" "$(cat "$CALL_LOG")" | ||
| [[ ${calls[1]} == "shell setLocalPluginWatch false" ]] || | ||
| fail "update suspends the local plugin watcher before mutation" "$(cat "$CALL_LOG")" | ||
| [[ ${calls[-2]} == "shell rescanPlugins" ]] || | ||
| fail "update rescans once after successful mutation" "$(cat "$CALL_LOG")" | ||
| [[ ${calls[-1]} == "shell setLocalPluginWatch true" ]] || | ||
| fail "update resumes the local plugin watcher after rescan" "$(cat "$CALL_LOG")" | ||
| grep -qF "Updated acme.watch." "$test_dir/out" || | ||
| fail "update reports success" "$(cat "$test_dir/out")" | ||
| pass "plugin update suspends, mutates, rescans, and resumes in order" | ||
|
|
||
| # A reachable shell that cannot suspend the watcher must fail before HEAD moves. | ||
| advance_remote 3 | ||
| before=$(git -C "$installed" rev-parse HEAD) | ||
| : >"$CALL_LOG" | ||
| if FAIL_SUSPEND=1 run_update >"$test_dir/fail-out" 2>&1; then | ||
| fail "update proceeds when a live shell rejects watcher suspension" | ||
| fi | ||
| after=$(git -C "$installed" rev-parse HEAD) | ||
| [[ $before == "$after" ]] || | ||
| fail "failed watcher suspension allowed the plugin checkout to mutate" | ||
| grep -qF "shell setLocalPluginWatch false" "$CALL_LOG" || | ||
| fail "fail-closed path did not attempt watcher suspension" | ||
| ! grep -qF "shell rescanPlugins" "$CALL_LOG" || | ||
| fail "failed watcher suspension should not request a rescan" | ||
| pass "live-shell watcher suspension fails closed before mutation" | ||
|
|
||
| # Two update processes share one mutation/session lock. The second must not | ||
| # suspend or mutate while the first is blocked inside validation. | ||
| : >"$CALL_LOG" | ||
| rm -f "$VALIDATE_STARTED" "$VALIDATE_RELEASE" | ||
| BLOCK_VALIDATE=1 run_update >"$test_dir/first-out" 2>&1 & | ||
| first_pid=$! | ||
|
|
||
| for _ in {1..300}; do | ||
| [[ -e $VALIDATE_STARTED ]] && break | ||
| sleep 0.01 | ||
| done | ||
| [[ -e $VALIDATE_STARTED ]] || { | ||
| kill "$first_pid" 2>/dev/null || true | ||
| fail "first updater did not reach the blocked mutation phase" | ||
| } | ||
|
|
||
| # The first updater has already merged, so the second would report "up to date" | ||
| # even unserialized; only the held lock shows the session is owned. | ||
| if flock -n "$OMARCHY_PLUGIN_UPDATE_LOCK" true; then | ||
| touch "$VALIDATE_RELEASE" | ||
| wait "$first_pid" || true | ||
| fail "first updater does not hold the update lock while mutating" | ||
| fi | ||
|
|
||
| BLOCK_VALIDATE=0 run_update >"$test_dir/second-out" 2>&1 & | ||
| second_pid=$! | ||
| sleep 0.2 | ||
|
|
||
| suspends=$(grep -cF "shell setLocalPluginWatch false" "$CALL_LOG" || true) | ||
| (( suspends == 1 )) || | ||
| fail "overlapping updater acquired watcher ownership before the first finished" "$(cat "$CALL_LOG")" | ||
|
|
||
| touch "$VALIDATE_RELEASE" | ||
| wait "$first_pid" | ||
| wait "$second_pid" | ||
|
|
||
| suspends=$(grep -cF "shell setLocalPluginWatch false" "$CALL_LOG" || true) | ||
| resumes=$(grep -cF "shell setLocalPluginWatch true" "$CALL_LOG" || true) | ||
| (( suspends == 1 && resumes == 1 )) || | ||
| fail "serialized updaters should produce one suspend/resume mutation cycle" "$(cat "$CALL_LOG")" | ||
| grep -qF "acme.watch is up to date." "$test_dir/second-out" || | ||
| fail "second updater did not observe the first updater's completed state" "$(cat "$test_dir/second-out")" | ||
| pass "overlapping plugin updates serialize before watcher ownership and mutation" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pingfailure does not prove the shell has stopped.omarchy-shellalso fails when a live shell takes longer than its timeout to answer. This branch then skips suspension and allows the merge. If the shell recovers during the merge, its watcher can reload partially written plugins—the race this PR aims to fix.Allow mutation without suspension only when the shell is confirmed absent. Abort on an unanswered live shell.
Knowledge Base Used: CLI command platform