|
5 | 5 |
|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
| 8 | +import atexit |
8 | 9 | import logging |
9 | 10 | import os |
10 | 11 | import pathlib |
|
16 | 17 | import weakref |
17 | 18 | from concurrent.futures import as_completed |
18 | 19 | from concurrent.futures.thread import ThreadPoolExecutor |
| 20 | +from contextlib import suppress |
19 | 21 | from difflib import get_close_matches |
20 | 22 | from functools import partial |
21 | 23 | from io import StringIO |
@@ -439,12 +441,8 @@ def __init__( |
439 | 441 |
|
440 | 442 | # get the imported modules from sys.modules |
441 | 443 | self._log_telemetry_imported_packages() |
442 | | - |
443 | | - def __del__(self) -> None: # pragma: no cover |
444 | | - try: |
445 | | - self.close(retry=False) |
446 | | - except Exception: |
447 | | - pass |
| 444 | + # check SNOW-1218851 for long term improvement plan to refactor ocsp code |
| 445 | + atexit.register(self._close_at_exit) |
448 | 446 |
|
449 | 447 | @property |
450 | 448 | def insecure_mode(self) -> bool: |
@@ -744,6 +742,8 @@ def connect(self, **kwargs) -> None: |
744 | 742 |
|
745 | 743 | def close(self, retry: bool = True) -> None: |
746 | 744 | """Closes the connection.""" |
| 745 | + # unregister to dereference connection object as it's already closed after the execution |
| 746 | + atexit.unregister(self._close_at_exit) |
747 | 747 | try: |
748 | 748 | if not self.rest: |
749 | 749 | logger.debug("Rest object has been destroyed, cannot close session") |
@@ -1771,6 +1771,10 @@ def _cache_query_status(self, sf_qid: str, status_ret: QueryStatus) -> None: |
1771 | 1771 | ) # Prevent KeyError when multiple threads try to remove the same query id |
1772 | 1772 | self._done_async_sfqids[sf_qid] = None |
1773 | 1773 |
|
| 1774 | + def _close_at_exit(self): |
| 1775 | + with suppress(Exception): |
| 1776 | + self.close(retry=False) |
| 1777 | + |
1774 | 1778 | def get_query_status(self, sf_qid: str) -> QueryStatus: |
1775 | 1779 | """Retrieves the status of query with sf_qid. |
1776 | 1780 |
|
|
0 commit comments