-
Notifications
You must be signed in to change notification settings - Fork 35
FEAT: Connection.closed property #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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
closedproperty to the Connection class that returns the state of the internal_closedattribute - 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.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
📋 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
|
…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.
…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.
Work Item / Issue Reference
Summary
This pull request introduces a new
closedproperty to the connection class inmssql_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:
closedproperty to the connection class, which returnsTrueifclose()was called, andFalseotherwise. This property does not indicate connection health, only whetherclose()was explicitly invoked.Test coverage improvements:
closedproperty reflects the connection state before and after callingclose().close()multiple times is safe and theclosedproperty remainsTrue(idempotency).closedproperty when using the connection as a context manager, ensuring it isTrueafter exiting the block.closedproperty accurately reflects the closed state.