Properly clean up main loops in integration tests