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
FIX: Ensure cursors are freed before connection (segfault) with added tests (#123)
### ADO Work Item Reference
<!-- Insert your ADO Work Item ID below (e.g. AB#37452) -->
>
[AB#37863](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37863)
-------------------------------------------------------------------
### Summary
<!-- Insert your Copilot Generated Summary below -->
This pull request introduces several updates to enhance connection
management, improve cursor handling, and update test cases for better
coverage and consistency. The most important changes include the
implementation of weak references for cursor tracking, additional
safeguards for connection cleanup, and modifications to test cases to
align with schema conventions.
### Connection Management Enhancements:
* Replaced `ctypes` with `weakref` and introduced `WeakSet` for managing
cursors to prevent memory leaks and ensure automatic cleanup of unused
cursors. (`mssql_python/connection.py`)
[[1]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90L8-R8)
[[2]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R57-R69)
* Added logic to ensure all cursors are closed before closing the
connection and made `close()` idempotent to handle multiple calls
safely. (`mssql_python/connection.py`)
### Cursor Handling Improvements:
* Updated the `cursor()` method to raise an exception if called on a
closed connection and track active cursors using `WeakSet`.
(`mssql_python/connection.py`)
### Test Case Updates:
* Added new tests to validate cursor cleanup behavior, weak reference
handling, cleanup order, and connection idempotency.
(`tests/test_003_connection.py`)
* Updated stored procedure-related tests to use the `dbo` schema prefix
for better compatibility and clarity. (`tests/test_004_cursor.py`)
[[1]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L873-R879)
[[2]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L895-R895)
[[3]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L907-R907)
[[4]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L918-R918)
* Modified table creation and cleanup logic in multiple test cases to
use temporary tables (`#pytest_*`) for isolation and reduced side
effects. (`tests/test_004_cursor.py`, `tests/test_005_exceptions.py`)
[[1]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1153-R1153)
[[2]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1164-R1170)
[[3]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1192-R1231)
[[4]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1240-R1240)
[[5]](diffhunk://#diff-282f76cf72519247beed3fe56aa378a339af5ecbdef32fb468a3470969d15b69L88-R128)
<!--
### PR Title Guide
> For feature requests
FEAT: (short-description)
> For non-feature requests like test case updates, config updates ,
dependency updates etc
CHORE: (short-description)
> For Fix requests
FIX: (short-description)
> For doc update requests
DOC: (short-description)
> For Formatting, indentation, or styling update
STYLE: (short-description)
> For Refactor, without any feature changes
REFACTOR: (short-description)
> For release related changes, without any feature changes
RELEASE: #<RELEASE_VERSION> (short-description)
-->
0 commit comments