Repository navigation
[QUESTION/FEATURE] How to cancel a query? #424
Description
Activity
dlevy-msft-sql commented
on Feb 8, 2026 ContributorMore actionsThere's no
cursor.cancel()orconnection.cancel()API today — this is a gap we need to close.Additionally, the GIL is currently held during
SQLExecDirect/SQLExecutein the C++ layer, socursor.execute()blocks the entire Python interpreter, not just the calling thread. That means killing the thread won't interrupt the underlying ODBC call.Calling
cursor.close()orconnection.close()from another thread during an in-flight query is not safe — it would free the statement handle while the ODBC driver is still using it.To support cancellation properly, we'd need to:
- Release the GIL during ODBC execute calls (so other threads can run)
- Expose
SQLCancel(hstmt)as acursor.cancel()method (thread-safe per the ODBC spec —SQLCancelis one of the few ODBC functions that can be called from a different thread) - Handle the
HY008(Operation canceled) SQLSTATE that comes back (error mapping already exists)
For now, the closest thing to a workaround is setting a query timeout via the connection string (
Connection Timeout/Command Timeout) orSET LOCK_TIMEOUTin SQL.Tagging this as a feature request. The "Asynchronous Query Execution" item on our roadmap (Q1 2026) overlaps with this — cancel support would be a natural prerequisite.
- addedenhancementNew feature or requestNew feature or request
on Feb 8, 2026 Thanks! Would changing the timeout have any effect on running queries? So if I'd set the timeout to 1, would that immediately stop the running query (given it's been running for a second)?
- addedarea: api-compliancePython API behavior and typing: DB-API 2.0, exceptions, type stubs, new APIs.Python API behavior and typing: DB-API 2.0, exceptions, type stubs, new APIs.
on Jun 4, 2026
Context: I'm creating an implementation of dbt-fabric that uses this driver.
How would I cancel a submitted query? Does the query cancel when the thread that is running
cursor.executegets killed?