Factor out loopWithReconnect.
Use a «MVar ()» instead of QSem.