Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

deadlock when using flask and libsql with local db (works fine with stock sqlite3 library) #30

Description

@Meehai

minimal repro i could come up with. Tested on python 3.11 (created via conda create -n deadlock python=3.11 anaconda)

run via: python main.py [sqlite3/libsql]

Versions:

(deadlock) mihai[deadlock]$ pip freeze | grep "Flask\|libsql"
Flask==3.0.2
Flask-Cors==4.0.0
libsql-client==0.3.1
import libsql_client
import os
import sys
import sqlite3
from flask.testing import FlaskClient
from flask import Flask, Response, current_app

def get_data() -> Response:
    if isinstance(current_app.db, sqlite3.Connection):
        data = current_app.db.execute("select item from data").fetchall()[0][0]
    else:
        data = current_app.db.execute("select item from data").rows[0][0]
    return {"hello": data}, 200

def _setup_db(db_path: str):
    os.remove(db_path)
    client = libsql_client.create_client_sync(f"file:{db_path}")
    client.execute("create table data (id integer primary key unique, item text)")
    client.execute("insert into data(id, item) values(null, 'world')")
    client.close()

def setup_app(db_path: str, engine: str) -> Flask:
    assert engine in ("sqlite3", "libsql"), engine
    app = Flask(__name__)
    app.add_url_rule("/get_data", "get_data", view_func=get_data, methods=["GET"])
    if engine == "sqlite3":
        app.db = sqlite3.Connection(db_path)
    else:
        app.db = libsql_client.create_client_sync(f"file:{db_path}")
    return app

if __name__ == "__main__":
    _setup_db("test.db")
    app = setup_app("test.db", sys.argv[1])
    app.config.update({"TESTING": True})
    flask_client: FlaskClient = app.test_client()
    data = flask_client.get("get_data").json
    assert data["hello"] == "world"

Upon ctrl+c:

^CException ignored in: <module 'threading' from '/home/mihai/libs/miniconda3/envs/deadlock/lib/python3.11/threading.py'>
Traceback (most recent call last):
  File "/home/mihai/libs/miniconda3/envs/deadlock/lib/python3.11/threading.py", line 1590, in _shutdown
    lock.acquire()
KeyboardInterrupt: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions