diff --git a/src/databricks/sql/client.py b/src/databricks/sql/client.py index a0215aae..e5e064b7 100755 --- a/src/databricks/sql/client.py +++ b/src/databricks/sql/client.py @@ -8,6 +8,7 @@ pyarrow = None import json import os +import sys import decimal from urllib.parse import urlparse from uuid import UUID @@ -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 + 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) 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) # 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) @property def autocommit(self) -> bool: diff --git a/src/databricks/sql/session.py b/src/databricks/sql/session.py index 0f723d14..1bca3f91 100644 --- a/src/databricks/sql/session.py +++ b/src/databricks/sql/session.py @@ -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