Fix possible thread leak