Skip to content

Commit 23eda6e

Browse files
authored
SNOW-996732: improve clean up logic of connection (#1898)
1 parent 1718a94 commit 23eda6e

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

DESCRIPTION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
2222
- NOTE: this has not been tested extensively, but has been shown to improve the experience when using WSL
2323
- Bumped platformdirs from >=2.6.0,<4.0.0 to >=2.6.0,<5.0.0
2424
- Updated diagnostics to use system$allowlist instead of system$whitelist.
25-
- Updated diagnostics to use system$allowlist instead of system$whitelist.
26-
- Update `write_pandas` to skip TABLE IF NOT EXISTS in truncate mode
25+
- Update `write_pandas` to skip TABLE IF NOT EXISTS in truncate mode.
26+
- Improved cleanup logic for connection to rely on interpreter shutdown instead of the `__del__` method.
2727

2828
- v3.7.1(February 21, 2024)
2929

src/snowflake/connector/connection.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import atexit
89
import logging
910
import os
1011
import pathlib
@@ -16,6 +17,7 @@
1617
import weakref
1718
from concurrent.futures import as_completed
1819
from concurrent.futures.thread import ThreadPoolExecutor
20+
from contextlib import suppress
1921
from difflib import get_close_matches
2022
from functools import partial
2123
from io import StringIO
@@ -439,12 +441,8 @@ def __init__(
439441

440442
# get the imported modules from sys.modules
441443
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)
448446

449447
@property
450448
def insecure_mode(self) -> bool:
@@ -744,6 +742,8 @@ def connect(self, **kwargs) -> None:
744742

745743
def close(self, retry: bool = True) -> None:
746744
"""Closes the connection."""
745+
# unregister to dereference connection object as it's already closed after the execution
746+
atexit.unregister(self._close_at_exit)
747747
try:
748748
if not self.rest:
749749
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:
17711771
) # Prevent KeyError when multiple threads try to remove the same query id
17721772
self._done_async_sfqids[sf_qid] = None
17731773

1774+
def _close_at_exit(self):
1775+
with suppress(Exception):
1776+
self.close(retry=False)
1777+
17741778
def get_query_status(self, sf_qid: str) -> QueryStatus:
17751779
"""Retrieves the status of query with sf_qid.
17761780

test/integ/test_connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,11 @@ def test_ocsp_mode_insecure(conn_cnx, is_public_test, caplog):
13421342
assert "snowflake.connector.ocsp_snowflake" in caplog.text
13431343
else:
13441344
assert "snowflake.connector.ocsp_snowflake" not in caplog.text
1345+
1346+
1347+
@pytest.mark.skipolddriver
1348+
def test_connection_atexit_close(conn_cnx):
1349+
"""Basic Connection test without schema."""
1350+
with conn_cnx() as conn:
1351+
conn._close_at_exit()
1352+
assert conn.is_closed()

0 commit comments

Comments
 (0)