Skip to content

Bug: prepare() deadlocks while another thread scans a DataFrame on the same connection #85

Backlog

Description

@zahariash

This work was produced with the help of language models.

On the pybind backend (the default), Connection.prepare() hangs forever if another thread is running a query over a pandas DataFrame on the same connection. All Python threads stop, and the process has to be killed. create_arrow_table(), create_arrow_rel_table() and drop_arrow_table() hang the same way, and so do get_num_nodes(), get_num_rels() and get_all_edges_for_torch_geometric(), the native connection methods the torch-geometric store uses.

import threading
import time

import ladybug as lb
import numpy as np
import pandas as pd

conn = lb.Connection(lb.Database(":memory:"))
df = pd.DataFrame({"v": np.arange(50_000)})
deadline = time.monotonic() + 5


def scan():
    while time.monotonic() < deadline:
        conn.execute("LOAD FROM $df RETURN count(*)", {"df": df}).get_all()


t = threading.Thread(target=scan)
t.start()
while time.monotonic() < deadline:
    conn.prepare("RETURN $x + 1")
t.join()
print("done")

Expected: done after 5 seconds.
Actual: the process hangs and never prints done.

The script hangs on 0.21.2, 0.22.3 and 0.23.0; on 0.23.0 each of the other calls hangs in the same loop. With LBUG_PYTHON_BACKEND=capi the script finishes.

Cause: these bindings call into the connection while holding the GIL. The connection runs one statement at a time, so the call waits for the running scan, and the scan waits for the GIL to read the DataFrame. execute() and query() release the GIL before they call the engine; set_query_timeout() had the same problem in #63.

Fix: #86

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions