make locks work properly in case of exceptions