From 889cd7057fd7164da00f9dcc152cccf3c6d32b25 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 13 Oct 2026 15:25:33 +0200 Subject: [PATCH 1/3] Reap orphaned plugin watchers at shell start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quickshell does not take its Process children with it when it stops, so every shell restart left the plugin-directory inotifywait running, reparented to init — 79 of them on one machine. Stop the watcher on a graceful destruction, and at startup, before starting our own, stop any plugin watcher whose parent is no longer a shell. A live shell's watcher, parented to it, is spared. --- shell/services/PluginRegistry.qml | 31 +++++++++ test/shell.d/plugin-watcher-reap-test.sh | 85 ++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100755 test/shell.d/plugin-watcher-reap-test.sh diff --git a/shell/services/PluginRegistry.qml b/shell/services/PluginRegistry.qml index 46908196e1a8..38d78a657195 100644 --- a/shell/services/PluginRegistry.qml +++ b/shell/services/PluginRegistry.qml @@ -671,12 +671,43 @@ QtObject { } property Process initProcess: Process { + onExited: { + registry.reapOrphanedPluginWatchers() + } + } + + // Quickshell does not take its Process children with it when it stops, so + // every shell restart leaves this watcher running, reparented to init + // (63 of them on one long-lived machine). Before starting our own, stop + // any plugin watcher whose parent is no longer a shell; a live shell's + // watcher — parented to it — is left alone. The [i] keeps this script's + // own command line from matching its search. + property Process watcherReaper: Process { onExited: { localPluginWatcher.running = true registry.rescan() } } + function reapOrphanedPluginWatchers() { + watcherReaper.command = ["bash", "-c", + "for p in $(pgrep -f '[i]notifywait -m -r -q -e close_write,create,delete,move' || true); do " + + "tr '\\0' ' ' < \"/proc/$p/cmdline\" 2>/dev/null | grep -qF \"$1\" || continue; " + + "parent=$(ps -o ppid= -p \"$p\" 2>/dev/null | tr -d ' '); " + + "[[ $parent =~ ^[0-9]+$ ]] || continue; " + + "case \"$(ps -o comm= -p \"$parent\" 2>/dev/null)\" in quickshell|qs) ;; *) kill \"$p\" 2>/dev/null || true ;; esac; " + + "done; true", + "watcher-reaper", registry.pluginsDir] + watcherReaper.running = true + } + + // A graceful stop takes the watcher with it; the reaper above covers the + // stops that never run this (a killed or crashed shell). + Component.onDestruction: { + localPluginWatcherRestart.stop() + localPluginWatcher.running = false + } + property Process localPluginWatcher: Process { command: [ "inotifywait", diff --git a/test/shell.d/plugin-watcher-reap-test.sh b/test/shell.d/plugin-watcher-reap-test.sh new file mode 100755 index 000000000000..b777d080c6c5 --- /dev/null +++ b/test/shell.d/plugin-watcher-reap-test.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +registry="$ROOT/shell/services/PluginRegistry.qml" +source_text=$(cat "$registry") + +# Source wiring: shell start reaps orphaned watchers before starting its +# own, spares one still parented to a live shell, and a graceful stop +# takes its watcher with it. +[[ $source_text == *"function reapOrphanedPluginWatchers()"* ]] || + fail "plugin registry reaps orphaned watchers at startup" +[[ $source_text == *"ps -o comm="* && $source_text == *"quickshell|qs"* ]] || + fail "watcher reaper spares watchers still parented to a live shell" +[[ $source_text == *"Component.onDestruction"* && $source_text == *"localPluginWatcher.running = false"* ]] || + fail "plugin watcher stops with its shell" +[[ $source_text == *"registry.reapOrphanedPluginWatchers()"* ]] || + fail "shell init runs the watcher reaper before starting the watcher" +pass "plugin watcher lifecycle is wired to the shell" + +command -v inotifywait >/dev/null 2>&1 || { skip "inotifywait not installed; skipping watcher reap check"; exit 0; } + +TMPDIR=$(mktemp -d) +trap 'rm -rf "$TMPDIR"' EXIT +mkdir -p "$TMPDIR/plugins" + +# An orphaned watcher: detached, so its parent is gone before the reap. +setsid -f inotifywait -m -r -q -e close_write,create,delete,move --format '%w%f' "$TMPDIR/plugins" /dev/null 2>&1 +orphan="" +for _ in {1..30}; do + orphan=$(pgrep -f "inotifywait -m -r -q -e close_write,create,delete,move --format %w%f $TMPDIR/plugins" | head -1 || true) + [[ -n $orphan ]] && break + sleep 0.1 +done +[[ -n $orphan ]] || fail "could not start an orphaned plugin watcher" +pass "orphaned plugin watcher started" + +# Same flags, unrelated directory: not the shell's watcher to reap. +mkdir -p "$TMPDIR/other" +setsid -f inotifywait -m -r -q -e close_write,create,delete,move --format '%w%f' "$TMPDIR/other" /dev/null 2>&1 +unrelated="" +for _ in {1..30}; do + unrelated=$(pgrep -f "inotifywait -m -r -q -e close_write,create,delete,move --format %w%f $TMPDIR/other" | head -1 || true) + [[ -n $unrelated ]] && break + sleep 0.1 +done +[[ -n $unrelated ]] || fail "could not start an unrelated watcher" + +# The reap itself, exactly as PluginRegistry runs it at shell start +# ($1 is the plugins directory there; the scratch one here). +for p in $(pgrep -f '[i]notifywait -m -r -q -e close_write,create,delete,move' || true); do + tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null | grep -qF "$TMPDIR/plugins" || continue + parent=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d ' ') + [[ $parent =~ ^[0-9]+$ ]] || continue + case "$(ps -o comm= -p "$parent" 2>/dev/null)" in quickshell|qs) ;; *) kill "$p" 2>/dev/null || true ;; esac +done + +for _ in {1..30}; do + kill -0 "$orphan" 2>/dev/null || break + sleep 0.1 +done +if kill -0 "$orphan" 2>/dev/null; then + fail "watcher reaper stops an orphaned plugin watcher" +fi +pass "watcher reaper stops an orphaned plugin watcher" + +kill -0 "$unrelated" 2>/dev/null || fail "watcher reaper leaves unrelated watchers alone" +kill "$unrelated" 2>/dev/null || true +pass "watcher reaper leaves unrelated watchers alone" + +# A watcher still parented to a live shell survives the reap. +live="" +while IFS= read -r p; do + parent=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d ' ') + [[ $parent =~ ^[0-9]+$ ]] || continue + case "$(ps -o comm= -p "$parent" 2>/dev/null)" in quickshell|qs) live=$p; break ;; esac +done < <(pgrep -f 'inotifywait -m -r -q -e close_write,create,delete,move' || true) +if [[ -n $live ]]; then + kill -0 "$live" 2>/dev/null || fail "watcher reaper spares a live shell's watcher" + pass "watcher reaper spares a live shell's watcher" +else + echo "ok - watcher reaper spares a live shell's watcher # SKIP no live shell watcher" +fi From f38459068ba6c64288dfb58c556540cc2b3e009e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 13 Oct 2026 16:08:31 +0200 Subject: [PATCH 2/3] Match the reaped watcher's directory exactly, and test the real reap Greptile: a substring match also caught a watcher of plugins-backup; the test copied the reap instead of running it and only looked for a live watcher after reaping. The reaper now matches a whole command-line argument, and the test extracts the real reap command from PluginRegistry.qml, finds its live watcher (a stand-in named quickshell) before reaping, and checks it and a similarly named directory's watcher survive. --- shell/services/PluginRegistry.qml | 2 +- test/shell.d/plugin-watcher-reap-test.sh | 101 +++++++++++++---------- 2 files changed, 57 insertions(+), 46 deletions(-) diff --git a/shell/services/PluginRegistry.qml b/shell/services/PluginRegistry.qml index 38d78a657195..3d8be46cab20 100644 --- a/shell/services/PluginRegistry.qml +++ b/shell/services/PluginRegistry.qml @@ -692,7 +692,7 @@ QtObject { function reapOrphanedPluginWatchers() { watcherReaper.command = ["bash", "-c", "for p in $(pgrep -f '[i]notifywait -m -r -q -e close_write,create,delete,move' || true); do " - + "tr '\\0' ' ' < \"/proc/$p/cmdline\" 2>/dev/null | grep -qF \"$1\" || continue; " + + "tr '\\0' '\\n' < \"/proc/$p/cmdline\" 2>/dev/null | grep -Fxq \"$1\" || continue; " + "parent=$(ps -o ppid= -p \"$p\" 2>/dev/null | tr -d ' '); " + "[[ $parent =~ ^[0-9]+$ ]] || continue; " + "case \"$(ps -o comm= -p \"$parent\" 2>/dev/null)\" in quickshell|qs) ;; *) kill \"$p\" 2>/dev/null || true ;; esac; " diff --git a/test/shell.d/plugin-watcher-reap-test.sh b/test/shell.d/plugin-watcher-reap-test.sh index b777d080c6c5..69d4cbbab0d1 100755 --- a/test/shell.d/plugin-watcher-reap-test.sh +++ b/test/shell.d/plugin-watcher-reap-test.sh @@ -21,41 +21,63 @@ source_text=$(cat "$registry") pass "plugin watcher lifecycle is wired to the shell" command -v inotifywait >/dev/null 2>&1 || { skip "inotifywait not installed; skipping watcher reap check"; exit 0; } +command -v node >/dev/null 2>&1 || { skip "node not installed; skipping watcher reap check"; exit 0; } TMPDIR=$(mktemp -d) -trap 'rm -rf "$TMPDIR"' EXIT -mkdir -p "$TMPDIR/plugins" +trap 'pkill -f "inotifywait.*$TMPDIR" 2>/dev/null; rm -rf "$TMPDIR"' EXIT +mkdir -p "$TMPDIR/plugins" "$TMPDIR/plugins-backup" "$TMPDIR/other" -# An orphaned watcher: detached, so its parent is gone before the reap. -setsid -f inotifywait -m -r -q -e close_write,create,delete,move --format '%w%f' "$TMPDIR/plugins" /dev/null 2>&1 -orphan="" -for _ in {1..30}; do - orphan=$(pgrep -f "inotifywait -m -r -q -e close_write,create,delete,move --format %w%f $TMPDIR/plugins" | head -1 || true) - [[ -n $orphan ]] && break - sleep 0.1 -done +# The reap command exactly as PluginRegistry builds it, extracted rather +# than copied, so this test fails when the real command changes. +node -e ' + const fs = require("fs") + const src = fs.readFileSync(process.argv[1], "utf8") + const start = src.indexOf("watcherReaper.command") + const end = src.indexOf("watcher-reaper", start) + if (start < 0 || end < 0) process.exit(1) + const parts = [...src.slice(start, end).matchAll(/"((?:[^"\\]|\\.)*)"/g)].map(m => JSON.parse(`"${m[1]}"`)) + if (parts.length < 3) process.exit(1) + process.stdout.write(parts.slice(2).join("")) +' "$registry" >"$TMPDIR/reap.sh" +[[ -s $TMPDIR/reap.sh ]] || fail "could not extract the watcher reap command" +grep -q 'grep -Fxq' "$TMPDIR/reap.sh" || fail "watcher reaper matches the watched directory exactly" + +start_watcher() { # dir, prints pid + setsid -f inotifywait -m -r -q -e close_write,create,delete,move --format '%w%f' "$1" /dev/null 2>&1 + local pid="" + for _ in {1..30}; do + pid=$(pgrep -f "inotifywait -m -r -q -e close_write,create,delete,move --format %w%f $1" | head -1 || true) + [[ -n $pid ]] && break + sleep 0.1 + done + printf '%s' "$pid" +} + +orphan=$(start_watcher "$TMPDIR/plugins") [[ -n $orphan ]] || fail "could not start an orphaned plugin watcher" -pass "orphaned plugin watcher started" -# Same flags, unrelated directory: not the shell's watcher to reap. -mkdir -p "$TMPDIR/other" -setsid -f inotifywait -m -r -q -e close_write,create,delete,move --format '%w%f' "$TMPDIR/other" /dev/null 2>&1 -unrelated="" -for _ in {1..30}; do - unrelated=$(pgrep -f "inotifywait -m -r -q -e close_write,create,delete,move --format %w%f $TMPDIR/other" | head -1 || true) - [[ -n $unrelated ]] && break - sleep 0.1 -done -[[ -n $unrelated ]] || fail "could not start an unrelated watcher" +# Same flags, similarly named directory: not this shell's watcher. +backup=$(start_watcher "$TMPDIR/plugins-backup") +[[ -n $backup ]] || fail "could not start a similarly named directory's watcher" -# The reap itself, exactly as PluginRegistry runs it at shell start -# ($1 is the plugins directory there; the scratch one here). -for p in $(pgrep -f '[i]notifywait -m -r -q -e close_write,create,delete,move' || true); do - tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null | grep -qF "$TMPDIR/plugins" || continue - parent=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d ' ') - [[ $parent =~ ^[0-9]+$ ]] || continue - case "$(ps -o comm= -p "$parent" 2>/dev/null)" in quickshell|qs) ;; *) kill "$p" 2>/dev/null || true ;; esac -done +# A watcher parented to a live shell (a stand-in process named +# quickshell, so this runs without a desktop session), found before the +# reap runs. +command -v python3 >/dev/null 2>&1 || { skip "python3 not installed; skipping watcher reap check"; exit 0; } +setsid -f python3 -c ' +import ctypes, subprocess, sys, time +ctypes.CDLL(None).prctl(15, b"quickshell", 0, 0, 0) +child = subprocess.Popen(["inotifywait", "-m", "-r", "-q", "-e", "close_write,create,delete,move", "--format", "%w%f", sys.argv[1]]) +open(sys.argv[2], "w").write(str(child.pid)) +time.sleep(30) +' "$TMPDIR/plugins" "$TMPDIR/live.pid" "$TMPDIR/live.log" 2>&1 +for _ in {1..30}; do [[ -s $TMPDIR/live.pid ]] && break; sleep 0.1; done +live=$(cat "$TMPDIR/live.pid" 2>/dev/null || true) +[[ -n $live ]] || fail "could not start a live shell's watcher" +pass "orphaned, similarly named, and live plugin watchers started" + +# The reap itself, run the way shell init runs it ($1 is the plugins dir). +bash -c "$(cat "$TMPDIR/reap.sh")" watcher-reaper "$TMPDIR/plugins" for _ in {1..30}; do kill -0 "$orphan" 2>/dev/null || break @@ -66,20 +88,9 @@ if kill -0 "$orphan" 2>/dev/null; then fi pass "watcher reaper stops an orphaned plugin watcher" -kill -0 "$unrelated" 2>/dev/null || fail "watcher reaper leaves unrelated watchers alone" -kill "$unrelated" 2>/dev/null || true -pass "watcher reaper leaves unrelated watchers alone" +kill -0 "$backup" 2>/dev/null || fail "watcher reaper leaves a similarly named directory's watcher alone" +kill "$backup" 2>/dev/null || true +pass "watcher reaper leaves a similarly named directory's watcher alone" -# A watcher still parented to a live shell survives the reap. -live="" -while IFS= read -r p; do - parent=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d ' ') - [[ $parent =~ ^[0-9]+$ ]] || continue - case "$(ps -o comm= -p "$parent" 2>/dev/null)" in quickshell|qs) live=$p; break ;; esac -done < <(pgrep -f 'inotifywait -m -r -q -e close_write,create,delete,move' || true) -if [[ -n $live ]]; then - kill -0 "$live" 2>/dev/null || fail "watcher reaper spares a live shell's watcher" - pass "watcher reaper spares a live shell's watcher" -else - echo "ok - watcher reaper spares a live shell's watcher # SKIP no live shell watcher" -fi +kill -0 "$live" 2>/dev/null || fail "watcher reaper spares a live shell's watcher" +pass "watcher reaper spares a live shell's watcher" From 74f872c0cc9214379681874159667d3367be8613 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 13 Oct 2026 16:16:28 +0200 Subject: [PATCH 3/3] Keep the reaped-watcher checks honest about zombies and cleanup Greptile: the stand-in parent never waited, so a killed watcher could linger as a zombie that kill -0 still sees; poll the child, check the survivor is not a zombie, and let the cleanup trap run when pkill finds nothing. --- test/shell.d/plugin-watcher-reap-test.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/shell.d/plugin-watcher-reap-test.sh b/test/shell.d/plugin-watcher-reap-test.sh index 69d4cbbab0d1..6cffa1d219c5 100755 --- a/test/shell.d/plugin-watcher-reap-test.sh +++ b/test/shell.d/plugin-watcher-reap-test.sh @@ -24,7 +24,7 @@ command -v inotifywait >/dev/null 2>&1 || { skip "inotifywait not installed; ski command -v node >/dev/null 2>&1 || { skip "node not installed; skipping watcher reap check"; exit 0; } TMPDIR=$(mktemp -d) -trap 'pkill -f "inotifywait.*$TMPDIR" 2>/dev/null; rm -rf "$TMPDIR"' EXIT +trap 'pkill -f "inotifywait.*$TMPDIR" 2>/dev/null || true; rm -rf "$TMPDIR"' EXIT mkdir -p "$TMPDIR/plugins" "$TMPDIR/plugins-backup" "$TMPDIR/other" # The reap command exactly as PluginRegistry builds it, extracted rather @@ -69,7 +69,9 @@ import ctypes, subprocess, sys, time ctypes.CDLL(None).prctl(15, b"quickshell", 0, 0, 0) child = subprocess.Popen(["inotifywait", "-m", "-r", "-q", "-e", "close_write,create,delete,move", "--format", "%w%f", sys.argv[1]]) open(sys.argv[2], "w").write(str(child.pid)) -time.sleep(30) +deadline = time.time() + 30 +while child.poll() is None and time.time() < deadline: + time.sleep(0.1) ' "$TMPDIR/plugins" "$TMPDIR/live.pid" "$TMPDIR/live.log" 2>&1 for _ in {1..30}; do [[ -s $TMPDIR/live.pid ]] && break; sleep 0.1; done live=$(cat "$TMPDIR/live.pid" 2>/dev/null || true) @@ -93,4 +95,5 @@ kill "$backup" 2>/dev/null || true pass "watcher reaper leaves a similarly named directory's watcher alone" kill -0 "$live" 2>/dev/null || fail "watcher reaper spares a live shell's watcher" +[[ $(ps -o stat= -p "$live" 2>/dev/null) != Z* ]] || fail "watcher reaper spares a live shell's watcher" pass "watcher reaper spares a live shell's watcher"