Skip to content

Comments

FIX: Segmentation Fault when interleaving fetchmany and fetchone calls #427#441

Open
gargsaumya wants to merge 3 commits intomainfrom
saumya/segfault
Open

FIX: Segmentation Fault when interleaving fetchmany and fetchone calls #427#441
gargsaumya wants to merge 3 commits intomainfrom
saumya/segfault

Conversation

@gargsaumya
Copy link
Contributor

@gargsaumya gargsaumya commented Feb 20, 2026

Work Item / Issue Reference

AB#42605

GitHub Issue: #427


Summary

This pull request addresses a segmentation fault issue that occurred when interleaving fetchmany() and fetchone() calls on a database cursor, as reported in GitHub Issue #427. The main improvements include unbinding columns after fetch operations to prevent conflicts with SQLGetData, and adding comprehensive tests to ensure correct interleaved fetch behavior.

Fixes for fetch interleaving bug:

  • Added calls to SQLFreeStmt_ptr(hStmt, SQL_UNBIND) after fetchmany() and fetchall() operations in ddbc_bindings.cpp to unbind columns, allowing subsequent fetchone() calls to use SQLGetData without causing segmentation faults. [1] [2]
  • Added column unbinding at the start of FetchOne_wrap to avoid conflicts from previous fetch operations, and improved error handling for SQLGetData failures.

Testing improvements:

  • Introduced a new test file test_017_fetchmany_fetchone_interleave.py with multiple test cases to verify that interleaving fetchmany() and fetchone() calls works correctly and no longer causes segmentation faults.

Copilot AI review requested due to automatic review settings February 20, 2026 08:30
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 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() and fetchone() calls to prevent regression
  • Fixed the segmentation fault by unbinding columns after fetchmany() and fetchall() operations
  • Enhanced error handling in FetchOne_wrap to properly detect and report SQLGetData failures

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.

@github-actions
Copy link

github-actions bot commented Feb 20, 2026

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

76%


📈 Total Lines Covered: 5509 out of 7185
📁 Project: mssql-python


Diff Coverage

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

  • mssql_python/pybind/ddbc_bindings.cpp (100%)

Summary

  • Total: 7 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.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

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

@gargsaumya gargsaumya changed the title Fix: Segmentation Fault when interleaving fetchmany and fetchone calls #427 FIX: Segmentation Fault when interleaving fetchmany and fetchone calls #427 Feb 20, 2026
@github-actions github-actions bot added the pr-size: small Minimal code update label Feb 20, 2026
Copy link
Contributor

@sumitmsft sumitmsft left a comment

Choose a reason for hiding this comment

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

fairly simple fix. good catch!! lgtm.

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.

2 participants