Put the lock.unlock in a finally clause .