Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Jan 8, 2026

Work Item / Issue Reference

AB#41491

GitHub Issue: #394


Summary

This pull request introduces a new closed property to the connection class in mssql_python/connection.py, providing a clear and explicit way to check if a connection has been closed. Comprehensive tests have been added to ensure correct behavior of this property under various scenarios, improving reliability and clarity for users of the connection API.

Connection class enhancements:

  • Added a closed property to the connection class, which returns True if close() was called, and False otherwise. This property does not indicate connection health, only whether close() was explicitly invoked.

Test coverage improvements:

  • Added tests to verify the closed property reflects the connection state before and after calling close().
  • Added tests to confirm that calling close() multiple times is safe and the closed property remains True (idempotency).
  • Added tests to check the closed property when using the connection as a context manager, ensuring it is True after exiting the block.
  • Added tests to verify that operations on a closed connection raise appropriate exceptions, and the closed property accurately reflects the closed state.

Copilot AI review requested due to automatic review settings January 8, 2026 10:50
@github-actions github-actions bot added the pr-size: small Minimal code update label Jan 8, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request introduces a new closed property to the Connection class, exposing the internal _closed attribute to provide users with a standardized way to check if a connection has been explicitly closed. This aligns with DB-API 2.0 conventions and improves the API's usability.

Key Changes:

  • Added a closed property to the Connection class that returns the state of the internal _closed attribute
  • Added comprehensive test coverage for the new property covering multiple scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
mssql_python/connection.py Added closed property with clear documentation explaining it reflects whether close() was called, not connection health
tests/test_003_connection.py Added four test functions covering basic state checking, idempotent close behavior, context manager usage, and operations on closed connections

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

github-actions bot commented Jan 8, 2026

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

76%


📈 Total Lines Covered: 5442 out of 7117
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/connection.py (100%)

Summary

  • Total: 3 lines
  • Missing: 0 lines
  • Coverage: 100%

📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.logger_bridge.hpp: 58.8%
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.row.py: 66.2%
mssql_python.helpers.py: 67.5%
mssql_python.pybind.ddbc_bindings.cpp: 69.4%
mssql_python.pybind.ddbc_bindings.h: 71.7%
mssql_python.pybind.connection.connection.cpp: 73.6%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 84.1%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

@jahnvi480 jahnvi480 changed the title FEAT: Exposing _closed() method for public FEAT: Connection.closed() property Jan 8, 2026
@jahnvi480 jahnvi480 changed the title FEAT: Connection.closed() property FEAT: Connection.closed property Jan 8, 2026
@jahnvi480 jahnvi480 merged commit 0bc1b5b into main Jan 9, 2026
47 checks passed
dlevy-msft-sql added a commit to dlevy-msft-sql/mssql-python that referenced this pull request Jan 16, 2026
…sor outlives connection

- Check connection.closed property before calling SQLFreeHandle in Cursor.close()
- Skip handle cleanup if parent connection is already closed (handles are invalid)
- Add early sys._is_finalizing() check in Cursor.__del__()
- Add 5 new tests covering the exact reproduction scenario from the issue

This fixes the case where:
1. Connection is closed (ODBC handles freed)
2. Cursor objects still exist in Python memory
3. GC runs during normal execution (not interpreter shutdown)
4. Cursor.__del__() tries to free invalid ODBC statement handle

PR microsoft#361 only checked sys.is_finalizing() which doesn't cover GC during normal execution.
This fix uses the Connection.closed property (from PR microsoft#398) to detect closed connections.
dlevy-msft-sql added a commit to dlevy-msft-sql/mssql-python that referenced this pull request Jan 16, 2026
…sor outlives connection

- Check connection state before calling SQLFreeHandle in Cursor.close()
- Default to SAFE behavior: skip handle cleanup if connection is None/gone/closed
- Only free handle if we can CONFIRM connection is alive and open
- Add early sys._is_finalizing() check in Cursor.__del__()
- Add 5 new tests covering the exact reproduction scenario from the issue

This fixes the case where:
1. Connection is closed (ODBC handles freed)
2. Cursor objects still exist in Python memory
3. GC runs during normal execution (not interpreter shutdown)
4. Cursor.__del__() tries to free invalid ODBC statement handle

Key insight: The previous fix defaulted to 'not closed' when connection was None,
which caused crashes when the connection was garbage collected. Now we default to
'closed' (safe) and only free if we can confirm connection is alive and open.

PR microsoft#361 only checked sys.is_finalizing() which doesn't cover GC during normal execution.
This fix uses the Connection.closed property (from PR microsoft#398) to detect closed connections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: small Minimal code update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants