Wow, solved it! Using strace and ChatGPT. It's still dumb (even on 4o), so I didn't get an answer from it, but at least I got a hint.
The problem was that my network namespace did not have the lo interface up. Everything was solved by adding one line to the script:
sudo ip -n $NET_NS link set lo up