Fix some thread and connection leakage.