Ensure everything is finalized properly, especially that the lock is unlocked

The issue here was that due to the method call there was no guarantee that the unlock was reached in case of an exception.
Restructuring the code like this provides this guarantee