| … | … | @@ -0,0 +1,143 @@ |
| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Diagnose (and optionally clear) a wedged FUSE mount. |
| 4 | # |
| 5 | # WHY THIS EXISTS |
| 6 | # |
| 7 | # A FUSE filesystem whose userspace daemon stops answering leaves every process |
| 8 | # that touches it blocked in UNINTERRUPTIBLE I/O — state D, wchan |
| 9 | # `request_wait_answer`. Such a process cannot be killed: SIGTERM and SIGKILL are |
| 10 | # both ignored until the I/O completes, which it never will. `timeout` appears to |
| 11 | # do nothing, and the process shows no output at all because it blocked before it |
| 12 | # got anywhere. |
| 13 | # |
| 14 | # The symptom that sent us here: Blender hung on startup, before even printing, |
| 15 | # after running fine dozens of times in the same session. Blender enumerates |
| 16 | # mounted volumes at startup, so it touches /run/user/$UID/gvfs — which is where |
| 17 | # GVFS puts an MTP phone. With adb running and a phone attached, one unanswered |
| 18 | # request wedges every subsequent Blender launch. |
| 19 | # |
| 20 | # It is not specific to Blender. Anything that enumerates mounts — file dialogs, |
| 21 | # `df`, backup tools, an editor's file browser — will hang the same way. |
| 22 | # |
| 23 | # WHAT ABORTING DOES |
| 24 | # |
| 25 | # Writing to /sys/fs/fuse/connections/<N>/abort makes all pending and future I/O |
| 26 | # on that connection fail with EIO, which releases the blocked processes. It |
| 27 | # destroys no data on disk. It DOES break that mount: anything using it (the |
| 28 | # phone) must be remounted, usually by unplugging and replugging, or by |
| 29 | # restarting the gvfs daemon. |
| 30 | # |
| 31 | # Because that is disruptive to something the user may be mid-way through, this |
| 32 | # script REPORTS by default and only aborts when explicitly asked. |
| 33 | # |
| 34 | # USAGE |
| 35 | # scripts/unwedge-fuse.sh # diagnose only; touches nothing |
| 36 | # scripts/unwedge-fuse.sh --abort # abort every connection with waiting > 0 |
| 37 | # scripts/unwedge-fuse.sh --abort 54 # abort exactly connection 54 |
| 38 | # scripts/unwedge-fuse.sh --abort 54 --force # even if it looks idle |
| 39 | # |
| 40 | # Aborting needs root, so it re-invokes itself through sudo for that step only. |
| 41 | |
| 42 | set -uo pipefail |
| 43 | |
| 44 | CONNECTIONS=/sys/fs/fuse/connections |
| 45 | DO_ABORT=0 |
| 46 | FORCE=0 |
| 47 | WANT="" |
| 48 | |
| 49 | while [ $# -gt 0 ]; do |
| 50 | case "$1" in |
| 51 | --abort) DO_ABORT=1; shift |
| 52 | # An optional bare number after --abort selects one connection. |
| 53 | if [ $# -gt 0 ] && printf '%s' "$1" | grep -qE '^[0-9]+$'; then WANT="$1"; shift; fi ;; |
| 54 | --force) FORCE=1; shift ;; |
| 55 | -h|--help) sed -n '2,40p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; |
| 56 | *) echo "unknown argument: $1" >&2; exit 2 ;; |
| 57 | esac |
| 58 | done |
| 59 | |
| 60 | if [ ! -d "$CONNECTIONS" ]; then |
| 61 | echo "no $CONNECTIONS — fusectl is not mounted, so there is nothing to inspect" |
| 62 | exit 0 |
| 63 | fi |
| 64 | |
| 65 | # ---------------------------------------------------------------- diagnose |
| 66 | |
| 67 | # A fuse mount's connection number IS the minor device number in mountinfo, which |
| 68 | # is how a bare number under /sys becomes a path a human recognises. |
| 69 | mount_for() { |
| 70 | awk -v want="0:$1" '$3 == want { print $9 " on " $5 }' /proc/self/mountinfo 2>/dev/null | head -1 |
| 71 | } |
| 72 | |
| 73 | echo "FUSE connections:" |
| 74 | wedged="" |
| 75 | for dir in "$CONNECTIONS"/*/; do |
| 76 | [ -d "$dir" ] || continue |
| 77 | n=$(basename "$dir") |
| 78 | waiting=$(cat "$dir/waiting" 2>/dev/null || echo "?") |
| 79 | where=$(mount_for "$n") |
| 80 | [ -n "$where" ] || where="(no matching mount — already unmounted?)" |
| 81 | mark=" " |
| 82 | if [ "$waiting" != "0" ] && [ "$waiting" != "?" ]; then |
| 83 | mark=" *" |
| 84 | wedged="$wedged $n" |
| 85 | fi |
| 86 | printf "%s %-4s waiting=%-4s %s\n" "$mark" "$n" "$waiting" "$where" |
| 87 | done |
| 88 | |
| 89 | echo |
| 90 | echo "Processes blocked in uninterruptible I/O (state D):" |
| 91 | blocked=$(ps -eo pid,stat,wchan:22,comm --no-headers 2>/dev/null | awk '$2 ~ /^D/') |
| 92 | if [ -z "$blocked" ]; then |
| 93 | echo " none" |
| 94 | else |
| 95 | echo "$blocked" | sed 's/^/ /' |
| 96 | echo |
| 97 | echo " These ignore SIGTERM and SIGKILL. Aborting the connection is what frees" |
| 98 | echo " them; killing is not an option while the I/O is outstanding." |
| 99 | fi |
| 100 | |
| 101 | if [ -z "$wedged" ]; then |
| 102 | echo |
| 103 | echo "No connection has requests waiting. If something is still hung, it is not" |
| 104 | echo "this — look elsewhere before aborting anything." |
| 105 | [ "$DO_ABORT" = "1" ] && [ "$FORCE" = "0" ] && exit 0 |
| 106 | fi |
| 107 | |
| 108 | # ------------------------------------------------------------------- abort |
| 109 | |
| 110 | if [ "$DO_ABORT" = "0" ]; then |
| 111 | echo |
| 112 | echo "Diagnosis only; nothing was touched." |
| 113 | [ -n "$wedged" ] && echo "To clear: $0 --abort${WANT:+ $WANT}" |
| 114 | exit 0 |
| 115 | fi |
| 116 | |
| 117 | targets="${WANT:-$wedged}" |
| 118 | if [ -z "$targets" ]; then |
| 119 | echo "nothing to abort" >&2 |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
| 123 | for n in $targets; do |
| 124 | if [ ! -w "$CONNECTIONS/$n/abort" ] && [ ! -e "$CONNECTIONS/$n/abort" ]; then |
| 125 | echo "connection $n does not exist" >&2 |
| 126 | continue |
| 127 | fi |
| 128 | waiting=$(cat "$CONNECTIONS/$n/waiting" 2>/dev/null || echo "?") |
| 129 | if [ "$waiting" = "0" ] && [ "$FORCE" = "0" ]; then |
| 130 | echo "connection $n has nothing waiting; skipping (use --force to abort anyway)" |
| 131 | continue |
| 132 | fi |
| 133 | echo "aborting connection $n ($(mount_for "$n"))" |
| 134 | if [ -w "$CONNECTIONS/$n/abort" ]; then |
| 135 | echo 1 > "$CONNECTIONS/$n/abort" |
| 136 | else |
| 137 | echo 1 | sudo tee "$CONNECTIONS/$n/abort" >/dev/null |
| 138 | fi |
| 139 | done |
| 140 | |
| 141 | echo |
| 142 | echo "Done. Blocked processes should exit within a moment; anything that was using" |
| 143 | echo "the mount (a phone over MTP, for instance) needs reconnecting." |