code: def listen_forever(
              self,
              timeout_ms: int = 30000,
              exception_handler: Callable[[Exception], None] = None,
              bad_sync_timeout: int = 5,
      ):
          
          _bad_sync_timeout = bad_sync_timeout
          self.should_listen = True
          while self.should_listen:
              try:
                  # may be killed and raise exception from _handle_thread
                  self._sync(timeout_ms)
                  _bad_sync_timeout = bad_sync_timeout
              except MatrixRequestError as e:
                  log.warning('A MatrixRequestError occured during sync.')
                  if e.code >= 500:
                      log.warning(
                          'Problem occured serverside. Waiting',
                          wait_for=_bad_sync_timeout,
                      )
                      gevent.sleep(_bad_sync_timeout)
                      _bad_sync_timeout = min(_bad_sync_timeout * 2, self.bad_sync_timeout_limit)
                  else:
                      raise
              except MatrixHttpLibError:
                  log.exception('A MatrixHttpLibError occured during sync.')
                  if self.should_listen:
                      gevent.sleep(_bad_sync_timeout)
                      _bad_sync_timeout = min(_bad_sync_timeout * 2, self.bad_sync_timeout_limit)
              except Exception as e:
                  log.exception('Exception thrown during sync')
                  if exception_handler is not None:
                      exception_handler(e)
                  else:
                      raise
docs: Keep listening for events forever.
          Args:
              timeout_ms: How long to poll the Home Server for before retrying.
              exception_handler: Optional exception handler function which can
                  be used to handle exceptions in the caller thread.
              bad_sync_timeout: Base time to wait after an error before retrying.
                  Will be increased according to exponential backoff.