code: def stop(self):
          
          if self._stop_event.ready():
              return
          self._stop_event.set()
          self._global_send_event.set()
  
          for retrier in self._address_to_retrier.values():
              if retrier:
                  retrier.notify()
  
          self._client.set_presence_state(UserPresence.OFFLINE.value)
  
          self._client.stop_listener_thread()  # stop sync_thread, wait client's greenlets
          # wait own greenlets, no need to get on them, exceptions should be raised in _run()
          gevent.wait(self.greenlets + [r.greenlet for r in self._address_to_retrier.values()])
  
          # Ensure keep-alive http connections are closed
          self._client.api.session.close()
  
          self.log.debug('Matrix stopped', config=self._config)
          del self.log
docs: Try to gracefully stop the greenlet synchronously
  
          Stop isn't expected to re-raise greenlet _run exception
          (use self.greenlet.get() for that),
          but it should raise any stop-time exception