Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 53
FIX: Segmentation Fault in libmsodbcsql-18.5 during SQLFreeHandle()#415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b6ff99f
segfault fix
subrata-ms b7f105f
test addition and linting fix
subrata-ms 2e21aab
Merge branch 'main' into subrata-ms/GCSegFault
subrata-ms b532a63
mssql_python/pybind/connection/connection.h
subrata-ms 245a75d
mssql_python/pybind/connection/connection.h
subrata-ms 97c009d
fixed issues
subrata-ms 498dfef
cleanning the unnecessary files
subrata-ms af08801
Merge branch 'main' into subrata-ms/GCSegFault
subrata-ms 88a6167
review comment
subrata-ms bdf1a97
review comment
subrata-ms 80e37f2
latest review comment
subrata-ms 563b8ad
review comments
subrata-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1144,6 +1144,21 @@ SQLSMALLINT SqlHandle::type() const { | ||
| return _type; | ||
| } | ||
| void SqlHandle::markImplicitlyFreed() { | ||
| // SAFETY: Only STMT handles should be marked as implicitly freed. | ||
| // When a DBC handle is freed, the ODBC driver automatically frees all child STMT handles. | ||
| // Other handle types (ENV, DBC, DESC) are NOT automatically freed by parents. | ||
| // Calling this on wrong handle types will cause silent handle leaks. | ||
| if (_type != SQL_HANDLE_STMT) { | ||
| // Log error but don't throw - we're likely in cleanup/destructor path | ||
| LOG_ERROR("SAFETY VIOLATION: Attempted to mark non-STMT handle as implicitly freed. " | ||
| "Handle type=%d. This will cause handle leak. Only STMT handles are " | ||
| "automatically freed by parent DBC handles.", _type); | ||
| return; // Refuse to mark - let normal free() handle it | ||
| } | ||
| _implicitly_freed = true; | ||
| } | ||
| /* | ||
| * IMPORTANT: Never log in destructors - it causes segfaults. | ||
| * During program exit, C++ destructors may run AFTER Python shuts down. | ||
| @@ -1169,16 +1184,19 @@ void SqlHandle::free() { | ||
| return; | ||
| } | ||
| // Always clean up ODBC resources, regardless of Python state | ||
| // CRITICAL FIX: Check if handle was already implicitly freed by parent handle | ||
| // When Connection::disconnect() frees the DBC handle, the ODBC driver automatically | ||
| // frees all child STMT handles. We track this state to avoid double-free attempts. | ||
| // This approach avoids calling ODBC functions on potentially-freed handles, which | ||
| // would cause use-after-free errors. | ||
| if (_implicitly_freed) { | ||
subrata-ms marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _handle = nullptr; // Just clear the pointer, don't call ODBC functions | ||
| return; | ||
| } | ||
| // Handle is valid and not implicitly freed, proceed with normal freeing | ||
| SQLFreeHandle_ptr(_type, _handle); | ||
| _handle = nullptr; | ||
| // Only log if Python is not shutting down (to avoid segfault) | ||
| if (!pythonShuttingDown) { | ||
| // Don't log during destruction - even in normal cases it can be | ||
| // problematic If logging is needed, use explicit close() methods | ||
| // instead | ||
| } | ||
| } | ||
| } | ||
| @@ -2893,7 +2911,6 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p | ||
| // Cache decimal separator to avoid repeated system calls | ||
| for (SQLSMALLINT i = 1; i <= colCount; ++i) { | ||
| SQLWCHAR columnName[256]; | ||
| SQLSMALLINT columnNameLen; | ||
| @@ -3615,8 +3632,6 @@ SQLRETURN FetchBatchData(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& colum | ||
| columnInfos[col].processedColumnSize + 1; // +1 for null terminator | ||
| } | ||
| // Performance: Build function pointer dispatch table (once per batch) | ||
| // This eliminates the switch statement from the hot loop - 10,000 rows × 10 | ||
| // cols reduces from 100,000 switch evaluations to just 10 switch | ||
| @@ -4033,8 +4048,8 @@ SQLRETURN FetchMany_wrap(SqlHandlePtr StatementHandle, py::list& rows, int fetch | ||
| lobColumns.push_back(i + 1); // 1-based | ||
| } | ||
| } | ||
| // Initialized to 0 for LOB path counter; overwritten by ODBC in non-LOB path; | ||
| // Initialized to 0 for LOB path counter; overwritten by ODBC in non-LOB path; | ||
| SQLULEN numRowsFetched = 0; | ||
| // If we have LOBs → fall back to row-by-row fetch + SQLGetData_wrap | ||
| if (!lobColumns.empty()) { | ||
| @@ -4066,7 +4081,7 @@ SQLRETURN FetchMany_wrap(SqlHandlePtr StatementHandle, py::list& rows, int fetch | ||
| LOG("FetchMany_wrap: Error when binding columns - SQLRETURN=%d", ret); | ||
| return ret; | ||
| } | ||
| SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)(intptr_t)fetchSize, 0); | ||
| SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &numRowsFetched, 0); | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.