[Draft] Async Query Execution - POC - #112
Draft
subrata-ms wants to merge 3 commits into
Draft
Conversation
Replaces the Phase-1 stub with a working async-first connection class: - PyCoreAsyncConnection::new (Python __init__) reuses PyCoreConnection::dict_to_client_context (promoted to pub(crate)) and drives TdsConnectionProvider::create_client on the shared Tokio runtime via block_on inside py.detach — GIL released during handshake. - cursor() returns PyCoreAsyncCursor sharing the Arc<Mutex<TdsClient>>. - close(): best-effort sync close via shared runtime; idempotent (AtomicBool). - is_connected() + __repr__. async_cursor.rs kept as a Phase-3-ready shell — it now stores tds_client and exposes pub(crate) fn new() so async_connection.rs compiles. execute_async / fetchone_async / cancel land in Phase 3. Smoke tested end-to-end against local SQL Server 2022 container: - connect / repr / is_connected / cursor / close / cursor-after-close raises. Regression: tests/test_019_bulkcopy.py still 10/10 passing.
Real async query surface for the mssql-py-core extension. Rust futures returned to Python are driven by the shared Tokio runtime (Phase 1) via pyo3_async_runtimes::tokio::future_into_py, so awaits do not block the asyncio event loop. - AsyncCursorState (Arc-wrapped): AtomicBool has_resultset + parking_lot::Mutex over an Option<CancelHandle>. The sync mutex is never held across .await. - execute_async(query, timeout_sec=30): installs a fresh CancelHandle before spawning the future so concurrent cancel() can find it; drains any stale result set; runs TdsClient::execute with Some(&child) cancel handle; sets has_resultset. A ClearActiveCancelOnDrop guard clears the slot on natural completion so a late cancel() becomes a no-op. - fetchone_async(): fast-path returns None without acquiring the connection lock when has_resultset is false; otherwise decodes one row via PyRowWriter off-GIL and materialises a Python tuple inside Python::attach at the tail. Returns None on exhaustion and closes the result set. - cancel(): consumes the stored CancelHandle and calls CancelHandle::cancel(), dispatching a TDS attention. Safe to invoke from another asyncio task while execute_async or fetchone_async is being awaited. - close(): clears has_resultset and cancels any in-flight op; does not drop the connection. Verified end-to-end against SQL Server 2022 container: - SELECT 42 / multi-row VALUES / re-execute after drain all round-trip. - Non-blocking: ticker task made 60 ticks (10 ms each) during a 400 ms server WAITFOR — loop stays responsive. - Concurrency: two 500 ms queries via asyncio.gather on separate connections finished in 0.555 s (would be ~1 s serialized). - Cancel: WAITFOR DELAY '00:00:10' aborted 5 ms after cur.cancel() with 'Operation Cancelled Error: Request was cancelled'. Regression: tests/test_019_bulkcopy.py still 10/10 passing.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql-py-core/src/async_connection.rsmssql-py-core/src/async_cursor.rs🔗 Quick Links |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Related Issues
Checklist
cargo bfmtpassescargo bclippypassescargo btestpasses