Use deferred unlock to release lock even if errors occur