Improved locking: use raise instead of abort, create lock before update_code