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

[FSTORE-1572] Add finalizer to Connection #365

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
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
8 changes: 7 additions & 1 deletion python/hopsworks_common/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import re
import sys
import warnings
import weakref
from typing import Any, Optional

from hopsworks_common import client, usage, util, version
Expand Down Expand Up @@ -351,6 +352,7 @@ def connect(self) -> None:
"""
client.stop()
self._connected = True
finalizer = weakref.finalize(self, self.close)
try:
# determine engine, needed to init client
if (self._engine is not None and self._engine.lower() == "spark") or (
Expand Down Expand Up @@ -413,6 +415,7 @@ def connect(self) -> None:
self._provide_project()
except (TypeError, ConnectionError):
self._connected = False
finalizer.detach()
raise

self._check_compatibility()
Expand Down Expand Up @@ -446,7 +449,7 @@ def close(self) -> None:
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.

!!! example
```python
Expand All @@ -455,6 +458,9 @@ def close(self) -> None:
conn.close()
```
"""
if not self._connected:
return # the connection is already closed

from hsfs import engine

if OpenSearchClientSingleton._instance:
Expand Down
Loading