Error: Unexpected Error - Database is Locked

Hi all! I’m trying to install dvc in a new environments with no luck. I’m probably messing something up on my end but I can’t find it.

install method: pip install dvc[all]
OS: Ubuntu 20.04
Python: Python3.9
Pip: 21.3.1

The install appears successful, however, any dvc command hangs, times out, and then returns the following:

ERROR: unexpected error - database is locked
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/dvc/main.py", line 55, in main
    ret = cmd.do_run()
  File "/usr/local/lib/python3.9/site-packages/dvc/command/base.py", line 59, in do_run
    return self.run()
  File "/usr/local/lib/python3.9/site-packages/dvc/command/init.py", line 44, in run
    with Repo.init(
  File "/usr/local/lib/python3.9/site-packages/dvc/repo/__init__.py", line 352, in init
    return init(
  File "/usr/local/lib/python3.9/site-packages/dvc/repo/init.py", line 77, in init
    proj = Repo(root_dir)
  File "/usr/local/lib/python3.9/site-packages/dvc/repo/__init__.py", line 214, in __init__
    self.state = State(self.root_dir, state_db_dir, self.dvcignore)
  File "/usr/local/lib/python3.9/site-packages/dvc/state.py", line 64, in __init__
    self.links = Cache(directory=os.path.join(tmp_dir, "links"), **config)
  File "/usr/local/lib/python3.9/site-packages/diskcache/core.py", line 481, in __init__
    self.reset(key, value, update=False)
  File "/usr/local/lib/python3.9/site-packages/diskcache/core.py", line 2452, in reset
    sql('PRAGMA %s = %s' % (pragma, value)).fetchall()
sqlite3.OperationalError: database is locked

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/dvc", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/dvc/main.py", line 88, in main
    dvc_info = get_dvc_info()
  File "/usr/local/lib/python3.9/site-packages/dvc/info.py", line 38, in get_dvc_info
    with Repo() as repo:
  File "/usr/local/lib/python3.9/site-packages/dvc/repo/__init__.py", line 214, in __init__
    self.state = State(self.root_dir, state_db_dir, self.dvcignore)
  File "/usr/local/lib/python3.9/site-packages/dvc/state.py", line 64, in __init__
    self.links = Cache(directory=os.path.join(tmp_dir, "links"), **config)
  File "/usr/local/lib/python3.9/site-packages/diskcache/core.py", line 481, in __init__
    self.reset(key, value, update=False)
  File "/usr/local/lib/python3.9/site-packages/diskcache/core.py", line 2452, in reset
    sql('PRAGMA %s = %s' % (pragma, value)).fetchall()
sqlite3.OperationalError: database is locked

Could this be a permission issue with accessing one of the dvc db files?

Any help is much appreciated as I am new to dvc.

SQLite is meant to be a lightweight database, and thus can’t support a high level of concurrency. OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.

Probably you have another connection in your code that is not closed or not committed and this cause this error.

I have not written any code yet. I have only install dvc and attempted to initialize dvc with dvc init. Does dvc have settings for a locking strategy that can be adjusted?

Looking at the diskcache code, it seems it’s possible to get database is unlocked error, but it does try for 60 seconds before giving up. Do you see that delay of 60 seconds before it raises any error?

Unfortunately, I don’t have any workaround to suggest.