fix race condition between thread and timer