Commit d3b1229
FIX: Fixing segmentation fault issue due to double free on SqlHandle::free in Linux (#361)
### Work Item / Issue Reference
>
[AB#40714](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/40714)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #341
-------------------------------------------------------------------
### Summary
**Problem:** Segmentation fault occurs during Python's garbage
collection when freeing ODBC handles.
**Stack Trace Analysis:**
0-2: Signal handler (SIGSEGV)
3: libmsodbcsql-18.5.so.1.1 - CRASH LOCATION
4: SQLFreeHandle() from ODBC driver
5: SqlHandle::free() from ddbc_bindings
6-11: pybind11 call stack
12: Python method call
20: slot_tp_finalize during __del__
21-27: Python GC finalizing objects
**Root Cause:** The crash occurs when;
1. Python's garbage collector runs during finalization
2. Objects with [__del__] methods are being cleaned up
3. The [__del__] calls [close()] which internally calls SQLFreeHandle()
4. The ODBC driver crashes because:
- Handle is already freed (double-free)
- Handle is in invalid state
- Connection handle freed before cursor statement handle
- Wrong finalization order due to circular references
**Fix Details:**
This pull request introduces a critical fix to the handle cleanup logic
in the SQL bindings for Python, specifically addressing potential
segfaults during interpreter shutdown. The change ensures that both
statement and database connection handles are not freed if Python is
shutting down, preventing invalid memory access.
Handle cleanup logic improvements:
* Updated the `SqlHandle::free()` method in
`mssql_python/pybind/ddbc_bindings.cpp` to skip freeing both statement
(`SQL_HANDLE_STMT`) and database connection (`SQL_HANDLE_DBC`) handles
during Python shutdown, rather than only statement handles. This
prevents segfaults caused by freeing handles in the wrong order when
their parent resources may have already been released.
---------
Co-authored-by: subrata-ms <subrata@microsoft.com>1 parent a3fde2c commit d3b1229
File tree
4 files changed
+1445
-5
lines changed- mssql_python
- pybind
- tests
4 files changed
+1445
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
| 9 | + | |
8 | 10 | | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
70 | 116 | | |
71 | 117 | | |
72 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
315 | 332 | | |
316 | 333 | | |
317 | 334 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1163 | 1163 | | |
1164 | 1164 | | |
1165 | 1165 | | |
1166 | | - | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
1171 | 1175 | | |
1172 | 1176 | | |
1173 | 1177 | | |
| |||
0 commit comments