@@ -530,27 +530,20 @@ void HandleZeroColumnSizeAtFetch(SQLULEN& columnSize) {
530530// TODO: Revisit GIL considerations if we're using python's logger
531531template <typename ... Args>
532532void 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