fix race condition bug