Skip to content

fix(pybind): don't hold the GIL in prepare() while the connection is busy - #86

Draft
zahariash wants to merge 2 commits into
LadybugDB:mainfrom
zahariash:fix/release-gil-waiting-for-connection
Draft

zahariash wants to merge 2 commits into
LadybugDB:mainfrom
zahariash:fix/release-gil-waiting-for-connection

Conversation

@zahariash

Copy link
Copy Markdown
Contributor

This work was produced with the help of language models.

Connection.prepare() hung forever if another thread scanned a DataFrame on the same connection, and so did create_arrow_table(), create_arrow_rel_table(), drop_arrow_table(), get_num_nodes(), get_num_rels() and get_all_edges_for_torch_geometric(). Each waited for the scan while holding the GIL, which the scan needs to read the DataFrame. This affects the pybind backend on 0.21.2 through 0.23.0.

Fix. These calls now release the GIL around the engine call only and, like execute(), pin the connection state first, so a close() during the wait cannot free it; that is why the release is in the body and not a call_guard. The three torch-geometric methods share one storage context per connection, which runs BEGIN, a read and COMMIT as separate statements, so they now take a per-connection mutex.

get_all_edges_for_torch_geometric() wrote past the numpy array when another connection added relationships during the fetch; on main that aborts with heap corruption. It now raises instead, sets one thread before every batch, and restores the setting if it raises.

Depends on LadybugDB/ladybug#1718. A statement destroyed on one thread races another thread's execute on the same connection, and without the deadlock a prepare() loop beside a DataFrame scan hits it: 4 of 150 runs crashed on engine main, none of 150 with #1718. The prepare() test keeps its statements until the scan stops; the torch-geometric case drops one per call and can crash on engine main until #1718 is merged.

Tests. test_call_does_not_block_a_dataframe_scan runs each call above, a failing create and a CSR relationship table for 2 seconds beside a DataFrame scan, in a subprocess with a 60-second timeout. All eight cases deadlock on pybind on main and 0.23.0; the five that also run on the C-API backend pass there. Three more tests cover a too-small edge array, an edge fetch while another connection inserts (aborts on main), and both counts from two threads on one connection (crashed without the mutex). Full suites on Python 3.12: no new failures on either backend.

Fixes #85

…busy

Connection.prepare(), create_arrow_table(), create_arrow_rel_table() and
drop_arrow_table(), and the pybind connection's get_num_nodes(),
get_num_rels() and get_all_edges_for_torch_geometric(), called into the
engine with the GIL held. The connection runs one statement at a time, so
while another thread scanned a DataFrame on the same connection the call
waited for that statement, and the scan waited for the GIL to read the
DataFrame. Both threads, and every other Python thread, blocked forever.

These bindings now release the GIL around the engine calls only, as
execute() and query() already do. Like execute(), each first pins the
connection state, so a close() from another thread during the wait cannot
free it; that is why the release is in the body and not a call_guard. The
parameters, Arrow tables and the numpy buffer are read before the release,
and the keep-alive references are stored after the GIL is taken back. Scans
and Python UDFs that run while a statement is compiled take the GIL
themselves, as they do under query().

get_all_edges_for_torch_geometric() runs one statement per batch, so
relationships committed by another connection during the fetch made it
write past the numpy array. It now checks the array size against the
relationship count and raises if the batches return more edges than were
counted. It sets one thread before every batch, since another thread can
now change the setting in between, and restores it when it raises.

The three torch-geometric methods share one StorageDriver context per
connection, which runs BEGIN, a read and COMMIT as separate statements; the
GIL kept two threads out of it, so the calls now take a per-connection
mutex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Opening a default-size in-memory database inside the pytest process fails
on the C-API CI runner ("Buffer manager exception: Mmap for size
8796093022208 failed."), so the six pybind-only tests failed there
instead of skipping. The backend check now uses lazy_init, as conftest
does, and the edge-array test uses the conn_db_in_mem fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adsharma

Copy link
Copy Markdown
Contributor

Defer to 0.24.x cycle

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants