diff --git a/packages/leann-backend-diskann/leann_backend_diskann/diskann_backend.py b/packages/leann-backend-diskann/leann_backend_diskann/diskann_backend.py index 0440c10..192c4c2 100644 --- a/packages/leann-backend-diskann/leann_backend_diskann/diskann_backend.py +++ b/packages/leann-backend-diskann/leann_backend_diskann/diskann_backend.py @@ -459,3 +459,25 @@ class DiskannSearcher(BaseSearcher): string_labels = [[str(int_label) for int_label in batch_labels] for batch_labels in labels] return {"labels": string_labels, "distances": distances} + + def cleanup(self): + """Cleanup DiskANN-specific resources including C++ index.""" + # Call parent cleanup first + super().cleanup() + + # Delete the C++ index to trigger destructors + try: + if hasattr(self, "_index") and self._index is not None: + del self._index + self._index = None + self._current_zmq_port = None + except Exception: + pass + + # Force garbage collection to ensure C++ objects are destroyed + try: + import gc + + gc.collect() + except Exception: + pass diff --git a/packages/leann-backend-hnsw/leann_backend_hnsw/hnsw_backend.py b/packages/leann-backend-hnsw/leann_backend_hnsw/hnsw_backend.py index 1d5f635..77fbb85 100644 --- a/packages/leann-backend-hnsw/leann_backend_hnsw/hnsw_backend.py +++ b/packages/leann-backend-hnsw/leann_backend_hnsw/hnsw_backend.py @@ -245,3 +245,25 @@ class HNSWSearcher(BaseSearcher): string_labels = [[str(int_label) for int_label in batch_labels] for batch_labels in labels] return {"labels": string_labels, "distances": distances} + + def cleanup(self): + """Cleanup HNSW-specific resources including C++ ZMQ connections.""" + # Call parent cleanup first + super().cleanup() + + # Additional cleanup for C++ side ZMQ connections + # The ZmqDistanceComputer in C++ uses ZMQ connections that need cleanup + try: + # Delete the index to trigger C++ destructors + if hasattr(self, "index"): + del self.index + except Exception: + pass + + # Force garbage collection to ensure C++ objects are destroyed + try: + import gc + + gc.collect() + except Exception: + pass diff --git a/packages/leann-core/src/leann/api.py b/packages/leann-core/src/leann/api.py index 15c865e..bbf2769 100644 --- a/packages/leann-core/src/leann/api.py +++ b/packages/leann-core/src/leann/api.py @@ -614,14 +614,26 @@ class LeannSearcher: return enriched_results def cleanup(self): - """Explicitly cleanup embedding server resources. + """Explicitly cleanup embedding server and ZMQ resources. This method should be called after you're done using the searcher, especially in test environments or batch processing scenarios. """ + # Stop embedding server if hasattr(self.backend_impl, "embedding_server_manager"): self.backend_impl.embedding_server_manager.stop_server() + # Force cleanup of ZMQ connections (especially for C++ side) + try: + import zmq + + # Aggressively terminate all ZMQ contexts to prevent hanging + ctx = zmq.Context.instance() + ctx.linger = 0 + # Don't call destroy() here as it might affect other components + except Exception: + pass + class LeannChat: def __init__( diff --git a/packages/leann-core/src/leann/searcher_base.py b/packages/leann-core/src/leann/searcher_base.py index 5b9b80a..3306ca6 100644 --- a/packages/leann-core/src/leann/searcher_base.py +++ b/packages/leann-core/src/leann/searcher_base.py @@ -196,7 +196,26 @@ class BaseSearcher(LeannBackendSearcherInterface, ABC): """ pass - def __del__(self): - """Ensures the embedding server is stopped when the searcher is destroyed.""" + def cleanup(self): + """Cleanup resources including embedding server and ZMQ connections.""" + # Stop embedding server if hasattr(self, "embedding_server_manager"): self.embedding_server_manager.stop_server() + + # Force cleanup of ZMQ connections (especially for C++ side in HNSW/DiskANN) + try: + import zmq + + # Set short linger to prevent blocking + ctx = zmq.Context.instance() + ctx.linger = 0 + except Exception: + pass + + def __del__(self): + """Ensures resources are cleaned up when the searcher is destroyed.""" + try: + self.cleanup() + except Exception: + # Ignore errors during destruction + pass diff --git a/tests/conftest.py b/tests/conftest.py index a1e45a5..8f165be 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,13 +18,37 @@ def global_test_cleanup() -> Generator: yield # Cleanup after all tests + print("\n🧹 Running global test cleanup...") + + # 1. Force cleanup of any LeannSearcher instances + try: + import gc + + # Force garbage collection to trigger __del__ methods + gc.collect() + time.sleep(0.2) + except Exception: + pass + + # 2. Terminate ZMQ contexts more aggressively try: import zmq - # Set a very short linger on any remaining contexts - # This prevents blocking on context termination + # Get the global instance and destroy it ctx = zmq.Context.instance() ctx.linger = 0 + + # Force termination - this is aggressive but needed for CI + try: + ctx.destroy(linger=0) + except Exception: + pass + + # Also try to terminate the default context + try: + zmq.Context.term(zmq.Context.instance()) + except Exception: + pass except Exception: pass @@ -78,6 +102,32 @@ def global_test_cleanup() -> Generator: pass +@pytest.fixture +def auto_cleanup_searcher(): + """Fixture that automatically cleans up LeannSearcher instances.""" + searchers = [] + + def register(searcher): + """Register a searcher for cleanup.""" + searchers.append(searcher) + return searcher + + yield register + + # Cleanup all registered searchers + for searcher in searchers: + try: + searcher.cleanup() + except Exception: + pass + + # Force garbage collection + import gc + + gc.collect() + time.sleep(0.1) + + @pytest.fixture(autouse=True) def cleanup_after_each_test(): """Cleanup after each test to prevent resource leaks."""