This PR should fix the intermittent failure of sendheaders.py described in #11673. The first three commits are tidying up and refactoring the file. The final commit fix flakiness in sendheaders.py fixes the intermittent failures. The commit message for that commit describes the problems that are being fixed.
I think @laanwj @MeshCollider @MarcoFalke have seen these failures.
fixes #11673