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

Calling Autoimport.connection.execute from differnt thread causes error #713

Closed
tkrabel-db opened this issue Oct 23, 2023 · 1 comment · Fixed by #720
Closed

Calling Autoimport.connection.execute from differnt thread causes error #713

tkrabel-db opened this issue Oct 23, 2023 · 1 comment · Fixed by #720
Labels
bug Unexpected or incorrect user-visible behavior
Milestone

Comments

@tkrabel-db
Copy link

tkrabel-db commented Oct 23, 2023

Describe the bug
When calling any autoimport API that uses Autoimport.connection.execute() from a thread different than the one from which the sqlite db was created, we get an error

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id <x> and this is thread id <y>.

To Reproduce
Steps to reproduce the behavior:

  1. Setup
from rope.base.project import Project
from rope.contrib.autoimport.sqlite import AutoImport

project = Project('.')
autoimport = AutoImport(project, memory=False)
autoimport.generate_cache()  # Generates a cache of the local modules, from the project you're working on
autoimport.generate_modules_cache()  # Generates a cache of external modules

def search_and_print(search_string):
    print(list(autoimport.search_full(search_string)))
  1. This works
search_and_print('numpy')
  1. This throws an error
import threading
thread = threading.Thread(target=search_and_print, args=("numpy",))
thread.start()
thread.join()

Why is this relevant?
rope is used by python-lsp-server, which uses threading

Additional context
The fix is easy. I'll open up a PR

@tkrabel-db tkrabel-db added the bug Unexpected or incorrect user-visible behavior label Oct 23, 2023
@tkrabel-db
Copy link
Author

tkrabel-db commented Oct 23, 2023

I created a PR also to facilitate discussion. I am aware that my proposal makes the sqlite db not threadsafe. python-lsp-server is the user of this feature. Happy to discuss how python-lsp-server can work around that. I have a current workaround in place python-lsp/python-lsp-server#471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected or incorrect user-visible behavior
Projects
None yet
2 participants