Backport BROKEN PIPE fix from upstream