FIX: Segmentation Fault when interleaving fetchmany and fetchone calls #427#441
FIX: Segmentation Fault when interleaving fetchmany and fetchone calls #427#441gargsaumya wants to merge 3 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a critical segmentation fault that occurred when interleaving fetchmany() and fetchone() calls on a database cursor (GitHub Issue #427). The root cause was that fetchmany() and fetchall() bind ODBC columns to memory buffers for performance, but fetchone() uses SQLGetData to retrieve data, which cannot be used on already-bound columns. The fix unbinds columns after bound fetch operations and at the start of fetchone() to prevent conflicts.
Changes:
- Added comprehensive tests for interleaving
fetchmany()andfetchone()calls to prevent regression - Fixed the segmentation fault by unbinding columns after
fetchmany()andfetchall()operations - Enhanced error handling in
FetchOne_wrapto properly detect and reportSQLGetDatafailures
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_017_fetchmany_fetchone_interleave.py | New test file with three test cases covering basic interleaved fetch scenarios (fetchmany→fetchone, fetchone→fetchmany, and multiple alternating calls) |
| mssql_python/pybind/ddbc_bindings.cpp | Added SQLFreeStmt(SQL_UNBIND) calls in FetchMany_wrap, FetchAll_wrap, and FetchOne_wrap; improved error handling in FetchOne_wrap to return early on SQLGetData failure |
💡 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.pybind.ddbc_bindings.cpp: 69.4%
mssql_python.pybind.ddbc_bindings.h: 69.7%
mssql_python.pybind.connection.connection.cpp: 75.3%
mssql_python.helpers.py: 77.4%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.cursor.py: 84.7%🔗 Quick Links
|
d089df6 to
81da164
Compare
sumitmsft
left a comment
There was a problem hiding this comment.
fairly simple fix. good catch!! lgtm.
Work Item / Issue Reference
Summary
This pull request addresses a segmentation fault issue that occurred when interleaving
fetchmany()andfetchone()calls on a database cursor, as reported in GitHub Issue #427. The main improvements include unbinding columns after fetch operations to prevent conflicts withSQLGetData, and adding comprehensive tests to ensure correct interleaved fetch behavior.Fixes for fetch interleaving bug:
SQLFreeStmt_ptr(hStmt, SQL_UNBIND)afterfetchmany()andfetchall()operations inddbc_bindings.cppto unbind columns, allowing subsequentfetchone()calls to useSQLGetDatawithout causing segmentation faults. [1] [2]FetchOne_wrapto avoid conflicts from previous fetch operations, and improved error handling forSQLGetDatafailures.Testing improvements:
test_017_fetchmany_fetchone_interleave.pywith multiple test cases to verify that interleavingfetchmany()andfetchone()calls works correctly and no longer causes segmentation faults.