Instead of locking an entire method, lock only the specific resource mutation:
Think of a lock as a digital "Occupied" sign on a restroom door. error resource is write-locked by another thread
with write_lock: # automatically releases on exit resource.write(data) Instead of locking an entire method, lock only
lock.writeLock().lock(); resource.write(data); // if this throws an exception... lock.writeLock().unlock(); // ...this line never runs! Instead of locking an entire method