#!/bin/bash
pid=$(pgrep -f "python.*main.py" | head -1)
echo "PID: $pid"

# Find py-spy
echo "=== Finding py-spy ==="
find /home/fabian/ComfyUI/venv -name "py-spy" 2>/dev/null
which py-spy 2>/dev/null
find /home/fabian -name "py-spy" -type f 2>/dev/null | head -3

# Find strace
echo "=== Finding strace ==="
which strace 2>/dev/null
pacman -Ql strace 2>/dev/null | grep bin | head -3

# Open files of the process
echo "=== Open files (all readlink) ==="
readlink /proc/$pid/fd/* 2>/dev/null | head -30

echo ""
echo "=== /proc/pid/wchan (what syscall is it in?) ==="
cat /proc/$pid/wchan 2>/dev/null
echo ""

echo "=== /proc/pid/stack (kernel stack) ==="
sudo cat /proc/$pid/stack 2>/dev/null | head -20

echo ""
echo "=== Thread status ==="
ls /proc/$pid/task/ 2>/dev/null | head -5
echo "..."
ls /proc/$pid/task/ 2>/dev/null | wc -l
echo "total threads"

# Check specific thread that's stuck (prompt_worker)
echo ""
echo "=== Thread wchan (first 10) ==="
for tid in $(ls /proc/$pid/task/ | head -10); do
    wchan=$(cat /proc/$pid/task/$tid/wchan 2>/dev/null)
    stat=$(cat /proc/$pid/task/$tid/stat 2>/dev/null | awk '{print $3}')
    echo "  TID $tid: $wchan (state: $stat)"
done

echo ""
echo "=== comgr cache modify times ==="
ls -lt /home/fabian/.cache/comgr/ 2>/dev/null | head -10
