Release the lock in a finally block to ensure this happens if there are
errors.