FIX: Handling Access token with class level variables and removing static defination by subrata-ms · Pull Request #323 · microsoft/mssql-python
Conversation
Work Item / Issue Reference
Summary
This pull request refactors the way temporary buffers are managed for setting connection attributes in the Connection class, improving memory safety and simplifying buffer handling. Instead of using static vectors to store temporary string and binary data, the code now uses member variables to hold these buffers directly within each Connection instance.
Buffer Management Refactor:
- Removed static vectors (
wstr_buffers,buffers) for storing temporary wide string and binary buffers, eliminating the need for manual buffer growth management and cleanup. [1] [2] [3] - Added member variables
wstrStringBufferandstrBytesBufferto theConnectionclass for managing temporary buffers per connection instance, improving encapsulation and memory safety. - Updated all relevant buffer usage in
setAttributeto use these new member variables, replacing references to static buffers with instance variables. [1] [2] [3]
Code Simplification:
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a memory accumulation issue in the Connection::setAttribute method by replacing static vectors that indefinitely stored attribute values with class-level buffers. The static vectors were unnecessary since attribute values only need to persist for the duration of the ODBC API call.
Key changes:
- Removed static
std::vector<std::wstring>andstd::vector<std::string>buffers that accumulated historical attribute values - Introduced two class-level member variables (
wstrStringBufferandstrBytesBuffer) to temporarily hold attribute data - Removed buffer size management code (MAX_BUFFER_COUNT logic) that was previously needed to prevent unbounded growth
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/pybind/connection/connection.h | Adds two class-level buffer member variables for storing string and byte attribute data |
| mssql_python/pybind/connection/connection.cpp | Replaces static vector buffers with class-level buffers and removes buffer management logic |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
@subrata-ms please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
@microsoft-github-policy-service agree [company="{your company}"]
Options:
(default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
(when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"
subrata-ms
changed the title
Handling Access token with class level variables and removing static defination
FIX:Handling Access token with class level variables and removing static defination
subrata-ms
changed the title
FIX:Handling Access token with class level variables and removing static defination
FIX: Handling Access token with class level variables and removing static defination
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple of comments.
📊 Code Coverage Report
🔥 Diff Coverage50% |
🎯 Overall Coverage77% |
📈 Total Lines Covered: |
Diff Coverage
Diff: main...HEAD, staged and unstaged changes
- mssql_python/pybind/connection/connection.cpp (50.0%): Missing lines 258-261
Summary
- Total: 8 lines
- Missing: 4 lines
- Coverage: 50%
mssql_python/pybind/connection/connection.cpp
Lines 254-265
254 } else if (py::isinstance<py::bytes>(value) || 255 py::isinstance<py::bytearray>(value)) { 256 try { 257 std::string binary_data = value.cast<std::string>(); ! 258 this->strBytesBuffer.clear(); ! 259 this->strBytesBuffer = std::move(binary_data); ! 260 SQLPOINTER ptr = const_cast<char*>(this->strBytesBuffer.c_str()); ! 261 SQLINTEGER length = static_cast<SQLINTEGER>(this->strBytesBuffer.size()); 262 263 SQLRETURN ret = SQLSetConnectAttr_ptr(_dbcHandle->get(), 264 attribute, ptr, length); 265 if (!SQL_SUCCEEDED(ret)) {
📋 Files Needing Attention
📉 Files with overall lowest coverage (click to expand)
mssql_python.helpers.py: 67.8% mssql_python.pybind.ddbc_bindings.cpp: 70.7% mssql_python.pybind.connection.connection.cpp: 75.9% mssql_python.pybind.connection.connection_pool.cpp: 78.9% mssql_python.ddbc_bindings.py: 79.6% mssql_python.pybind.ddbc_bindings.h: 79.7% mssql_python.auth.py: 87.1% mssql_python.pooling.py: 87.7% mssql_python.__init__.py: 90.9% mssql_python.exceptions.py: 92.8%
🔗 Quick Links
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