Fix an issues that mutexes are not unlocked in error cases.