fix bug that could cause lock to be overwritten under a race condition