Skip to content

Commit c4cf081

Browse files
committed
Adding Python shutdown check in _close
1 parent 4b7df5b commit c4cf081

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/databricks/sql/client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
pyarrow = None
99
import json
1010
import os
11+
import sys
1112
import decimal
1213
from urllib.parse import urlparse
1314
from uuid import UUID
@@ -516,20 +517,36 @@ def close(self) -> None:
516517
self._close()
517518

518519
def _close(self, close_cursors=True) -> None:
520+
# Check if Python is shutting down
521+
shutting_down = sys.meta_path is None
522+
519523
if close_cursors:
520524
for cursor in self._cursors:
521-
cursor.close()
525+
try:
526+
cursor.close()
527+
except Exception:
528+
if not shutting_down:
529+
logger.debug("Error closing cursor during connection close", exc_info=True)
522530

523531
try:
524532
self.session.close()
525533
except Exception as e:
526-
logger.error(f"Attempt to close session raised a local exception: {e}")
534+
if not shutting_down:
535+
logger.error(f"Attempt to close session raised a local exception: {e}")
527536

528-
TelemetryClientFactory.close(host_url=self.session.host)
537+
try:
538+
TelemetryClientFactory.close(host_url=self.session.host)
539+
except Exception:
540+
if not shutting_down:
541+
logger.debug("Error closing telemetry client", exc_info=True)
529542

530543
# Close HTTP client that was created by this connection
531544
if self.http_client:
532-
self.http_client.close()
545+
try:
546+
self.http_client.close()
547+
except Exception:
548+
if not shutting_down:
549+
logger.debug("Error closing HTTP client", exc_info=True)
533550

534551
@property
535552
def autocommit(self) -> bool:

0 commit comments

Comments
 (0)