Fix thread not correctly stopping