OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / scripts / unwedge-fuse.sh

144 lines · bash ref:
#!/usr/bin/env bash
#
# Diagnose (and optionally clear) a wedged FUSE mount.
#
# WHY THIS EXISTS
#
# A FUSE filesystem whose userspace daemon stops answering leaves every process
# that touches it blocked in UNINTERRUPTIBLE I/O — state D, wchan
# `request_wait_answer`. Such a process cannot be killed: SIGTERM and SIGKILL are
# both ignored until the I/O completes, which it never will. `timeout` appears to
# do nothing, and the process shows no output at all because it blocked before it
# got anywhere.
#
# The symptom that sent us here: Blender hung on startup, before even printing,
# after running fine dozens of times in the same session. Blender enumerates
# mounted volumes at startup, so it touches /run/user/$UID/gvfs — which is where
# GVFS puts an MTP phone. With adb running and a phone attached, one unanswered
# request wedges every subsequent Blender launch.
#
# It is not specific to Blender. Anything that enumerates mounts — file dialogs,
# `df`, backup tools, an editor's file browser — will hang the same way.
#
# WHAT ABORTING DOES
#
# Writing to /sys/fs/fuse/connections/<N>/abort makes all pending and future I/O
# on that connection fail with EIO, which releases the blocked processes. It
# destroys no data on disk. It DOES break that mount: anything using it (the
# phone) must be remounted, usually by unplugging and replugging, or by
# restarting the gvfs daemon.
#
# Because that is disruptive to something the user may be mid-way through, this
# script REPORTS by default and only aborts when explicitly asked.
#
# USAGE
#   scripts/unwedge-fuse.sh              # diagnose only; touches nothing
#   scripts/unwedge-fuse.sh --abort      # abort every connection with waiting > 0
#   scripts/unwedge-fuse.sh --abort 54   # abort exactly connection 54
#   scripts/unwedge-fuse.sh --abort 54 --force   # even if it looks idle
#
# Aborting needs root, so it re-invokes itself through sudo for that step only.

set -uo pipefail

CONNECTIONS=/sys/fs/fuse/connections
DO_ABORT=0
FORCE=0
WANT=""

while [ $# -gt 0 ]; do
  case "$1" in
    --abort) DO_ABORT=1; shift
             # An optional bare number after --abort selects one connection.
             if [ $# -gt 0 ] && printf '%s' "$1" | grep -qE '^[0-9]+$'; then WANT="$1"; shift; fi ;;
    --force) FORCE=1; shift ;;
    -h|--help) sed -n '2,40p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "unknown argument: $1" >&2; exit 2 ;;
  esac
done

if [ ! -d "$CONNECTIONS" ]; then
  echo "no $CONNECTIONS — fusectl is not mounted, so there is nothing to inspect"
  exit 0
fi

# ---------------------------------------------------------------- diagnose

# A fuse mount's connection number IS the minor device number in mountinfo, which
# is how a bare number under /sys becomes a path a human recognises.
mount_for() {
  awk -v want="0:$1" '$3 == want { print $9 " on " $5 }' /proc/self/mountinfo 2>/dev/null | head -1
}

echo "FUSE connections:"
wedged=""
for dir in "$CONNECTIONS"/*/; do
  [ -d "$dir" ] || continue
  n=$(basename "$dir")
  waiting=$(cat "$dir/waiting" 2>/dev/null || echo "?")
  where=$(mount_for "$n")
  [ -n "$where" ] || where="(no matching mount — already unmounted?)"
  mark="  "
  if [ "$waiting" != "0" ] && [ "$waiting" != "?" ]; then
    mark=" *"
    wedged="$wedged $n"
  fi
  printf "%s %-4s waiting=%-4s %s\n" "$mark" "$n" "$waiting" "$where"
done

echo
echo "Processes blocked in uninterruptible I/O (state D):"
blocked=$(ps -eo pid,stat,wchan:22,comm --no-headers 2>/dev/null | awk '$2 ~ /^D/')
if [ -z "$blocked" ]; then
  echo "  none"
else
  echo "$blocked" | sed 's/^/  /'
  echo
  echo "  These ignore SIGTERM and SIGKILL. Aborting the connection is what frees"
  echo "  them; killing is not an option while the I/O is outstanding."
fi

if [ -z "$wedged" ]; then
  echo
  echo "No connection has requests waiting. If something is still hung, it is not"
  echo "this — look elsewhere before aborting anything."
  [ "$DO_ABORT" = "1" ] && [ "$FORCE" = "0" ] && exit 0
fi

# ------------------------------------------------------------------- abort

if [ "$DO_ABORT" = "0" ]; then
  echo
  echo "Diagnosis only; nothing was touched."
  [ -n "$wedged" ] && echo "To clear:  $0 --abort${WANT:+ $WANT}"
  exit 0
fi

targets="${WANT:-$wedged}"
if [ -z "$targets" ]; then
  echo "nothing to abort" >&2
  exit 1
fi

for n in $targets; do
  if [ ! -w "$CONNECTIONS/$n/abort" ] && [ ! -e "$CONNECTIONS/$n/abort" ]; then
    echo "connection $n does not exist" >&2
    continue
  fi
  waiting=$(cat "$CONNECTIONS/$n/waiting" 2>/dev/null || echo "?")
  if [ "$waiting" = "0" ] && [ "$FORCE" = "0" ]; then
    echo "connection $n has nothing waiting; skipping (use --force to abort anyway)"
    continue
  fi
  echo "aborting connection $n ($(mount_for "$n"))"
  if [ -w "$CONNECTIONS/$n/abort" ]; then
    echo 1 > "$CONNECTIONS/$n/abort"
  else
    echo 1 | sudo tee "$CONNECTIONS/$n/abort" >/dev/null
  fi
done

echo
echo "Done. Blocked processes should exit within a moment; anything that was using"
echo "the mount (a phone over MTP, for instance) needs reconnecting."