Fix closing bug, when threads are still running.