Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

sqlite3.OperationalError: no such table: sessions when trying to sign_in after log_out

Open DaopingWang opened this issue 2 years ago • 0 comments

Hi,

I am trying to client.sign_in(), client.log_out() and then client.sign_in() consecutively but failed. The last sign_in() raises sqlite3.OperationalError: no such table: sessions when it tries to connect to Telegram. I am using theses APIs in my Fastapi Backend and calling them through http requests.

Thanks very much in advance!

Checklist

  • [x] The error is in the library's code, and not in my own.
  • [x] I have searched for this issue before posting it and there isn't a duplicate.
  • [x] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Code that causes the issue


client = TelegramClient('telegram_session', API_KEY, API_HASH)

async def signin(
        self,
        phone: str,
        code: str=None,
        password: str=None,
        phone_code_hash: str=None
    ):
        # Check if the client is connected to TG
        if not self._client.is_connected():
            await self._client.connect()    # TRIGGERs ERROR AFTER SIGN OUT

        if signed_in_user is not None:
            print("Already signed in as {0} {1}".format(signed_in_user.first_name, signed_in_user.last_name))
            return signed_in_user

        if phone and not code:
            result = await self._client.send_code_request(phone=phone)
            print("Verification code sent")
            return result

        if phone and code:
            print("Trying to login with given phone, code and hash")
            use_hash = ...
            
            # Omitting the try except as sign in succeeds
            signed_in_user = await self._client.sign_in(phone=phone, code=code, phone_code_hash=use_hash)
            return signed_in_user

async def signout(self):
        result =  await self._client.log_out()
        return result

loop = asyncio.get_event_loop()

# get verification code
loop.run_until_complete(signin(phone)) 

# sign in with phone and code
loop.run_until_complete(signin(phone, code))

# sign out returns true
loop.run_until_complete(signout())

# try to sign in again (same phone) -> sqlite3 no such table session
loop.run_until_complete(signin(phone))

Traceback

Traceback (most recent call last):
  File "/.venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/.venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/fastapi/applications.py", line 261, in __call__
    await super().__call__(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/.venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/.venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/.venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/.venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/.venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/.venv/lib/python3.9/site-packages/starlette/routing.py", line 61, in app
    response = await func(request)
  File "/.venv/lib/python3.9/site-packages/fastapi/routing.py", line 227, in app
    raw_response = await run_endpoint_function(
  File "/.venv/lib/python3.9/site-packages/fastapi/routing.py", line 160, in run_endpoint_function
    return await dependant.call(**values)
  File "/src/./app/api/telegram/telegram_controller.py", line 30, in signin_telegram
    result = await telegram_service.signin(
  File "/src/./app/core/services/telegram_service.py", line 24, in signin
    await self._client.connect()
  File "/.venv/lib/python3.9/site-packages/telethon/client/telegrambaseclient.py", line 536, in connect
    self.session.auth_key = self._sender.auth_key
  File "/.venv/lib/python3.9/site-packages/telethon/sessions/sqlite.py", line 180, in auth_key
    self._update_session_table()
  File "/.venv/lib/python3.9/site-packages/telethon/sessions/sqlite.py", line 194, in _update_session_table
    c.execute('delete from sessions')
sqlite3.OperationalError: no such table: sessions

DaopingWang avatar Mar 25 '22 09:03 DaopingWang

I have the same problem.

swimmwatch avatar Aug 13 '22 11:08 swimmwatch

@DaopingWang, try to change session type to MemorySession in TelegramClient. log_out and sign_in work. SQLiteSession has bug.

swimmwatch avatar Aug 13 '22 18:08 swimmwatch