Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,10 @@ SQLRETURN FetchMany_wrap(SqlHandlePtr StatementHandle, py::list& rows, int fetch
// Reset attributes before returning to avoid using stack pointers later
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)1, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, NULL, 0);

// Unbind columns to allow subsequent fetchone() calls to use SQLGetData
SQLFreeStmt_ptr(hStmt, SQL_UNBIND);

return ret;
}

Expand Down Expand Up @@ -4228,6 +4232,9 @@ SQLRETURN FetchAll_wrap(SqlHandlePtr StatementHandle, py::list& rows,
// Reset attributes before returning to avoid using stack pointers later
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)1, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, NULL, 0);

// Unbind columns to allow subsequent fetchone() calls to use SQLGetData
SQLFreeStmt_ptr(hStmt, SQL_UNBIND);

return ret;
}
Expand All @@ -4254,12 +4261,21 @@ SQLRETURN FetchOne_wrap(SqlHandlePtr StatementHandle, py::list& row,
SQLRETURN ret;
SQLHSTMT hStmt = StatementHandle->get();

// Unbind any columns from previous fetch operations (e.g., fetchmany)
// to avoid conflicts with SQLGetData. SQLGetData cannot be used on
// columns that are already bound.
SQLFreeStmt_ptr(hStmt, SQL_UNBIND);

// Assume hStmt is already allocated and a query has been executed
ret = SQLFetch_ptr(hStmt);
if (SQL_SUCCEEDED(ret)) {
// Retrieve column count
SQLSMALLINT colCount = SQLNumResultCols_wrap(StatementHandle);
ret = SQLGetData_wrap(StatementHandle, colCount, row, charEncoding, wcharEncoding);
if (!SQL_SUCCEEDED(ret)) {
LOG("FetchOne_wrap: Error retrieving data with SQLGetData - SQLRETURN=%d", ret);
return ret;
}
} else if (ret != SQL_NO_DATA) {
LOG("FetchOne_wrap: Error when fetching data - SQLRETURN=%d", ret);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/test_017_fetchmany_fetchone_interleave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Test for GitHub Issue #427: Segmentation fault when interleaving fetchmany() and fetchone()
https://github.com/microsoft/mssql-python/issues/427
"""

import pytest


def test_fetchmany_then_fetchone_interleave(cursor):
"""
Test that fetchmany() followed by fetchone() doesn't cause segfault.
This was the original failing case from issue #427.
"""
cursor.execute("SELECT 1 UNION SELECT 2")

result1 = cursor.fetchmany(1)
assert len(result1) == 1
assert result1[0][0] == 1

# This used to cause segfault on Linux, return None on Windows
result2 = cursor.fetchone()
assert result2 is not None
assert result2[0] == 2


def test_fetchone_then_fetchmany_interleave(cursor):
"""Test that fetchone() followed by fetchmany() works correctly."""
cursor.execute("SELECT 1 UNION SELECT 2 UNION SELECT 3")

result1 = cursor.fetchone()
assert result1[0] == 1

result2 = cursor.fetchmany(2)
assert len(result2) == 2
assert result2[0][0] == 2
assert result2[1][0] == 3


def test_multiple_interleaved_fetches(cursor):
"""Test multiple alternating fetchmany() and fetchone() calls."""
cursor.execute("SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4")

result = cursor.fetchmany(1)
assert result[0][0] == 1

result = cursor.fetchone()
assert result[0] == 2

result = cursor.fetchmany(1)
assert result[0][0] == 3

result = cursor.fetchone()
assert result[0] == 4
Loading