Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite3.OperationalError: database is locked #294

Closed
shamloo opened this issue Aug 1, 2019 · 16 comments
Closed

sqlite3.OperationalError: database is locked #294

shamloo opened this issue Aug 1, 2019 · 16 comments
Labels

Comments

@shamloo
Copy link

shamloo commented Aug 1, 2019

Great Library.
I am using it and its ok.
in many times i get " sqlite3.OperationalError: database is locked " errors and it stops working.
in there any way to solve this?

@AyraHikari
Copy link

Make sure bot is not running.

If you're using Linux, make sure your work dir owner is your, if you logged in as user but workdir owner is root, it will not allow you to read database.
To change owner just type
chown user workdir -R

If you're using windows, try update to python 3.7, because python3.6.0 have bug at this.

Based on my experience

@shamloo
Copy link
Author

shamloo commented Aug 3, 2019

Seems SQLite dose not support concurrent connection
can we use another storage ?

@delivrance
Copy link
Member

delivrance commented Aug 3, 2019

Seems SQLite dose not support concurrent connection

SQLite does support multiple concurrent connections, but Pyrogram doesn't do anything to allow this nicely, on purpose. The error is a nice-to-have in order to protect the database integrity.

What do you need concurrent connections for? You should not mess with the session file in any way, it's only an internal detail needed to cache peers and persist the authorized key so that you don't have to login again when you restart the client.

If you want to run multiple client instances using the same account, please read this: https://docs.pyrogram.org/faq#can-i-use-multiple-clients-at-once-on-the-same-account

@shamloo
Copy link
Author

shamloo commented Aug 4, 2019

Thank you for your good support.
but still problem

  • OS : CentOS release 6.10 (Final)
  • python version : 3.6
  • SQLite format 3
  • Code struct:
app = Client(
                        session_name = client_name,
                        api_id=******,
                        api_hash="**************************",
                        bot_token=***********************,
                        no_updates = True
                )
app.start()
app.send_message(111111111, "Hi, it's me!")
app.stop()

@delivrance
Copy link
Member

@shamloo Send the full traceback of the error and the full Python version (python3.6 --version).

@shamloo
Copy link
Author

shamloo commented Aug 5, 2019

Traceback (most recent call last):
  File "/root/tdlib/td/py/pyrogram/client/ext/syncer.py", line 80, in sync
    client.storage.save()
  File "/root/tdlib/td/py/pyrogram/client/storage/memory_storage.py", line 91, in save
    self.date = int(time.time())
  File "/root/tdlib/td/py/pyrogram/client/storage/memory_storage.py", line 223, in date
    self._set(value)
  File "/root/tdlib/td/py/pyrogram/client/storage/memory_storage.py", line 190, in _set
    (value,)
sqlite3.OperationalError: database is locked
python3.6 --version
Python 3.6.6

@delivrance
Copy link
Member

@shamloo with your authorized session, call export_session_string and use that to restart the client as explained here: https://docs.pyrogram.org/topics/storage-engines#session-strings

Tell me what happens.

@shamloo
Copy link
Author

shamloo commented Aug 6, 2019

seems app.export_session_string() work for user mode , not bot login mode

Code:

app = Client(
session_name = str(client_name),
api_id=********,
api_hash="****************",
bot_token="********:**************************",
no_updates = True
)
app.start()
app.send_message(chat_id, "Hi, it's me!")
with Client(":memory:") as app:
  print(app.export_session_string())
  f = open("my_bot", 'w')
  f.write(app.export_session_string())
  f.close()


app.stop()

output:
Enter phone number:

config.ini file also present:

[pyrogram]
api_id = *********
api_hash = "***************"
bot_token="**************:**********************"
no_updates = True

@shamloo
Copy link
Author

shamloo commented Aug 6, 2019

@delivrance , Actually i want to use your library to complete my bot , due to lack of a method to delete other bots messages in Bot API.
any simple and straight solution would be appreciated

@shamloo
Copy link
Author

shamloo commented Aug 8, 2019

@delivrance
I am looking forward to hearing from you

@delivrance
Copy link
Member

seems app.export_session_string() work for user mode , not bot login mode

It works, but you have to login first, then use the result from export_session_string as session name.

About this issue, I've added a new FAQ: https://docs.pyrogram.org/dev/faq#sqlite3-operationalerror-database-is-locked

@shamloo
Copy link
Author

shamloo commented Aug 8, 2019

i logged in and then using memory method , its shown on sample code.
i am using different session name for each bot base of bot id.
will your app.stop() method free the database?

@delivrance
Copy link
Member

@shamloo This place is not intended for questions, but for issues with the library. Please join our community and ask there instead: https://t.me/pyrogram

@uday5495pl
Copy link

first find running python3 process
sudo ps -a

==========
8749 pts/1 00:00:00 screen
10093 pts/2 00:00:01 python3
10169 pts/2 00:00:00 sudo
10170 pts/2 00:00:00 ps

step 2
kill python3

sudo kill -9 10093

@SergoZar
Copy link

This is because when you press ctrl + Z (or an error occurs) the script is disabled "aggressively" (or killed) and therefore the data exchanged by the script with the database is terminated. To avoid this, the script must be disabled with Ctrl + C.

p.s. I hope the translation is correct

@dshahrokhian
Copy link

dshahrokhian commented Oct 15, 2023

For anyone facing the same issue 3 years later... just make sure you're using "with" statement with any client operations:

client = Client(ACC_NAME, api_id=API_ID, api_hash=API_HASH)

with client:
  # your methods using pyrogram client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

6 participants