Fix working in closed threads (#675)