Skip to content

FIX: Robust resource cleanup of Connection/Cursor for graceful exit & add tests#128

Merged
bewithgaurav merged 7 commits intomainfrom
bewithgaurav/conn_cursor_resource_cleanup
Jul 16, 2025
Merged

FIX: Robust resource cleanup of Connection/Cursor for graceful exit & add tests#128
bewithgaurav merged 7 commits intomainfrom
bewithgaurav/conn_cursor_resource_cleanup

Conversation

@bewithgaurav
Copy link
Copy Markdown
Collaborator

@bewithgaurav bewithgaurav commented Jul 8, 2025

ADO Work Item Reference

AB#37889
Github Issue - #131


Summary

This pull request introduces enhancements to cursor and connection management in the mssql_python library, aiming to improve resource cleanup and debugging capabilities. Key changes include adding destructors for automatic cleanup, enhancing logging for better traceability, and introducing new tests to validate cursor behavior during garbage collection and connection closure.

Resource Cleanup Enhancements:

  • Added a __del__ method in mssql_python/connection.py to automatically close connections during object destruction, with error logging for cleanup issues.
  • Added a __del__ method in mssql_python/cursor.py to ensure cursors are closed when no longer needed, with error handling during destruction.

Debugging Improvements:

  • Enhanced logging in mssql_python/connection.py and mssql_python/cursor.py with detailed print statements for tracking cursor states and operations (e.g., during connection closure, statement execution, and data fetching). [1] [2] [3] [4]

Testing Additions:

  • Introduced test_cursor_cleanup_without_close to verify that cursors are properly cleaned up when the connection is not explicitly closed.
  • Added test_no_segfault_on_gc to ensure no segmentation faults occur during garbage collection of connections and cursors. This test runs in a subprocess to isolate potential crashes.

@github-actions github-actions bot added the pr-size: medium Moderate update size label Jul 8, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 8, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 8, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 8, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 10, 2025
jahnvi480
jahnvi480 previously approved these changes Jul 16, 2025
Comment thread mssql_python/connection.py
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Jul 16, 2025
Copy link
Copy Markdown
Contributor

@gargsaumya gargsaumya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bewithgaurav bewithgaurav merged commit 4116996 into main Jul 16, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault while exiting when both cursor/connection are left unclosed

3 participants