Fix lock/unlock bug

In some situations, a lock was acquired but then never released.