diff --git a/qdrant_client/async_client_base.py b/qdrant_client/async_client_base.py index aeb4224ae..d86d1d51f 100644 --- a/qdrant_client/async_client_base.py +++ b/qdrant_client/async_client_base.py @@ -348,6 +348,11 @@ async def recover_shard_snapshot( async def close(self, **kwargs: Any) -> None: pass + @property + def closed(self) -> bool: + """Whether close() has been called on this client.""" + return False + def migrate( self, dest_client: "AsyncQdrantBase", diff --git a/qdrant_client/async_qdrant_client.py b/qdrant_client/async_qdrant_client.py index 4e58bd04d..857b0f2b3 100644 --- a/qdrant_client/async_qdrant_client.py +++ b/qdrant_client/async_qdrant_client.py @@ -155,6 +155,15 @@ def __init__( is_local_mode=isinstance(self._client, AsyncQdrantLocal), ) + @property + def closed(self) -> bool: + """Whether the connection to Qdrant has been closed + + Returns: + True once close() has been called, False otherwise + """ + return self._client.closed + async def close(self, grpc_grace: float | None = None, **kwargs: Any) -> None: """Closes the connection to Qdrant diff --git a/qdrant_client/client_base.py b/qdrant_client/client_base.py index d16362633..d5ad4a85c 100644 --- a/qdrant_client/client_base.py +++ b/qdrant_client/client_base.py @@ -375,6 +375,11 @@ def recover_shard_snapshot( def close(self, **kwargs: Any) -> None: pass + @property + def closed(self) -> bool: + """Whether close() has been called on this client.""" + return False + def migrate( self, dest_client: "QdrantBase", diff --git a/qdrant_client/qdrant_client.py b/qdrant_client/qdrant_client.py index 5ee0f5c24..b03b0bb64 100644 --- a/qdrant_client/qdrant_client.py +++ b/qdrant_client/qdrant_client.py @@ -168,6 +168,15 @@ def __init__( def __del__(self) -> None: self.close() + @property + def closed(self) -> bool: + """Whether the connection to Qdrant has been closed + + Returns: + True once close() has been called, False otherwise + """ + return self._client.closed + def close(self, grpc_grace: float | None = None, **kwargs: Any) -> None: """Closes the connection to Qdrant diff --git a/tests/test_closed.py b/tests/test_closed.py new file mode 100644 index 000000000..768480c0f --- /dev/null +++ b/tests/test_closed.py @@ -0,0 +1,53 @@ +import pytest + +from qdrant_client import AsyncQdrantClient, QdrantClient + + +def test_local_client_reports_closed_state(): + client = QdrantClient(":memory:") + assert client.closed is False + + client.close() + assert client.closed is True + + +def test_persistent_local_client_reports_closed_state(tmp_path): + client = QdrantClient(path=str(tmp_path / "storage")) + assert client.closed is False + + client.close() + assert client.closed is True + + +def test_remote_client_reports_closed_state(): + client = QdrantClient("localhost", port=6333, check_compatibility=False) + assert client.closed is False + + client.close() + assert client.closed is True + + +def test_closed_matches_the_inner_client(): + client = QdrantClient(":memory:") + assert client.closed is client._client.closed + + client.close() + assert client.closed is client._client.closed + + +@pytest.mark.asyncio +async def test_async_local_client_reports_closed_state(): + client = AsyncQdrantClient(":memory:") + assert client.closed is False + + await client.close() + assert client.closed is True + + +@pytest.mark.asyncio +async def test_async_remote_client_reports_closed_state(): + client = AsyncQdrantClient("localhost", port=6333, check_compatibility=False) + assert client.closed is False + + await client.close() + assert client.closed is True