You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's no cursor.cancel() or connection.cancel() API today — this is a gap we need to close.
Additionally, the GIL is currently held during SQLExecDirect/SQLExecute in the C++ layer, so cursor.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() or connection.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 a cursor.cancel() method (thread-safe per the ODBC spec — SQLCancel is 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) or SET LOCK_TIMEOUT in 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.
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)?
Activity
dlevy-msft-sql commentedon Feb 8, 2026
There'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:
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)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.
sdebruyn commentedon 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)?
sdebruyn commentedon Oct 20, 2026
In the meantime, the stable version of dbt-fabric is now using this driver. This is still a feature we'd very much like to have in the adapter.
jschuba commentedon Nov 18, 2026
I think the first blocker has been resolved. The GIL is now released during most operations.
See:
#541
#604
axellpadilla commentedon Nov 20, 2026
Following for Cursor.cancel()
fix(mssql-python): cancel in-flight statements via statement handle
FEAT: add native query cancellation foundations (#911)
FEAT: add synchronous cursor cancellation (#915)
jahnvi480 commentedon Nov 26, 2026
We have added new API for cancelling the query and it will be available from this release