thread race condition fix