I think you'll need this entire function wrapped by a write lock due to a potential data race between the read-unlock and the write lock.