test: use pgrep for better compatibility
pidof is not available on BSD system, while pgrep is present on BSD, Linux and macOS
test: retry when write to a socket fails on macOS
If the socket is tearing down macOS will return EPROTOTYPE instead of EPIPE.
Because python doesn't handle this internally we have to do a workaround and retry the request.
See https://bugs.python.org/issue33450
This is a backport of core#19368