move lock call before the try block