Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fixing test failure
  • Loading branch information
subrata-ms committed Apr 3, 2026
commit 6b6cf82a42788bef207475d9a36ba7f98d7e67c9
8 changes: 8 additions & 0 deletions mssql_python/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,14 @@ def __del__(self) -> None:
This is a safety net to ensure resources are cleaned up
even if close() was not called explicitly.
"""
import sys

# During interpreter shutdown, ODBC handles may already be invalid.
# Attempting to free them can cause segfaults (SIGSEGV).
# The OS will reclaim all resources when the process exits.
if sys is None or sys._is_finalizing():
return

if "_closed" not in self.__dict__ or not self._closed:
try:
self.close()
Expand Down
14 changes: 8 additions & 6 deletions mssql_python/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3021,17 +3021,19 @@ def __del__(self):
even if close() was not called explicitly.
If the cursor is already closed, it will not raise an exception during cleanup.
"""
import sys

# During interpreter shutdown, ODBC handles may already be invalid.
# Attempting to free them can cause segfaults (SIGSEGV).
# The OS will reclaim all resources when the process exits.
if sys is None or sys._is_finalizing():
return

if "closed" not in self.__dict__ or not self.closed:
try:
self.close()
except Exception as e: # pylint: disable=broad-exception-caught
# Don't raise an exception in __del__, just log it
# If interpreter is shutting down, we might not have logging set up
import sys

if sys and sys._is_finalizing():
# Suppress logging during interpreter shutdown
return
logger.debug("Exception during cursor cleanup in __del__: %s", e)

def scroll(
Expand Down
Loading