Skip to content

Commit 6e00afb

Browse files
committed
stopped logging during destruction since that cause GIL issues
1 parent 56634ce commit 6e00afb

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

mssql_python/connection.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,10 @@ def __del__(self):
277277
This is a safety net to ensure resources are cleaned up
278278
even if close() was not called explicitly.
279279
"""
280-
if not self._closed:
280+
if "_closed" not in self.__dict__ or not self._closed:
281281
try:
282282
self.close()
283283
except Exception as e:
284-
raise InterfaceError(
285-
driver_error=str(e),
286-
ddbc_error="Error during python connection cleanup"
287-
)
284+
pass
285+
# if logger:
286+
# logger.error(f"Error during connection cleanup: {e}")

mssql_python/cursor.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,10 @@ def __del__(self):
736736
This is a safety net to ensure resources are cleaned up
737737
even if close() was not called explicitly.
738738
"""
739-
if not self.closed:
739+
if "_closed" not in self.__dict__ or not self._closed:
740740
try:
741741
self.close()
742742
except Exception as e:
743-
raise InterfaceError(
744-
driver_error=str(e),
745-
ddbc_error="Error during python connection cleanup"
746-
)
743+
pass
744+
# if logger:
745+
# logger.error(f"Error during cursor cleanup in __del__: {e}")

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -530,27 +530,20 @@ void HandleZeroColumnSizeAtFetch(SQLULEN& columnSize) {
530530
// TODO: Revisit GIL considerations if we're using python's logger
531531
template <typename... Args>
532532
void LOG(const std::string& formatString, Args&&... args) {
533-
// Get the logger each time to ensure we have the most up-to-date logger state
534-
py::object logging = py::module_::import("mssql_python.logging_config").attr("get_logger")();
535-
if (py::isinstance<py::none>(logging)) {
536-
return;
537-
}
538-
533+
py::gil_scoped_acquire gil; // <---- this ensures safe Python API usage
534+
535+
py::object logger = py::module_::import("mssql_python.logging_config").attr("get_logger")();
536+
if (py::isinstance<py::none>(logger)) return;
537+
539538
try {
540-
// Add prefix to all logs
541539
std::string ddbcFormatString = "[DDBC Bindings log] " + formatString;
542-
543-
// Handle both formatted and non-formatted cases
544540
if constexpr (sizeof...(args) == 0) {
545-
// No formatting needed, just use the string directly
546-
logging.attr("debug")(py::str(ddbcFormatString));
541+
logger.attr("debug")(py::str(ddbcFormatString));
547542
} else {
548-
// Apply formatting
549543
py::str message = py::str(ddbcFormatString).format(std::forward<Args>(args)...);
550-
logging.attr("debug")(message);
544+
logger.attr("debug")(message);
551545
}
552546
} catch (const std::exception& e) {
553-
// Fallback in case of Python error - don't let logging errors crash the application
554547
std::cerr << "Logging error: " << e.what() << std::endl;
555548
}
556549
}
@@ -799,9 +792,7 @@ void SqlHandle::free() {
799792
}
800793
SQLFreeHandle_ptr(_type, _handle);
801794
_handle = nullptr;
802-
std::stringstream ss;
803-
ss << "Freed SQL Handle of type: " << type_str;
804-
LOG(ss.str());
795+
// Don't log during destruction - it can cause segfaults during Python shutdown
805796
}
806797
}
807798

0 commit comments

Comments
 (0)