Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 21 additions & 4 deletions src/databricks/sql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pyarrow = None
import json
import os
import sys
import decimal
from urllib.parse import urlparse
from uuid import UUID
Expand Down Expand Up @@ -516,20 +517,36 @@ def close(self) -> None:
self._close()

def _close(self, close_cursors=True) -> None:
# Check if Python is shutting down
shutting_down = sys.meta_path is None

Comment on lines +520 to +522
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shutdown check variable is computed once at the beginning of _close, but Python shutdown state could change during the execution of this method. Consider checking sys.meta_path is None directly in each exception handler for more accurate shutdown detection.

Copilot uses AI. Check for mistakes.
if close_cursors:
for cursor in self._cursors:
cursor.close()
try:
cursor.close()
except Exception:
if not shutting_down:
logger.debug("Error closing cursor during connection close", exc_info=True)
Comment on lines +525 to +529
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching bare Exception is too broad and could mask unexpected errors during normal operation. Consider catching specific exceptions or at minimum using except Exception as e: to log the exception details when not shutting down.

Copilot uses AI. Check for mistakes.

try:
self.session.close()
except Exception as e:
logger.error(f"Attempt to close session raised a local exception: {e}")
if not shutting_down:
logger.error(f"Attempt to close session raised a local exception: {e}")

TelemetryClientFactory.close(host_url=self.session.host)
try:
TelemetryClientFactory.close(host_url=self.session.host)
except Exception:
if not shutting_down:
logger.debug("Error closing telemetry client", exc_info=True)
Comment on lines +537 to +541
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching bare Exception is too broad and could mask unexpected errors during normal operation. Consider catching specific exceptions or at minimum using except Exception as e: to log the exception details when not shutting down.

Copilot uses AI. Check for mistakes.

# Close HTTP client that was created by this connection
if self.http_client:
self.http_client.close()
try:
self.http_client.close()
except Exception:
if not shutting_down:
logger.debug("Error closing HTTP client", exc_info=True)
Comment on lines +545 to +549
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching bare Exception is too broad and could mask unexpected errors during normal operation. Consider catching specific exceptions or at minimum using except Exception as e: to log the exception details when not shutting down.

Copilot uses AI. Check for mistakes.

@property
def autocommit(self) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion src/databricks/sql/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def close(self) -> None:
"Attempt to close session raised an exception at the server: %s", e
)
except Exception as e:
logger.error("Attempt to close session raised a local exception: %s", e)
if getattr(sys, "meta_path", None):
logger.error("Attempt to close session raised a local exception: %s", e)

self.is_open = False