fix possible race conditions