LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.93k stars 1.4k forks source link

Error when running file for the second time #1719

Closed Nickelza closed 3 years ago

Nickelza commented 3 years ago

Checklist

Code that causes the issue

from telethon import TelegramClient, events
import json, logging

#Create log
logging.basicConfig(filename="errors.log",
                            filemode='a',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%d-%b-%y %H:%M:%S',
                            level=logging.ERROR)

# Use your own values from my.telegram.org
api_id = "1234"
api_hash = '1234abc'

main_channel_id = 12345678

client = TelegramClient('UB Session', api_id, api_hash)

@client.on(events.NewMessage) #New Message
async def my_event_handler(event):
  try:
    chat = await event.get_chat()
    message = event.message

    if chat.id == main_channel_id:
      async for dialog in client.iter_dialogs(): #Iterate all chats
        try:
          print(dialog.id)
          await message.forward_to(dialog.id)
        except Exception as e:
          logging.error(str(e))
  except Exception as e:
    logging.error(str(e))

client.start()
client.run_until_disconnected()

Traceback

Traceback (most recent call last):
  File "main.py", line 35, in <module>
    client.start()
  File "/home/nick/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 133, in start
    else self.loop.run_until_complete(coro)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/nick/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 140, in _start
    await self.connect()
  File "/home/nick/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 524, in connect
    self.session.auth_key = self._sender.auth_key
  File "/home/nick/.local/lib/python3.8/site-packages/telethon/sessions/sqlite.py", line 180, in auth_key
    self._update_session_table()
  File "/home/nick/.local/lib/python3.8/site-packages/telethon/sessions/sqlite.py", line 194, in _update_session_table
    c.execute('delete from sessions')
sqlite3.OperationalError: database is locked
Exception ignored in: <bound method BaseEventLoop.call_exception_handler of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined
Exception ignored in: <bound method BaseEventLoop.call_exception_handler of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined
Exception ignored in: <bound method BaseEventLoop.call_exception_handler of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined
Exception ignored in: <bound method BaseEventLoop.call_exception_handler of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined

Note Error occurs after starting the Userbot a second time, and disappears if session files are deleted and Userbot instance is redefined, only to re-occur after the second start again. Ex:

Lonami commented 3 years ago

On Linux, use fuser <session file> after you close the script. Are there any processes using the file? Are you sure there is no process running using the session file before you re-launch it?

Nickelza commented 3 years ago

No output when command is run

Lonami commented 3 years ago

Sorry, I don't think I can solve this. I encourage you to try a virtualenv, another Python version, reinstalling Python, or system version and report back. You can also use another session backend as a workaround, docs explain how.