FIX: Fixing test that does not work properly / Failing in connection pool#493
Open
subrata-ms wants to merge 5 commits intomainfrom
Open
FIX: Fixing test that does not work properly / Failing in connection pool#493subrata-ms wants to merge 5 commits intomainfrom
subrata-ms wants to merge 5 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make connection pooling behavior easier to diagnose and make pooling-related tests more reliable, while also hardening cleanup paths (notably destructors / disconnect) to avoid crashes during shutdown or error unwinding.
Changes:
- Updates pooling tests to avoid SPID reuse issues and re-enables previously skipped pooling recovery tests.
- Makes
Connectioncleanup paths more destructor-safe by suppressing exceptions and logging failures. - Adjusts pooling configuration to be reconfigurable across test runs and adds more pool acquisition diagnostics.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
tests/test_009_pooling.py |
Reworks idle-timeout and invalid-connection pooling tests; improves bad-password mutation logic. |
mssql_python/pybind/ddbc_bindings.cpp |
Allows enable_pooling() to reconfigure defaults on each call (removes call_once). |
mssql_python/pybind/connection/connection.cpp |
Makes destructor/disconnect safer by suppressing throws and improving log formatting. |
mssql_python/pybind/connection/connection_pool.cpp |
Adds detailed debug logging around pool acquisition and idle pruning. |
💡 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
mssql_python/pybind/connection/connection.cppLines 55-64 55 disconnect(); // fallback if user forgets to disconnect
56 } catch (...) {
57 // Never throw from a destructor — doing so during stack unwinding
58 // causes std::terminate(). Log and swallow.
! 59 LOG_ERROR("Exception suppressed in ~Connection destructor");
! 60 }
61 }
62
63 // Allocates connection handle
64 void Connection::allocateDbcHandle() {Lines 128-139 128 // SAFETY ASSERTION: Only STMT handles should be in this vector
129 // This is guaranteed by allocStatementHandle() which only creates STMT handles
130 // If this assertion fails, it indicates a serious bug in handle tracking
131 if (handle->type() != SQL_HANDLE_STMT) {
! 132 LOG_ERROR(
! 133 "CRITICAL: Non-STMT handle (type=%d) found in _childStatementHandles. "
! 134 "This will cause a handle leak!",
! 135 handle->type());
136 continue; // Skip marking to prevent leak
137 }
138 handle->markImplicitlyFreed();
139 }Lines 156-165 156 std::string diagMsg = WideToUTF8(err.ddbcErrorMsg);
157 LOG_ERROR("SQLDisconnect diagnostics: %s", diagMsg.c_str());
158 } catch (...) {
159 // Swallow all exceptions: cleanup paths must not throw.
! 160 LOG_ERROR("SQLDisconnect: failed to retrieve ODBC diagnostics");
! 161 }
162 }
163 // Always free the handle regardless of SQLDisconnect result
164 _dbcHandle.reset();
165 } else {📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 67.8%
mssql_python.pybind.ddbc_bindings.cpp: 70.4%
mssql_python.row.py: 70.5%
mssql_python.__init__.py: 77.1%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.connection.py: 85.2%
mssql_python.cursor.py: 85.6%
mssql_python.helpers.py: 89.3%
mssql_python.auth.py: 89.8%🔗 Quick Links
|
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request improves the robustness, reliability, and debuggability of the connection pooling and cleanup logic in the SQL Server Python driver. It introduces safer error handling in destructors, enhances logging for connection pool operations, and makes the pooling tests more reliable and meaningful by using unique connection identifiers and removing unnecessary skips.
Error handling and resource management:
Connectiondestructor now safely handles exceptions fromdisconnect()by logging and suppressing them, preventing potential crashes due to exceptions during stack unwinding.disconnect(), errors fromSQLDisconnectare logged instead of thrown, ensuring cleanup is always attempted and never throws from contexts like destructors or pool cleanup.Logging and diagnostics:
acquire()method to track pool size, idle timeout, and connection idle times, aiding in diagnosing pool behavior.disconnect().Connection pooling configuration:
std::once_flagfromenable_pooling(), allowing reconfiguration of the pool for each test run.Testing improvements:
test_pool_idle_timeout_removes_connectionsnow usesconnection_idinstead of@@SPIDto uniquely identify physical connections and increases the idle wait to avoid flakiness in CI environments.test_pool_removes_invalid_connectionsis now robust, directly simulates a dirty connection, and verifies that the pool discards invalid connections and creates new ones usingconnection_id.test_pool_recovery_after_failed_connectionfor broader compatibility with different connection string formats.