Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finalizer to Connection #366

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions python/hopsworks/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

import os
import re
import warnings
import sys
import warnings
import weakref

from requests.exceptions import ConnectionError

from hopsworks.decorators import connected, not_connected
from hopsworks import client, version
from hopsworks.core import project_api, secret_api, variable_api
from hopsworks.decorators import connected, not_connected
from requests.exceptions import ConnectionError


HOPSWORKS_PORT_DEFAULT = 443
HOSTNAME_VERIFICATION_DEFAULT = True
Expand Down Expand Up @@ -207,7 +208,7 @@

if major_minor_backend != major_minor_client:
print("\n", file=sys.stderr)
warnings.warn(

Check failure on line 211 in python/hopsworks/connection.py

View workflow job for this annotation

GitHub Actions / Lint and Stylecheck

Ruff (B028)

python/hopsworks/connection.py:211:13: B028 No explicit `stacklevel` keyword argument found
"The installed hopsworks client version {0} may not be compatible with the connected Hopsworks backend version {1}. \nTo ensure compatibility please install the latest bug fix release matching the minor version of your backend ({2}) by running 'pip install hopsworks=={2}.*'".format(
client_version, backend_version, major_minor_backend
)
Expand Down Expand Up @@ -241,6 +242,7 @@
"""
client.stop()
self._connected = True
finalizer = weakref.finalize(self, self.close)
try:
# init client
if client.base.Client.REST_ENDPOINT not in os.environ:
Expand All @@ -263,6 +265,7 @@
self._variable_api = variable_api.VariableApi()
except (TypeError, ConnectionError):
self._connected = False
finalizer.detach()
raise
print(
"Connected. Call `.close()` to terminate connection gracefully.",
Expand All @@ -278,7 +281,7 @@
This will clean up any materialized certificates on the local file system of
external environments such as AWS SageMaker.

Usage is recommended but optional.
Usage is optional.
"""
from hsfs import client as hsfs_client
from hsfs import engine as hsfs_engine
Expand All @@ -299,6 +302,9 @@
except: # noqa: E722
pass

if not self._connected:
return # the connection is already closed

client.stop()
self._connected = False

Expand Down
Loading