Also recover when HYPRLAND_INSTANCE_SIGNATURE is stale

Walker's env can carry a stale HIS across Hyprland restarts — the variable
points at a signature whose socket dir no longer exists, so hyprctl dumps
a plain-text "not running" error to stdout and the TUI silently bails.

Validate HIS against `$XDG_RUNTIME_DIR/hypr/$HIS/` and fall through to the
`hyprctl instances` recovery path when the directory is missing, not only
when HIS is unset. Same detection added to the watcher.
This commit is contained in:
28allday 2026-04-23 22:50:14 +01:00
parent c0c5758bfe
commit 154bcc85d4
2 changed files with 23 additions and 13 deletions

View file

@ -182,20 +182,29 @@ show_header() {
# ===== selection ==============================================================
ensure_hyprland_env() {
# The TUI can be launched from contexts whose env doesn't carry
# HYPRLAND_INSTANCE_SIGNATURE (XDG launchers, cron, ssh). `hyprctl instances`
# still works in that case and prints the running signatures, so recover by
# picking the first one. If we can't, leave HIS unset and let the caller
# surface a friendly error.
if [ -n "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then
# The TUI can be launched from contexts whose env doesn't carry a valid
# HYPRLAND_INSTANCE_SIGNATURE. Two failure modes:
# 1) HIS unset (fresh login shell from a non-graphical context)
# 2) HIS set but STALE — points to a previous Hyprland session whose
# socket no longer exists (seen when Walker's env carries the
# signature from an earlier login). The second case is silent but
# equally broken: hyprctl dumps a plain-text error on stdout.
# `hyprctl instances` always reports the *live* signatures, so recover
# from that. Leave HIS unchanged if we can't find a live instance.
local his="${HYPRLAND_INSTANCE_SIGNATURE:-}"
if [ -n "$his" ] && [ -d "$RUNTIME_DIR/hypr/$his" ]; then
return 0
fi
local sig
sig="$(hyprctl instances 2>/dev/null | awk '/^instance /{sub(/:$/,"",$2); print $2; exit}')"
if [ -n "$sig" ]; then
if [ -n "$sig" ] && [ "$sig" != "$his" ]; then
export HYPRLAND_INSTANCE_SIGNATURE="$sig"
if [ -n "$his" ]; then
log "replaced stale HYPRLAND_INSTANCE_SIGNATURE ($his → $sig)"
else
log "recovered HYPRLAND_INSTANCE_SIGNATURE=$sig from hyprctl instances"
fi
fi
}
get_monitors() {

View file

@ -20,15 +20,16 @@ log() {
printf '[%s] watcher: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >> "$LOG_FILE"
}
if [ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then
# Recover the signature from `hyprctl instances` — same trick the toggle
# script uses when launched from an env that didn't inherit HIS.
his="${HYPRLAND_INSTANCE_SIGNATURE:-}"
if [ -z "$his" ] || [ ! -d "$RUNTIME_DIR/hypr/$his" ]; then
# HIS is missing or stale (points to a dead session). Recover from the
# live signature reported by `hyprctl instances`.
sig="$(hyprctl instances 2>/dev/null | awk '/^instance /{sub(/:$/,"",$2); print $2; exit}')"
if [ -n "$sig" ]; then
export HYPRLAND_INSTANCE_SIGNATURE="$sig"
log "recovered HYPRLAND_INSTANCE_SIGNATURE=$sig from hyprctl instances"
log "using HYPRLAND_INSTANCE_SIGNATURE=$sig (was: ${his:-unset})"
else
log "HYPRLAND_INSTANCE_SIGNATURE not set and no instance found — exiting"
log "HYPRLAND_INSTANCE_SIGNATURE not usable and no live instance found — exiting"
exit 0
fi
fi