code: def create_session(self, lock_type=library.LockType.shared,
                         session=None):
          
          if session is None:
              session = library.ISession()
          # NOTE: The following hack handles the issue of unknown machine state.
          #       This occurs most frequently when a machine is powered off and
          #       in spite waiting for the completion event to end, the state of
          #       machine still raises the following Error:
          #          virtualbox.library.VBoxErrorVmError: 0x80bb0003 (Failed to \
          #          get a console object from the direct session (Unknown \
          #          Status 0x80BB0002))
          error = None
          for _ in range(10):
              try:
                  self.lock_machine(session, lock_type)
              except Exception as exc:
                  error = exc
                  time.sleep(1)
                  continue
              else:
                  break
          else:
              if error is not None:
                  raise Exception("Failed to create clone - %s" % error)
          return session
docs: Lock this machine
  
          Arguments:
              lock_type - see IMachine.lock_machine for details
              session - optionally define a session object to lock this machine
                        against.  If not defined, a new ISession object is
                        created to lock against
  
          return an ISession object