add semaphore to fix race condition