Thread cleanup and self-connect avoidance